1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arrayzy\Traits; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Trait with helpful methods for traversing array. |
7
|
|
|
* |
8
|
|
|
* @author Victor Bocharsky <[email protected]> |
9
|
|
|
* |
10
|
|
|
* @property array $elements |
11
|
|
|
*/ |
12
|
|
|
trait TraversableTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Sets the internal pointer of an array to its first element. |
16
|
|
|
* |
17
|
|
|
* @return mixed The value of the first array element, or false if the array is empty. |
18
|
|
|
* |
19
|
|
|
* @link http://php.net/manual/en/function.reset.php |
20
|
|
|
*/ |
21
|
4 |
|
public function reset() |
22
|
|
|
{ |
23
|
4 |
|
return reset($this->elements); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Alias of reset() method. Sets the internal pointer of an array to its first element. |
28
|
|
|
* |
29
|
|
|
* @return mixed The value of the first array element, or false if the array is empty. |
30
|
|
|
* |
31
|
|
|
* @link http://php.net/manual/en/function.reset.php |
32
|
|
|
*/ |
33
|
4 |
|
public function first() |
34
|
|
|
{ |
35
|
4 |
|
return $this->reset(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Sets the internal pointer of an array to its last element. |
40
|
|
|
* |
41
|
|
|
* @return mixed The value of the last array element, or false if the array is empty. |
42
|
|
|
* |
43
|
|
|
* @link http://php.net/manual/en/function.end.php |
44
|
|
|
*/ |
45
|
4 |
|
public function end() |
46
|
|
|
{ |
47
|
4 |
|
return end($this->elements); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Alias of end() method. Sets the internal pointer of an array to its last element. |
52
|
|
|
* |
53
|
|
|
* @return mixed The value of the last array element, or false if the array is empty. |
54
|
|
|
* |
55
|
|
|
* @link http://php.net/manual/en/function.end.php |
56
|
|
|
*/ |
57
|
4 |
|
public function last() |
58
|
|
|
{ |
59
|
4 |
|
return $this->end(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Advances the internal array pointer of an array. |
64
|
|
|
* |
65
|
|
|
* @return mixed The array value in the next place that's pointed |
66
|
|
|
* to by the internal array pointer, or false if there are no more elements. |
67
|
|
|
* |
68
|
|
|
* @link http://php.net/manual/en/function.next.php |
69
|
|
|
*/ |
70
|
4 |
|
public function next() |
71
|
|
|
{ |
72
|
4 |
|
return next($this->elements); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Rewinds the internal array pointer. |
77
|
|
|
* |
78
|
|
|
* @return mixed The array value in the previous place that's pointed |
79
|
|
|
* to by the internal array pointer, or false if there are no more elements. |
80
|
|
|
* |
81
|
|
|
* @link http://php.net/manual/en/function.prev.php |
82
|
|
|
*/ |
83
|
4 |
|
public function previous() |
84
|
|
|
{ |
85
|
4 |
|
return prev($this->elements); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Fetch a key from an array. |
90
|
|
|
* |
91
|
|
|
* @return mixed The key function simply returns the key of the array element |
92
|
|
|
* that's currently being pointed to by the internal pointer. It does not move |
93
|
|
|
* the pointer in any way. If the internal pointer points beyond the end |
94
|
|
|
* of the elements list or the array is empty, key returns null. |
95
|
|
|
* |
96
|
|
|
* @link http://php.net/manual/en/function.key.php |
97
|
|
|
*/ |
98
|
4 |
|
public function key() |
99
|
|
|
{ |
100
|
4 |
|
return key($this->elements); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the current element in an array. |
105
|
|
|
* |
106
|
|
|
* @return mixed The current function simply returns the value of the array element |
107
|
|
|
* that's currently being pointed to by the internal pointer. It does not move |
108
|
|
|
* the pointer in any way. If the internal pointer points beyond the end |
109
|
|
|
* of the elements list or the array is empty, current returns false. |
110
|
|
|
* |
111
|
|
|
* @link http://php.net/manual/en/function.current.php |
112
|
|
|
*/ |
113
|
4 |
|
public function current() |
114
|
|
|
{ |
115
|
4 |
|
return current($this->elements); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns the current key and value pair from an array and advance the array cursor. |
120
|
|
|
* |
121
|
|
|
* @return array The current key and value pair from the array array. |
122
|
|
|
* This pair is returned in a four-element array, with the keys 0, 1, key, and value. |
123
|
|
|
* Elements 0 and key contain the key name of the array element, and 1 and value contain the data. |
124
|
|
|
* If the internal pointer for the array points past the end of the array contents, each returns false. |
125
|
|
|
* |
126
|
|
|
* @link http://php.net/manual/en/function.each.php |
127
|
|
|
*/ |
128
|
4 |
|
public function each() |
129
|
|
|
{ |
130
|
4 |
|
return each($this->elements); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|