1 | <?php |
||
14 | class Map implements JsonSerializable { |
||
|
|||
15 | |||
16 | protected $fields = []; |
||
17 | |||
18 | /** |
||
19 | * Returns the map as an associative array |
||
20 | * @return array reference |
||
21 | */ |
||
22 | public function & all(){ |
||
25 | |||
26 | /** |
||
27 | * Get a value assigned to a key path from the map |
||
28 | * @param string $key The key path of the value in dot notation |
||
29 | * @param mixed $default (optional) The default value. If is a callable it will executed and the return value will be used. |
||
30 | * @return mixed The value of the key or the default (resolved) value if the key not existed. |
||
31 | */ |
||
32 | public function get($key, $default=null){ |
||
33 | if (null !== ($ptr =& $this->find($key,false))){ |
||
34 | return $ptr; |
||
35 | } else { |
||
36 | if ($default !== null){ |
||
37 | return $this->set($key, is_callable($default) ? call_user_func($default) : $default); |
||
38 | } else { |
||
39 | return null; |
||
40 | } |
||
41 | } |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Set a value for a key path from map |
||
46 | * @param string $key The key path of the value in dot notation |
||
47 | * @param mixed $value (optional) The value. If is a callable it will executed and the return value will be used. |
||
48 | * @return mixed The value of the key or the default (resolved) value if the key not existed. |
||
49 | */ |
||
50 | public function set($key, $value=null){ |
||
51 | if (is_array($key)) { |
||
52 | return $this->merge($key); |
||
53 | } else { |
||
54 | $ptr =& $this->find($key, true); |
||
55 | return $ptr = $value; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Delete a value and the key path from map. |
||
61 | * @param string $key The key path in dot notation |
||
62 | * @param boolean $compact (optional) Compact map removing empty paths. |
||
63 | */ |
||
64 | public function delete($key, $compact=true){ |
||
65 | $this->set($key, null); |
||
66 | if ($compact) $this->compact(); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Check if a key path exists in map. |
||
71 | * @param string $key The key path in dot notation |
||
72 | * @return boolean |
||
73 | */ |
||
74 | public function exists($key){ |
||
77 | |||
78 | /** |
||
79 | * Clear all key path in map. |
||
80 | */ |
||
81 | public function clear(){ |
||
84 | |||
85 | public function __construct($fields=null){ |
||
86 | $this->load($fields); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Load an associative array/object as the map source. |
||
91 | * @param string $fields The array to merge |
||
92 | */ |
||
93 | public function load($fields){ |
||
96 | |||
97 | /** |
||
98 | * Merge an associative array to the map. |
||
99 | * @param array $array The array to merge |
||
100 | * @param boolean $merge_back If `true` merge the map over the $array, if `false` (default) the reverse. |
||
101 | */ |
||
102 | public function merge($array, $merge_back=false){ |
||
107 | |||
108 | /** |
||
109 | * Compact map removing empty paths |
||
110 | */ |
||
111 | |||
112 | public function compact(){ |
||
125 | |||
126 | /** |
||
127 | * Navigate map and find the element from the path in dot notation. |
||
128 | * @param string $path Key path in dot notation. |
||
129 | * @param boolean $create If true will create empty paths. |
||
130 | * @param callable If passed this callback will be applied to the founded value. |
||
131 | * @return mixed The founded value. |
||
132 | */ |
||
133 | public function & find($path, $create=false, callable $operation=null) { |
||
143 | |||
144 | /** |
||
145 | * JsonSerializable Interface handler |
||
146 | * |
||
147 | * @method jsonSerialize |
||
148 | * |
||
149 | * @return string The json object |
||
150 | */ |
||
151 | public function jsonSerialize(){ |
||
154 | |||
155 | |||
156 | } |
||
157 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.