1 | <?php |
||
22 | trait PairStackable |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var \PhpCollection\Map |
||
27 | */ |
||
28 | protected $collection; |
||
29 | |||
30 | /** |
||
31 | * @return mixed|null |
||
32 | */ |
||
33 | public function first() |
||
34 | { |
||
35 | $first = $this->collection->first(); |
||
36 | return $this->returnValue($first); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return mixed|null |
||
41 | */ |
||
42 | public function last() |
||
43 | { |
||
44 | $last = $this->collection->last(); |
||
45 | return $this->returnValue($last); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param Option $value |
||
50 | * @return mixed|null |
||
51 | */ |
||
52 | private function returnValue(Option $value) |
||
53 | { |
||
54 | if ($value->isEmpty()) { |
||
55 | return null; |
||
56 | } |
||
57 | $kv = $value->get(); |
||
58 | |||
59 | return array_pop($kv); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return array |
||
64 | */ |
||
65 | public function toArray() |
||
69 | |||
70 | /** |
||
71 | * @param AbstractMap $collection |
||
72 | * @return array |
||
73 | */ |
||
74 | protected function createArray(AbstractMap $collection) |
||
75 | { |
||
76 | $keys = $collection->keys(); |
||
77 | $values = $collection->values(); |
||
78 | |||
79 | return array_combine($keys, $values); |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return int |
||
84 | */ |
||
85 | public function isEmpty() |
||
89 | |||
90 | /** |
||
91 | * @return int |
||
92 | */ |
||
93 | public function count() |
||
97 | |||
98 | /** |
||
99 | * @return \ArrayIterator |
||
100 | */ |
||
101 | public function getIterator() |
||
105 | |||
106 | } |
||
107 |