Completed
Push — master ( 7335c0...871235 )
by Bocharsky
02:44
created

TraversableTrait::previous()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 first()
22
    {
23 4
        return reset($this->elements);
24
    }
25
26
    /**
27
     * Sets the internal pointer of an array to its last element.
28
     *
29
     * @return mixed The value of the last array element, or false if the array is empty.
30
     *
31
     * @link http://php.net/manual/en/function.end.php
32
     */
33 4
    public function last()
34
    {
35 4
        return end($this->elements);
36
    }
37
38
    /**
39
     * Advances the internal array pointer of an array.
40
     *
41
     * @return mixed The array value in the next place that's pointed
42
     * to by the internal array pointer, or false if there are no more elements.
43
     *
44
     * @link http://php.net/manual/en/function.next.php
45
     */
46 4
    public function next()
47
    {
48 4
        return next($this->elements);
49
    }
50
51
    /**
52
     * Rewinds the internal array pointer.
53
     *
54
     * @return mixed The array value in the previous place that's pointed
55
     * to by the internal array pointer, or false if there are no more elements.
56
     *
57
     * @link http://php.net/manual/en/function.prev.php
58
     */
59 4
    public function previous()
60
    {
61 4
        return prev($this->elements);
62
    }
63
64
    /**
65
     * Fetch a key from an array.
66
     *
67
     * @return mixed The key function simply returns the key of the array element
68
     * that's currently being pointed to by the internal pointer. It does not move
69
     * the pointer in any way. If the internal pointer points beyond the end
70
     * of the elements list or the array is empty, key returns null.
71
     *
72
     * @link http://php.net/manual/en/function.key.php
73
     */
74 4
    public function key()
75
    {
76 4
        return key($this->elements);
77
    }
78
79
    /**
80
     * Returns the current element in an array.
81
     *
82
     * @return mixed The current function simply returns the value of the array element
83
     * that's currently being pointed to by the internal pointer. It does not move
84
     * the pointer in any way. If the internal pointer points beyond the end
85
     * of the elements list or the array is empty, current returns false.
86
     *
87
     * @link http://php.net/manual/en/function.current.php
88
     */
89 4
    public function current()
90
    {
91 4
        return current($this->elements);
92
    }
93
94
    /**
95
     * Returns the current key and value pair from an array and advance the array cursor.
96
     *
97
     * @return array The current key and value pair from the array array.
98
     * This pair is returned in a four-element array, with the keys 0, 1, key, and value.
99
     * Elements 0 and key contain the key name of the array element, and 1 and value contain the data.
100
     * If the internal pointer for the array points past the end of the array contents, each returns false.
101
     *
102
     * @link http://php.net/manual/en/function.each.php
103
     */
104 4
    public function each()
105
    {
106 4
        return each($this->elements);
107
    }
108
}
109