1 | <?php |
||
28 | class AbstractObject { |
||
29 | /** |
||
30 | * @var mixed[] set of key value pairs representing data |
||
31 | */ |
||
32 | protected $data = array(); |
||
33 | protected $_type_checker; |
||
|
|||
34 | |||
35 | public function __construct() { |
||
36 | $this->data = static::getFieldsEnum()->getValuesMap(); |
||
37 | $this->_type_checker = new TypeChecker( |
||
38 | static::getFieldTypes(), static::getReferencedEnums()); |
||
39 | } |
||
40 | |||
41 | protected static function getFieldTypes() { |
||
42 | $fields_enum = static::getFieldsEnum(); |
||
43 | if (method_exists($fields_enum, 'getFieldTypes')) { |
||
44 | return $fields_enum->getFieldTypes(); |
||
45 | } else { |
||
46 | return array(); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | protected static function getReferencedEnums() { |
||
51 | return array(); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param string $name |
||
56 | * @param mixed $value |
||
57 | */ |
||
58 | public function __set($name, $value) { |
||
59 | if (ApiConfig::TYPE_CHECKER_STRICT_MODE |
||
60 | && $this->_type_checker->isValidParam($name) |
||
61 | ) { |
||
62 | if ($this->_type_checker->isValidParamPair($name, $value)) { |
||
63 | $this->data[$name] = $value; |
||
64 | } else { |
||
65 | throw new \InvalidArgumentException( |
||
66 | $name." and ".$this->exportValue($value) |
||
67 | ." are not a valid type value pair"); |
||
68 | } |
||
69 | } else { |
||
70 | $this->data[$name] = $value; |
||
71 | } |
||
72 | return $this; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param string $name |
||
77 | * @return mixed |
||
78 | * @throws \InvalidArgumentException |
||
79 | */ |
||
80 | public function __get($name) { |
||
81 | if (array_key_exists($name, $this->data)) { |
||
82 | return $this->data[$name]; |
||
83 | } else { |
||
84 | throw new \InvalidArgumentException( |
||
85 | $name.' is not a field of '.get_class($this)); |
||
86 | } |
||
87 | } |
||
88 | /** |
||
89 | * @param string $name |
||
90 | * @return boolean |
||
91 | */ |
||
92 | public function __isset($name) { |
||
93 | return array_key_exists($name, $this->data); |
||
94 | } |
||
95 | /** |
||
96 | * @param array |
||
97 | * @return $this |
||
98 | */ |
||
99 | public function setData(array $data) { |
||
100 | foreach ($data as $key => $value) { |
||
101 | $this->{$key} = $value; |
||
102 | } |
||
103 | // Handle class-specific situations |
||
104 | if (method_exists($this, 'setDataTrigger')) { |
||
105 | $this->setDataTrigger($data); |
||
106 | } |
||
107 | |||
108 | return $this; |
||
109 | } |
||
110 | /** |
||
111 | * Like setData but will skip field validation |
||
112 | * |
||
113 | * @param array |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function setDataWithoutValidation(array $data) { |
||
117 | foreach ($data as $key => $value) { |
||
118 | $this->data[$key] = $value; |
||
119 | } |
||
120 | // Handle class-specific situations |
||
121 | if (method_exists($this, 'setDataTrigger')) { |
||
122 | $this->setDataTrigger($data); |
||
123 | } |
||
124 | return $this; |
||
125 | } |
||
126 | /** |
||
127 | * @return array |
||
128 | */ |
||
129 | public function getData() { |
||
132 | /** |
||
133 | * @param mixed $value |
||
134 | * @return mixed |
||
135 | */ |
||
136 | protected function exportValue($value) { |
||
137 | $result = $value; |
||
138 | switch (true) { |
||
139 | case $value === null: |
||
140 | break; |
||
141 | case $value instanceof AbstractObject: |
||
142 | $result = $value->exportData(); |
||
143 | break; |
||
144 | case is_array($value): |
||
145 | $result = array(); |
||
146 | foreach ($value as $key => $sub_value) { |
||
147 | if ($sub_value !== null) { |
||
148 | $result[$key] = $this->exportValue($sub_value); |
||
149 | } |
||
150 | } |
||
151 | break; |
||
152 | } |
||
153 | return $result; |
||
154 | } |
||
155 | /** |
||
156 | * @return array |
||
157 | */ |
||
158 | public function exportData() { |
||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | public function exportAllData() { |
||
165 | return $this->exportValue($this->data); |
||
166 | } |
||
167 | /** |
||
168 | * @return EmptyEnum |
||
169 | */ |
||
170 | public static function getFieldsEnum() { |
||
171 | return EmptyEnum::getInstance(); |
||
172 | } |
||
173 | /** |
||
174 | * @return array |
||
175 | */ |
||
176 | public static function getFields() { |
||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | public static function className() { |
||
185 | } |
||
186 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.