-
Notifications
You must be signed in to change notification settings - Fork 0
/
Message.php
128 lines (96 loc) · 3.46 KB
/
Message.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**
* The MIT License (MIT)
*
* Copyright (C) 2013 Jonathan Nacionales
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
abstract class Message {
private $properties = array();
final public function getArray() {
return $this->properties;
}
final public function __construct( array $args = null ) {
$properties = static::getProperties();
if( $args == null )
return;
foreach( $args as $key => $value ) {
$this->set( $key, $value );
}
}
final public function __call( $functionName, $args ) {
if( strpos( $functionName, "get" ) === 0 ) {
$variableName = lcfirst( substr( $functionName, strlen("get") ) );
$variableValue = $this->get( $variableName );
return $variableValue;
}
if( strpos( $functionName, "set" ) === 0 ) {
$variableName = lcfirst( substr( $functionName, strlen("set") ) );
return $this->set( $variableName, $args[0] );
}
throw new \Exception(
sprintf("Tried to call unknown method %s::%s",
get_class($this),
$functionName
)
);
}
final public function __get( $key ) {
$properties = static::getProperties();
return $this->get( $key );
}
final public function __set( $key, $value ) {
return $this->set( $key, $value );
}
final protected function get( $key ) {
if( !array_key_exists( $key, $this->properties ) )
return null;
return $this->properties[ $key ];
}
final protected function set( $key, $value ) {
$errors = $this->getSetterExceptions( $key, $value );
if( !empty($errors) )
throw $errors[0];
$this->properties[$key] = $value;
return $this;
}
final protected function getSetterExceptions( $key, $value ) {
$properties = static::getProperties();
$errors = array();
if( !array_key_exists( $key, $properties ) )
$errors[] = new \InvalidArgumentException(
sprintf( "'%s' is not a valid property for %s",
$key,
get_class( $this )
)
);
$property = (object)$properties[$key];
$type = $this->getType( $value );
if( $property->type !== $type )
$errors[] = new \InvalidArgumentException(
sprintf( "'%s' is of type '%s' but this function expected '%s' for class '%s'",
$key,
$type,
$property->type,
get_class( $this )
)
);
return $errors;
}
final public static function getInstance() {
return new static();
}
final protected function getType( $value ) {
$property = gettype( $value );
if( $property === "object" )
return get_class( $value );
return $property;
}
abstract public static function getProperties();
}
?>