jasny /
dotkey
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Jasny\DotKey; |
||
| 6 | |||
| 7 | /** |
||
| 8 | * Access objects and arrays through dot notation. |
||
| 9 | */ |
||
| 10 | class DotKey |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var object|array<string,mixed> |
||
| 14 | */ |
||
| 15 | protected $subject; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Class constructor. |
||
| 19 | * |
||
| 20 | * @param object|array $subject |
||
| 21 | */ |
||
| 22 | 109 | public function __construct(&$subject) |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 23 | { |
||
| 24 | 109 | if (!\is_object($subject) && !\is_array($subject)) { |
|
| 25 | 1 | $type = \gettype($subject); |
|
| 26 | 1 | throw new \InvalidArgumentException("Subject should be an array or object; $type given"); |
|
| 27 | } |
||
| 28 | |||
| 29 | 108 | $this->subject =& $subject; |
|
| 30 | 108 | } |
|
| 31 | |||
| 32 | /** |
||
| 33 | * Check if path exists in subject. |
||
| 34 | */ |
||
| 35 | 10 | public function exists(string $path, string $delimiter = '.'): bool |
|
| 36 | { |
||
| 37 | 10 | return Internal\Read::exists($this->subject, $path, $delimiter); |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Get a value from subject by path. |
||
| 42 | * |
||
| 43 | * @param string $path |
||
| 44 | * @param string $delimiter |
||
| 45 | * @return mixed |
||
| 46 | * @throws ResolveException |
||
| 47 | */ |
||
| 48 | 19 | public function get(string $path, string $delimiter = '.') |
|
| 49 | { |
||
| 50 | 19 | return Internal\Read::get($this->subject, $path, $delimiter); |
|
| 51 | } |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * Set a value within the subject by path. |
||
| 56 | * |
||
| 57 | * @param string $path |
||
| 58 | * @param mixed $value |
||
| 59 | * @param string $delimiter |
||
| 60 | * @throws ResolveException |
||
| 61 | */ |
||
| 62 | 23 | public function set(string $path, $value, string $delimiter = '.'): void |
|
| 63 | { |
||
| 64 | 23 | Internal\Write::set($this->subject, $path, $value, $delimiter); |
|
| 65 | 9 | } |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Set a value, creating a structure if needed. |
||
| 69 | * |
||
| 70 | * @param string $path |
||
| 71 | * @param mixed $value |
||
| 72 | * @param string $delimiter |
||
| 73 | * @param bool|null $assoc Create new structure as array. Omit to base upon subject type. |
||
| 74 | * @throws ResolveException |
||
| 75 | */ |
||
| 76 | 30 | public function put(string $path, $value, string $delimiter = '.', ?bool $assoc = null): void |
|
| 77 | { |
||
| 78 | 30 | $assoc ??= is_array($this->subject) || $this->subject instanceof \ArrayAccess; |
|
| 79 | |||
| 80 | 30 | Internal\Write::put($this->subject, $path, $value, $delimiter, $assoc); |
|
| 81 | 26 | } |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Get a particular value back from the config array |
||
| 85 | * |
||
| 86 | * @param string $path |
||
| 87 | * @param string $delimiter |
||
| 88 | */ |
||
| 89 | 26 | public function remove(string $path, string $delimiter = '.'): void |
|
| 90 | { |
||
| 91 | 26 | Internal\Remove::remove($this->subject, $path, $delimiter); |
|
| 92 | 17 | } |
|
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Factory method. |
||
| 97 | * |
||
| 98 | * @param object|array<string,mixed> $subject |
||
| 99 | * @return static |
||
| 100 | */ |
||
| 101 | 109 | public static function on(&$subject): self |
|
| 102 | { |
||
| 103 | 109 | return new static($subject); |
|
| 104 | } |
||
| 105 | } |
||
| 106 |