Completed
Pull Request — master (#3)
by Cristiano
01:50
created

AbstractList::indexOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace phootwork\collection;
3
4
use phootwork\lang\Comparator;
5
6
/**
7
 * Abstract class for all list-like collections
8
 * 
9
 */
10
abstract class AbstractList extends AbstractCollection {
11
12
	/**
13
	 * Iterative reduction of this collection with the help of a callback function. The callback
14
	 * function takes two parameters, the first is the carry, the second the current item, with this
15
	 * signature: mixed callback(mixed $carry, mixed $item)
16
	 *
17
	 * @param callable $callback the callback function
18
	 * @param mixed $fallback the default value, that will be returned when the list is empty
19
	 * @return mixed
20
	 */
21 1
	public function reduce(callable $callback, $fallback = null) {
22 1
		return array_reduce($this->collection, $callback, $fallback);
23
	}
24
25
	/**
26
	 * Sorts the collection.
27
	 * 
28
	 * @param Comparator|callable $cmp
29
	 * @return $this
30
	 */
31 1
	public function sort($cmp = null) {
32 1
		$this->doSort($this->collection, $cmp, 'usort', 'sort');
33
34 1
		return $this;
35
	}
36
37
	/**
38
	 * Reverses the order of all elements
39
	 * 
40
	 * @return $this
41
	 */
42 1
	public function reverse() {
43 1
		$this->collection = array_reverse($this->collection);
44 1
		return $this;
45
	}
46
47
	/**
48
	 * Sorts the collection in reverse order
49
	 * 
50
	 * @see #sort
51
	 * @see #reverse
52
	 * @param Comparator|callable $cmp
53
	 * @return $this
54
	 */
55 1
	public function reverseSort($cmp = null) {
56 1
		return $this->sort($cmp)->reverse();
57
	}
58
	
59
	/**
60
	 * Iterates the collection and calls the callback function with the current item as parameter
61
	 * 
62
	 * @param callable $callback
63
	 */
64 1
	public function each(callable $callback) {
65 1
		foreach ($this->collection as $item) {
66 1
			$callback($item);
67
		}
68 1
	}
69
70
	/**
71
	 * Searches the collection with a given callback and returns the index for the first element if found.
72
	 *
73
	 * The callback function takes one or two parameters:
74
	 *
75
	 *     function ($element [, $query]) {}
76
	 *
77
	 * The callback must return a boolean
78
	 *
79
	 * @param mixed $query OPTIONAL the provided query
80
	 * @param callable $callback the callback function
81
	 * @return int|null the index or null if it hasn't been found
82
	 */
83 1
	public function findIndex() {
84 1
		if (func_num_args() == 1) {
85 1
			$callback = func_get_arg(0);
86
		} else {
87 1
			$query = func_get_arg(0);
88 1
			$callback = func_get_arg(1);
89
		}
90
		
91 1
		$index = func_num_args() == 1 ? $this->find($callback) : $this->find($query, $callback);
92 1
		if ($index !== null) {
93 1
			$index = $this->indexOf($index);
94
		}
95
		
96 1
		return $index;
97
	}
98
	
99
	/**
100
	 * Searches the collection with a given callback and returns the index for the last element if found.
101
	 *
102
	 * The callback function takes one or two parameters:
103
	 *
104
	 *     function ($element [, $query]) {}
105
	 *
106
	 * The callback must return a boolean
107
	 *
108
	 * @param mixed $query OPTIONAL the provided query
109
	 * @param callable $callback the callback function
110
	 * @return int|null the index or null if it hasn't been found
111
	 */
112 1
	public function findLastIndex() {
113 1
		if (func_num_args() == 1) {
114 1
			$callback = func_get_arg(0);
115
		} else {
116 1
			$query = func_get_arg(0);
117 1
			$callback = func_get_arg(1);
118
		}
119
		
120 1
		$index = func_num_args() == 1 ? $this->findLast($callback) : $this->findLast($query, $callback);
121 1
		if ($index !== null) {
122 1
			$index = $this->indexOf($index);
123
		}
124
	
125 1
		return $index;
126
	}
127
128
    /**
129
     * Returns the element at the given index (or nothing if the index isn't present)
130
     *
131
     * @param int $index
132
     * @return mixed
133
     */
134 5
    public function get($index) {
135 5
        if (isset($this->collection[$index])) {
136 5
            return $this->collection[$index];
137
        }
138 1
    }
139
140
    /**
141
     * Returns the index of the given element or FALSE if the element can't be found
142
     *
143
     * @param mixed $element
144
     * @return int the index for the given element
145
     */
146 3
    public function indexOf($element) {
147 3
        return array_search($element, $this->collection, true);
148
    }
149
150
}
151