Test Failed
Push — master ( 6eea59...bfd094 )
by Chris
31:05
created

IdentifiableItemList::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace WebTheory\Collection\Access;
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 187
    public function __construct(
17
        string $property,
18
        PropertyResolverInterface $propertyResolver,
19
        ObjectComparatorInterface $objectComparator
20
    ) {
21 187
        $this->property = $property;
22 187
        $this->propertyResolver = $propertyResolver;
23 187
        $this->objectComparator = $objectComparator;
24
    }
25
26
    public function fetch(array $array, $item)
27
    {
28
        return $this->getFirstMatchingItem($array, $item);
29
    }
30
31 2
    public function maybeRemoveItem(array &$array, $item): bool
32
    {
33 2
        return ($item = $this->getFirstMatchingItem($array, $item))
34 2
            ? $this->remove($array, $item)
35 2
            : false;
36
    }
37
38 2
    protected function arrayContains(array $array, $item): bool
39
    {
40 2
        return $this->getQuery(...$this->query($item))->match($array);
41
    }
42
43 2
    protected function getFirstMatchingItem(array $array, $item): ?object
44
    {
45 2
        return $this->getQuery(...$this->query($item))->first($array);
46
    }
47
48 4
    protected function query($item): array
49
    {
50 4
        return [$this->property, '=', $item];
51
    }
52
53 4
    protected function getQuery(string $property, string $operator, $value): CollectionQueryInterface
54
    {
55 4
        return new BasicQuery(
56 4
            $property,
57 4
            $operator,
58 4
            $value,
59 4
            $this->propertyResolver,
60 4
        );
61
    }
62
}
63