mailpoet
/
vendor-prefixed
/
doctrine
/
collections
/
lib
/
Doctrine
/
Common
/
Collections
/
ArrayCollection.php
mailpoet
/
vendor-prefixed
/
doctrine
/
collections
/
lib
/
Doctrine
/
Common
/
Collections
Last commit date
Expr
9 months ago
AbstractLazyCollection.php
9 months ago
ArrayCollection.php
4 years ago
Collection.php
9 months ago
Criteria.php
9 months ago
ExpressionBuilder.php
4 years ago
ReadableCollection.php
9 months ago
Selectable.php
4 years ago
index.php
3 years ago
ArrayCollection.php
226 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\Common\Collections; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use ArrayIterator; |
| 5 | use Closure; |
| 6 | use MailPoetVendor\Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; |
| 7 | use ReturnTypeWillChange; |
| 8 | use Traversable; |
| 9 | use function array_filter; |
| 10 | use function array_key_exists; |
| 11 | use function array_keys; |
| 12 | use function array_map; |
| 13 | use function array_reverse; |
| 14 | use function array_search; |
| 15 | use function array_slice; |
| 16 | use function array_values; |
| 17 | use function count; |
| 18 | use function current; |
| 19 | use function end; |
| 20 | use function in_array; |
| 21 | use function key; |
| 22 | use function next; |
| 23 | use function reset; |
| 24 | use function spl_object_hash; |
| 25 | use function uasort; |
| 26 | use const ARRAY_FILTER_USE_BOTH; |
| 27 | class ArrayCollection implements Collection, Selectable |
| 28 | { |
| 29 | private $elements; |
| 30 | public function __construct(array $elements = []) |
| 31 | { |
| 32 | $this->elements = $elements; |
| 33 | } |
| 34 | public function toArray() |
| 35 | { |
| 36 | return $this->elements; |
| 37 | } |
| 38 | public function first() |
| 39 | { |
| 40 | return reset($this->elements); |
| 41 | } |
| 42 | protected function createFrom(array $elements) |
| 43 | { |
| 44 | return new static($elements); |
| 45 | } |
| 46 | public function last() |
| 47 | { |
| 48 | return end($this->elements); |
| 49 | } |
| 50 | public function key() |
| 51 | { |
| 52 | return key($this->elements); |
| 53 | } |
| 54 | public function next() |
| 55 | { |
| 56 | return next($this->elements); |
| 57 | } |
| 58 | public function current() |
| 59 | { |
| 60 | return current($this->elements); |
| 61 | } |
| 62 | public function remove($key) |
| 63 | { |
| 64 | if (!isset($this->elements[$key]) && !array_key_exists($key, $this->elements)) { |
| 65 | return null; |
| 66 | } |
| 67 | $removed = $this->elements[$key]; |
| 68 | unset($this->elements[$key]); |
| 69 | return $removed; |
| 70 | } |
| 71 | public function removeElement($element) |
| 72 | { |
| 73 | $key = array_search($element, $this->elements, \true); |
| 74 | if ($key === \false) { |
| 75 | return \false; |
| 76 | } |
| 77 | unset($this->elements[$key]); |
| 78 | return \true; |
| 79 | } |
| 80 | #[\ReturnTypeWillChange] |
| 81 | public function offsetExists($offset) |
| 82 | { |
| 83 | return $this->containsKey($offset); |
| 84 | } |
| 85 | #[\ReturnTypeWillChange] |
| 86 | public function offsetGet($offset) |
| 87 | { |
| 88 | return $this->get($offset); |
| 89 | } |
| 90 | #[\ReturnTypeWillChange] |
| 91 | public function offsetSet($offset, $value) |
| 92 | { |
| 93 | if (!isset($offset)) { |
| 94 | $this->add($value); |
| 95 | return; |
| 96 | } |
| 97 | $this->set($offset, $value); |
| 98 | } |
| 99 | #[\ReturnTypeWillChange] |
| 100 | public function offsetUnset($offset) |
| 101 | { |
| 102 | $this->remove($offset); |
| 103 | } |
| 104 | public function containsKey($key) |
| 105 | { |
| 106 | return isset($this->elements[$key]) || array_key_exists($key, $this->elements); |
| 107 | } |
| 108 | public function contains($element) |
| 109 | { |
| 110 | return in_array($element, $this->elements, \true); |
| 111 | } |
| 112 | public function exists(Closure $p) |
| 113 | { |
| 114 | foreach ($this->elements as $key => $element) { |
| 115 | if ($p($key, $element)) { |
| 116 | return \true; |
| 117 | } |
| 118 | } |
| 119 | return \false; |
| 120 | } |
| 121 | public function indexOf($element) |
| 122 | { |
| 123 | return array_search($element, $this->elements, \true); |
| 124 | } |
| 125 | public function get($key) |
| 126 | { |
| 127 | return $this->elements[$key] ?? null; |
| 128 | } |
| 129 | public function getKeys() |
| 130 | { |
| 131 | return array_keys($this->elements); |
| 132 | } |
| 133 | public function getValues() |
| 134 | { |
| 135 | return array_values($this->elements); |
| 136 | } |
| 137 | #[\ReturnTypeWillChange] |
| 138 | public function count() |
| 139 | { |
| 140 | return count($this->elements); |
| 141 | } |
| 142 | public function set($key, $value) |
| 143 | { |
| 144 | $this->elements[$key] = $value; |
| 145 | } |
| 146 | public function add($element) |
| 147 | { |
| 148 | $this->elements[] = $element; |
| 149 | return \true; |
| 150 | } |
| 151 | public function isEmpty() |
| 152 | { |
| 153 | return empty($this->elements); |
| 154 | } |
| 155 | #[\ReturnTypeWillChange] |
| 156 | public function getIterator() |
| 157 | { |
| 158 | return new ArrayIterator($this->elements); |
| 159 | } |
| 160 | public function map(Closure $func) |
| 161 | { |
| 162 | return $this->createFrom(array_map($func, $this->elements)); |
| 163 | } |
| 164 | public function filter(Closure $p) |
| 165 | { |
| 166 | return $this->createFrom(array_filter($this->elements, $p, ARRAY_FILTER_USE_BOTH)); |
| 167 | } |
| 168 | public function forAll(Closure $p) |
| 169 | { |
| 170 | foreach ($this->elements as $key => $element) { |
| 171 | if (!$p($key, $element)) { |
| 172 | return \false; |
| 173 | } |
| 174 | } |
| 175 | return \true; |
| 176 | } |
| 177 | public function partition(Closure $p) |
| 178 | { |
| 179 | $matches = $noMatches = []; |
| 180 | foreach ($this->elements as $key => $element) { |
| 181 | if ($p($key, $element)) { |
| 182 | $matches[$key] = $element; |
| 183 | } else { |
| 184 | $noMatches[$key] = $element; |
| 185 | } |
| 186 | } |
| 187 | return [$this->createFrom($matches), $this->createFrom($noMatches)]; |
| 188 | } |
| 189 | public function __toString() |
| 190 | { |
| 191 | return self::class . '@' . spl_object_hash($this); |
| 192 | } |
| 193 | public function clear() |
| 194 | { |
| 195 | $this->elements = []; |
| 196 | } |
| 197 | public function slice($offset, $length = null) |
| 198 | { |
| 199 | return array_slice($this->elements, $offset, $length, \true); |
| 200 | } |
| 201 | public function matching(Criteria $criteria) |
| 202 | { |
| 203 | $expr = $criteria->getWhereExpression(); |
| 204 | $filtered = $this->elements; |
| 205 | if ($expr) { |
| 206 | $visitor = new ClosureExpressionVisitor(); |
| 207 | $filter = $visitor->dispatch($expr); |
| 208 | $filtered = array_filter($filtered, $filter); |
| 209 | } |
| 210 | $orderings = $criteria->getOrderings(); |
| 211 | if ($orderings) { |
| 212 | $next = null; |
| 213 | foreach (array_reverse($orderings) as $field => $ordering) { |
| 214 | $next = ClosureExpressionVisitor::sortByField($field, $ordering === Criteria::DESC ? -1 : 1, $next); |
| 215 | } |
| 216 | uasort($filtered, $next); |
| 217 | } |
| 218 | $offset = $criteria->getFirstResult(); |
| 219 | $length = $criteria->getMaxResults(); |
| 220 | if ($offset || $length) { |
| 221 | $filtered = array_slice($filtered, (int) $offset, $length); |
| 222 | } |
| 223 | return $this->createFrom($filtered); |
| 224 | } |
| 225 | } |
| 226 |