1 | <?php |
||
11 | class ObjectPath |
||
12 | { |
||
13 | /** |
||
14 | * The subject to match path expressions against. |
||
15 | * |
||
16 | * @var array|object |
||
17 | */ |
||
18 | protected $subject; |
||
19 | |||
20 | /** |
||
21 | * A pattern for matching array keys. |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | private static $arrayKey = '/\[([^\]]+)\]/'; |
||
26 | |||
27 | /** |
||
28 | * @param array|object $subject |
||
29 | */ |
||
30 | public function __construct($subject) |
||
34 | |||
35 | /** |
||
36 | * Returns an ObjectPathValue if the property described by $path |
||
37 | * can be located in the subject. |
||
38 | * |
||
39 | * A path expression uses object and array syntax. |
||
40 | * |
||
41 | * @code |
||
42 | * |
||
43 | * $person = new stdClass(); |
||
44 | * $person->name = new stdClass(); |
||
45 | * $person->name->first = 'brian'; |
||
46 | * $person->name->last = 'scaturro'; |
||
47 | * $person->hobbies = ['programming', 'reading', 'board games']; |
||
48 | * |
||
49 | * $path = new ObjectPath($person); |
||
50 | * $first = $path->get('name->first'); |
||
51 | * $reading = $path->get('hobbies[0]'); |
||
52 | * |
||
53 | * @endcode |
||
54 | * |
||
55 | * @param string $path |
||
56 | * @return ObjectPathValue |
||
57 | */ |
||
58 | public function get($path) |
||
77 | |||
78 | /** |
||
79 | * Breaks a path expression into an array used |
||
80 | * for navigating a path. |
||
81 | * |
||
82 | * @param $path |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getPathParts($path) |
||
94 | |||
95 | /** |
||
96 | * Returns a property as an array. |
||
97 | * |
||
98 | * @param $subject |
||
99 | * @return array |
||
100 | */ |
||
101 | protected function getPropertyCollection($subject) |
||
109 | |||
110 | /** |
||
111 | * Return a key that can be used on the current subject. |
||
112 | * |
||
113 | * @param $key |
||
114 | * @param $matches |
||
115 | * @return mixed |
||
116 | */ |
||
117 | protected function normalizeKey($key) |
||
127 | |||
128 | /** |
||
129 | * Given a key and a collection of properties, this method |
||
130 | * will return an ObjectPathValue if possible. |
||
131 | * |
||
132 | * @param $key |
||
133 | * @param $properties |
||
134 | * @return null|ObjectPathValue |
||
135 | */ |
||
136 | protected function getPathValue($key, $properties) |
||
144 | } |
||
145 |