1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace DedicatedApi\Structures;
13:
14: abstract class AbstractStructure
15: {
16: static public function fromArray($array)
17: {
18: if(!is_array($array))
19: return $array;
20:
21: $object = new static;
22: foreach($array as $key => $value)
23: $object->{lcfirst($key)} = $value;
24: return $object;
25: }
26:
27: static public function fromArrayOfArray($array)
28: {
29: if(!is_array($array))
30: return $array;
31:
32: $result = array();
33: foreach($array as $key => $value)
34: $result[$key] = static::fromArray($value);
35: return $result;
36: }
37:
38: static public function getPropertyFromArray($array, $property)
39: {
40: return array_map(get_called_class().'::extractProperty', $array, array_fill(0, count($array), $property));
41: }
42:
43: static protected function ($element, $property)
44: {
45: if(!is_a($element, get_called_class()) || !property_exists($element, $property))
46: throw new \InvalidArgumentException('property '.$property.' does not exists in class: '.get_called_class());
47:
48: return $element->$property;
49: }
50:
51: function toArray()
52: {
53: $out = array();
54: foreach(get_object_vars($this) as $key => $value)
55: $out[ucfirst($key)] = $value;
56: return $out;
57: }
58: }
59:
60: ?>