|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebTheory\Collection\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use WebTheory\Collection\Contracts\ArrayDriverInterface; |
|
6
|
|
|
use WebTheory\Collection\Contracts\CollectionQueryInterface; |
|
7
|
|
|
use WebTheory\Collection\Contracts\ObjectComparatorInterface; |
|
8
|
|
|
use WebTheory\Collection\Contracts\PropertyResolverInterface; |
|
9
|
|
|
use WebTheory\Collection\Query\BasicQuery; |
|
10
|
|
|
use WebTheory\Collection\Resolution\Abstracts\ResolvesPropertyValueTrait; |
|
11
|
|
|
|
|
12
|
|
|
class IdentifiableItemList extends StandardList implements ArrayDriverInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use ResolvesPropertyValueTrait; |
|
15
|
|
|
|
|
16
|
306 |
|
public function __construct( |
|
17
|
|
|
string $property, |
|
18
|
|
|
PropertyResolverInterface $propertyResolver, |
|
19
|
|
|
ObjectComparatorInterface $objectComparator |
|
20
|
|
|
) { |
|
21
|
306 |
|
$this->property = $property; |
|
22
|
306 |
|
$this->propertyResolver = $propertyResolver; |
|
23
|
306 |
|
$this->objectComparator = $objectComparator; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function fetch(array $array, $item) |
|
27
|
|
|
{ |
|
28
|
|
|
return $this->getFirstMatchingItem($array, $item); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
public function maybeRemoveItem(array &$array, $item): bool |
|
32
|
|
|
{ |
|
33
|
3 |
|
return ($item = $this->getFirstMatchingItem($array, $item)) |
|
34
|
3 |
|
? $this->remove($array, $item) |
|
35
|
3 |
|
: false; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
6 |
|
protected function arrayContains(array $array, $item): bool |
|
39
|
|
|
{ |
|
40
|
6 |
|
return !empty($this->getMatchingItems($array, $item)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
9 |
|
protected function getMatchingItems(array $array, $item): array |
|
44
|
|
|
{ |
|
45
|
9 |
|
return $this->getQuery($this->property, '=', $item)->query($array); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
3 |
|
protected function getFirstMatchingItem(array $array, $item): ?object |
|
49
|
|
|
{ |
|
50
|
3 |
|
return $this->getMatchingItems($array, $item)[0] ?? null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
9 |
|
protected function getQuery(string $property, string $operator, $value): CollectionQueryInterface |
|
54
|
|
|
{ |
|
55
|
9 |
|
return new BasicQuery( |
|
56
|
9 |
|
$this->propertyResolver, |
|
57
|
|
|
$property, |
|
58
|
|
|
$operator, |
|
59
|
|
|
$value, |
|
60
|
|
|
); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|