1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Linio\Collection; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\Criteria; |
8
|
|
|
use Doctrine\Common\Collections\Expr\ClosureExpressionVisitor; |
9
|
|
|
|
10
|
|
|
abstract class FixedTypedCollection extends TypedCollection |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var int |
14
|
|
|
*/ |
15
|
|
|
protected $size; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param int $size |
19
|
|
|
* @param array $elements |
20
|
|
|
*/ |
21
|
|
|
public function __construct($size, array $elements = []) |
22
|
|
|
{ |
23
|
|
|
$this->size = (int) $size; |
24
|
|
|
|
25
|
|
|
parent::__construct($elements); |
26
|
|
|
|
27
|
|
|
if ($this->count() > $this->size) { |
28
|
|
|
throw new \InvalidArgumentException('Index invalid or out of range'); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function offsetSet($offset, $value) |
36
|
|
|
{ |
37
|
|
|
$this->validateSize(); |
38
|
|
|
parent::offsetSet($offset, $value); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param int|string $key |
43
|
|
|
* |
44
|
|
|
* @throws \InvalidArgumentException |
45
|
|
|
* |
46
|
|
|
* @return mixed|null |
47
|
|
|
*/ |
48
|
|
|
public function get($key) |
49
|
|
|
{ |
50
|
|
|
if (!$this->containsKey($key)) { |
51
|
|
|
throw new \InvalidArgumentException('Index invalid or out of range'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return parent::get($key); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function offsetGet($offset) |
61
|
|
|
{ |
62
|
|
|
return $this->get($offset); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function add($value) |
69
|
|
|
{ |
70
|
|
|
$this->validateSize(); |
71
|
|
|
parent::add($value); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @throws \InvalidArgumentException |
76
|
|
|
*/ |
77
|
|
|
protected function validateSize() |
78
|
|
|
{ |
79
|
|
|
if ($this->count() >= $this->size) { |
80
|
|
|
throw new \InvalidArgumentException('Index invalid or out of range'); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function matching(Criteria $criteria) |
88
|
|
|
{ |
89
|
|
|
$expr = $criteria->getWhereExpression(); |
90
|
|
|
$filtered = $this->toArray(); |
91
|
|
|
|
92
|
|
|
if ($expr) { |
93
|
|
|
$visitor = new ClosureExpressionVisitor(); |
94
|
|
|
$filter = $visitor->dispatch($expr); |
95
|
|
|
$filtered = array_filter($filtered, $filter); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($orderings = $criteria->getOrderings()) { |
99
|
|
|
$next = null; |
100
|
|
|
foreach (array_reverse($orderings) as $field => $ordering) { |
101
|
|
|
$next = ClosureExpressionVisitor::sortByField($field, $ordering == 'DESC' ? -1 : 1, $next); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
usort($filtered, $next); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$offset = $criteria->getFirstResult(); |
108
|
|
|
$length = $criteria->getMaxResults(); |
109
|
|
|
|
110
|
|
|
if ($offset || $length) { |
|
|
|
|
111
|
|
|
$filtered = array_slice($filtered, (int) $offset, $length); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return new static(count($filtered), $filtered); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: