|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Dot - PHP dot notation access to arrays |
|
5
|
|
|
* |
|
6
|
|
|
* @author Riku Särkinen <[email protected]> |
|
7
|
|
|
* @link https://github.com/adbario/php-dot-notation |
|
8
|
|
|
* @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Ballybran\Core\Collections\Collection; |
|
12
|
|
|
|
|
13
|
|
|
use Countable; |
|
14
|
|
|
use ArrayIterator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Dot |
|
18
|
|
|
* |
|
19
|
|
|
* This class provides a dot notation access and helper functions for |
|
20
|
|
|
* working with arrays of data. Inspired by Laravel Collection. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ValidateDot implements Countable |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Replace all items with a given array as a reference |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $items |
|
29
|
|
|
*/ |
|
30
|
|
|
public function setReference(array &$items) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->elements = &$items; |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Return all the stored items |
|
37
|
|
|
* |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
|
|
public function all() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->elements; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Checks if the given key exists in the provided array. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $array Array to validate |
|
50
|
|
|
* @param int|string $key The key to look for |
|
51
|
|
|
* |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
private function exists($array, $key) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
return array_key_exists($key, $array); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/* |
|
61
|
|
|
* -------------------------------------------------------------- |
|
62
|
|
|
* ArrayAccess interface |
|
63
|
|
|
* -------------------------------------------------------------- |
|
64
|
|
|
*/ |
|
65
|
|
|
/** |
|
66
|
|
|
* Check if a given key exists |
|
67
|
|
|
* |
|
68
|
|
|
* @param int|string $key |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
|
|
public function offsetExists($key) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->has($key); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Replace all items with a given array |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $items |
|
80
|
|
|
*/ |
|
81
|
|
|
public function setArray($items) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->elements = $this->getArrayItems($items); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/* |
|
87
|
|
|
* -------------------------------------------------------------- |
|
88
|
|
|
* Countable interface |
|
89
|
|
|
* -------------------------------------------------------------- |
|
90
|
|
|
*/ |
|
91
|
|
|
/** |
|
92
|
|
|
* Return the number of items in a given key |
|
93
|
|
|
* |
|
94
|
|
|
* @param int|string|null $key |
|
95
|
|
|
* @return int |
|
96
|
|
|
*/ |
|
97
|
|
|
public function count($key = null) |
|
98
|
|
|
{ |
|
99
|
|
|
return count($this->get($key)); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
} |