|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Peridot\ObjectPath; |
| 4 | + |
| 5 | +/** |
| 6 | + * ObjectPath is a utility for parsing object and array strings into |
| 7 | + * ObjectPathValues. |
| 8 | + * |
| 9 | + * @package Peridot\ObjectPath |
| 10 | + */ |
| 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) |
| 31 | + { |
| 32 | + $this->subject = $subject; |
| 33 | + } |
| 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) |
| 59 | + { |
| 60 | + $parts = $this->getPathParts($path); |
| 61 | + $properties = $this->getPropertyCollection($this->subject); |
| 62 | + $pathValue = null; |
| 63 | + while ($properties && $parts) { |
| 64 | + $key = array_shift($parts); |
| 65 | + $key = $this->normalizeKey($key); |
| 66 | + $pathValue = array_key_exists($key, $properties) ? new ObjectPathValue($key, $properties[$key]) : null; |
| 67 | + |
| 68 | + if (! array_key_exists($key, $properties)) { |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + $properties = $this->getPropertyCollection($properties[$key]); |
| 73 | + } |
| 74 | + return $pathValue; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Breaks a path expression into an array used |
| 79 | + * for navigating a path. |
| 80 | + * |
| 81 | + * @param $path |
| 82 | + * @return array |
| 83 | + */ |
| 84 | + public function getPathParts($path) |
| 85 | + { |
| 86 | + $path = preg_replace('/\[/', '->[', $path); |
| 87 | + if (preg_match('/^->/', $path)) { |
| 88 | + $path = substr($path, 2); |
| 89 | + } |
| 90 | + |
| 91 | + return explode('->', $path); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Returns a property as an array. |
| 96 | + * |
| 97 | + * @param $subject |
| 98 | + * @return array |
| 99 | + */ |
| 100 | + protected function getPropertyCollection($subject) |
| 101 | + { |
| 102 | + if (is_object($subject)) { |
| 103 | + return get_object_vars($subject); |
| 104 | + } |
| 105 | + |
| 106 | + return $subject; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Return a key that can be used on the current subject. |
| 111 | + * |
| 112 | + * @param $key |
| 113 | + * @param $matches |
| 114 | + * @return mixed |
| 115 | + */ |
| 116 | + protected function normalizeKey($key) |
| 117 | + { |
| 118 | + if (preg_match(static::$arrayKey, $key, $matches)) { |
| 119 | + $key = $matches[1]; |
| 120 | + return $key; |
| 121 | + } |
| 122 | + return $key; |
| 123 | + } |
| 124 | +} |
0 commit comments