1 | <?php |
||
47 | abstract class AbstractGenericProperties extends AbstractProperties implements GenericPropertiesInterface |
||
48 | { |
||
49 | |||
50 | /** |
||
51 | * Property collection constructor |
||
52 | * |
||
53 | * @param array $data Property data |
||
54 | * @param ObjectInterface $object Owner object |
||
55 | */ |
||
56 | 18 | public function __construct(array $data, ObjectInterface $object) |
|
60 | |||
61 | /** |
||
62 | * Get a property value |
||
63 | * |
||
64 | * Multi-level properties might be traversed by property name paths separated with colons (":"). |
||
65 | * |
||
66 | * @param string $property Property name |
||
67 | * @return mixed Property value |
||
68 | * @throws InvalidArgumentException If the property name is invalid |
||
69 | */ |
||
70 | 2 | public function getProperty($property) |
|
71 | { |
||
72 | 2 | $propertyPath = $this->buildPropertyPath($property); |
|
73 | |||
74 | // Traverse the property tree |
||
75 | 1 | $propertyPathSteps = []; |
|
76 | 1 | $data =& $this->data; |
|
77 | 1 | foreach ($propertyPath as $property) { |
|
78 | 1 | $propertyPathSteps[] = $property; |
|
79 | |||
80 | // If the property name step is invalid |
||
81 | 1 | if (!array_key_exists($property, $data)) { |
|
82 | 1 | throw new InvalidArgumentException( |
|
83 | sprintf( |
||
84 | 1 | 'Invalid property name "%s"', |
|
85 | 1 | implode(self::PROPERTY_TRAVERSAL_SEPARATOR, $propertyPathSteps) |
|
86 | ), |
||
87 | 1 | InvalidArgumentException::INVALID_PROPERTY_NAME |
|
88 | ); |
||
89 | } |
||
90 | |||
91 | 1 | $data =& $data[$property]; |
|
92 | } |
||
93 | |||
94 | 1 | return $data; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Translate a property name to a property path segments |
||
99 | * |
||
100 | * @param string $property Property name |
||
101 | * @return array Property path |
||
102 | * @throws InvalidArgumentException If the property name is empty |
||
103 | */ |
||
104 | 4 | protected function buildPropertyPath($property) |
|
115 | |||
116 | /** |
||
117 | * Set a property value |
||
118 | * |
||
119 | * Multi-level properties might be traversed by property name paths separated with colons (":"). |
||
120 | * |
||
121 | * @param string $property Property name |
||
122 | * @param mixed $value Property value |
||
123 | * @return GenericPropertiesInterface Self reference |
||
124 | */ |
||
125 | 2 | public function setProperty($property, $value) |
|
154 | } |
||
155 |