Completed
Push — master ( c23db2...9a0aab )
by Vitaliy
03:27
created

ObjectCollectionTrait::indexByProperty()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Nayjest\Collection\Extended;
4
5
use mp;
6
7
/**
8
 * Implementation of methods added in ObjectCollectionReadInterface.
9
 *
10
 * ObjectCollectionTrait requires implementation of \Nayjest\Collection\CollectionReadInterface.
11
 */
12
trait ObjectCollectionTrait
13
{
14
15
    /**
16
     * @param callable $callback
17
     * @param array|null $optionalArguments
18
     * @return static
19
     */
20
    abstract public function find(callable $callback, array $optionalArguments = null);
21
22
    /**
23
     * @param callable $callback
24
     * @param array|null $optionalArguments
25
     * @return static
26
     */
27
    abstract public function filter(callable $callback, array $optionalArguments = null);
28
29
    /**
30
     * @param callable $compareFunction
31
     * @return static
32
     */
33
    abstract public function sort(callable $compareFunction);
34
35
    /**
36
     * @param string $className
37
     *
38
     * @return mixed
39
     */
40
    public function findByType($className)
41
    {
42
        return $this->find('is_a', [$className]);
43
    }
44
45
    /**
46
     * @param string $className
47
     * @return static
48
     */
49
    public function filterByType($className)
50
    {
51
        return $this->filter('is_a', [$className]);
52
    }
53
54
    /**
55
     * @param string $propertyName
56
     * @param $value
57
     * @param bool $useGetters
58
     * @return static
59
     */
60 1
    public function filterByProperty($propertyName, $value, $useGetters = false)
61
    {
62 1
        return $this->filter(
63 1
            $this->getPropertyComparator($propertyName, $value, $useGetters)
64 1
        );
65
    }
66
67
    /**
68
     * @param string $propertyName
69
     * @param $value
70
     * @param bool $useGetters
71
     * @return mixed
72
     */
73 1
    public function findByProperty($propertyName, $value, $useGetters = false)
74
    {
75 1
        return $this->find(
76 1
            $this->getPropertyComparator($propertyName, $value, $useGetters)
77 1
        );
78
    }
79
80
    /**
81
     * @param string $propertyName
82
     * @return static
83
     */
84
    public function sortByProperty($propertyName)
85
    {
86
87
        return $this->sort(function ($itemA, $itemB) use ($propertyName) {
88
            $a = mp\getValue($itemA, $propertyName, 0);
89
            $b = mp\getValue($itemB, $propertyName, 0);
90
            if ($a < $b) {
91
                return -1;
92
            }
93
            return ($a > $b) ? 1 : 0;
94
        });
95
    }
96
97 1
    private function getPropertyComparator($propertyName, $value, $useGetters)
98
    {
99 1
        if ($useGetters) {
100
            return function ($item) use ($propertyName, $value) {
101
                // NAN used as default because NAN !== NAN
102 1
                return mp\getValue($item, $propertyName, NAN) === $value;
103 1
            };
104
        } else {
105 1
            return function ($item) use ($propertyName, $value) {
106 1
                return isset($item->{$propertyName}) && $item->{$propertyName} === $value;
107 1
            };
108
        }
109
    }
110
111
    /**
112
     * Returns array indexed by specified property of collection elements.
113
     * If there is few elements with same property value, last will be used.
114
     *
115
     * @param string $propertyName
116
     * @return array|object[]
117
     */
118
    public function indexByProperty($propertyName)
119
    {
120
        $results = [];
121
        foreach($this as $item) {
0 ignored issues
show
Bug introduced by
The expression $this of type this<Nayjest\Collection\...\ObjectCollectionTrait> is not traversable.
Loading history...
122
            $key = mp\getValue($item, $propertyName);
123
            if ($key) {
124
                $results[$key] = $item;
125
            }
126
        }
127
        return $results;
128
    }
129
}
130