1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @filesource EnumerableTrait.php |
5
|
|
|
* @created 11.05.2017 |
6
|
|
|
* @package chillerlan\PrototypeDOM\Traits |
7
|
|
|
* @author Smiley <[email protected]> |
8
|
|
|
* @copyright 2017 Smiley |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace chillerlan\PrototypeDOM\Traits; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait EnumerableTrait |
16
|
|
|
*/ |
17
|
|
|
trait EnumerableTrait{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/each/ |
21
|
|
|
* |
22
|
|
|
* @param callable $callback |
23
|
|
|
* |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
1 |
|
public function each($callback){ |
27
|
1 |
|
$this->map($callback); |
28
|
|
|
|
29
|
1 |
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @link http://api.prototypejs.org/language/Array/prototype/first/ |
34
|
|
|
* |
35
|
|
|
* @return \chillerlan\PrototypeDOM\Node\Element|null |
36
|
|
|
*/ |
37
|
3 |
|
public function first(){ |
38
|
3 |
|
return $this->item(0); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @link http://api.prototypejs.org/language/Array/prototype/last/ |
43
|
|
|
* |
44
|
|
|
* @return \chillerlan\PrototypeDOM\Node\Element|null |
45
|
|
|
*/ |
46
|
1 |
|
public function last(){ |
47
|
1 |
|
return $this->item($this->count() - 1); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/collect/ |
52
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/map/ |
53
|
|
|
* |
54
|
|
|
* @param $callback |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
* @throws \Exception |
58
|
|
|
*/ |
59
|
3 |
|
public function map($callback):array { |
60
|
|
|
|
61
|
3 |
|
if(!is_callable($callback)){ |
62
|
|
|
throw new \Exception('invalid callback'); // @codeCoverageIgnore |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
$return = []; |
66
|
|
|
|
67
|
3 |
|
foreach($this->array as $index => $element){ |
|
|
|
|
68
|
3 |
|
$return[$index] = call_user_func_array($callback, [$element, $index]); |
69
|
|
|
} |
70
|
|
|
|
71
|
3 |
|
return $return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/pluck/ |
76
|
|
|
* |
77
|
|
|
* @param string $property |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
3 |
|
public function pluck(string $property):array { |
82
|
3 |
|
return array_column($this->array, $property); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @link http://api.prototypejs.org/language/Array/prototype/reverse/ |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
3 |
|
public function reverse(){ |
91
|
3 |
|
$this->array = array_reverse($this->array); |
92
|
3 |
|
$this->rewind(); |
|
|
|
|
93
|
|
|
|
94
|
3 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @link http://api.prototypejs.org/language/Enumerable/prototype/toArray/ |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
1 |
|
public function toArray():array { |
103
|
1 |
|
return $this->array; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.