Innmind /
Immutable
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Innmind\Immutable; |
||
| 4 | |||
| 5 | class Collection implements CollectionInterface |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 6 | { |
||
| 7 | private $values; |
||
| 8 | |||
| 9 | public function __construct(array $values) |
||
| 10 | { |
||
| 11 | $this->values = $values; |
||
| 12 | } |
||
| 13 | |||
| 14 | /** |
||
| 15 | * {@inheritdoc} |
||
| 16 | */ |
||
| 17 | public function toPrimitive() |
||
| 18 | { |
||
| 19 | return $this->values; |
||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * {@inheritdoc} |
||
| 24 | */ |
||
| 25 | public function filter(callable $filter = null) |
||
| 26 | { |
||
| 27 | if ($filter === null) { |
||
| 28 | $values = array_filter($this->values); |
||
| 29 | } else { |
||
| 30 | $values = array_filter($this->values, $filter); |
||
| 31 | } |
||
| 32 | |||
| 33 | return new self($values); |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | public function intersect(CollectionInterface $collection) |
||
| 40 | { |
||
| 41 | return new self(array_intersect( |
||
| 42 | $this->values, |
||
| 43 | $collection->toPrimitive() |
||
| 44 | )); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritdoc} |
||
| 49 | */ |
||
| 50 | public function chunk($size) |
||
| 51 | { |
||
| 52 | return new self(array_chunk($this->values, (int) $size)); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | public function shift() |
||
| 59 | { |
||
| 60 | $values = $this->values; |
||
| 61 | array_shift($values); |
||
| 62 | |||
| 63 | return new self($values); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function reduce(callable $reducer, $initial = null) |
||
| 70 | { |
||
| 71 | return array_reduce($this->values, $reducer, $initial); |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | public function search($needle, $strict = true) |
||
| 78 | { |
||
| 79 | return array_search($needle, $this->values, $strict); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function uintersect(CollectionInterface $collection, callable $intersecter) |
||
| 86 | { |
||
| 87 | return new self(array_uintersect( |
||
| 88 | $this->values, |
||
| 89 | $collection->toPrimitive(), |
||
| 90 | $intersecter |
||
| 91 | )); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * {@inheritdoc} |
||
| 96 | */ |
||
| 97 | public function keyIntersect(CollectionInterface $collection) |
||
| 98 | { |
||
| 99 | return new self(array_intersect_key( |
||
| 100 | $this->values, |
||
| 101 | $collection->toPrimitive() |
||
| 102 | )); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | public function map(callable $mapper) |
||
| 109 | { |
||
| 110 | return new self(array_map($mapper, $this->values)); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function pad($size, $value) |
||
| 117 | { |
||
| 118 | return new self(array_pad($this->values, (int) $size, $value)); |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | public function pop() |
||
| 125 | { |
||
| 126 | $values = $this->values; |
||
| 127 | array_pop($values); |
||
| 128 | |||
| 129 | return new self($values); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function sum() |
||
| 136 | { |
||
| 137 | return array_sum($this->values); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function diff(CollectionInterface $collection) |
||
| 144 | { |
||
| 145 | return new self(array_diff($this->values, $collection->toPrimitive())); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function flip() |
||
| 152 | { |
||
| 153 | return new self(array_flip($this->values)); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | public function keys($search = null, $strict = true) |
||
| 160 | { |
||
| 161 | return new self(array_keys($this->values, $search, $strict)); |
||
| 162 | } |
||
| 163 | } |
||
| 164 |