ObjectCollectionTrait::toArray()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
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
    abstract public function toArray();
30
31
    /**
32
     * @param callable $compareFunction
33
     * @return static
34
     */
35
    abstract public function sort(callable $compareFunction);
36
37
    /**
38
     * @param string $className
39
     *
40
     * @return mixed
41
     */
42
    public function findByType($className)
43
    {
44
        return $this->find('is_a', [$className]);
45
    }
46
47
    /**
48
     * @param string $className
49
     * @return static
50
     */
51
    public function filterByType($className)
52
    {
53
        return $this->filter('is_a', [$className]);
54
    }
55
56
    /**
57
     * @param string $propertyName
58
     * @param $value
59
     * @param bool $useGetters
60
     * @return static
61
     */
62 1
    public function filterByProperty($propertyName, $value, $useGetters = false)
63
    {
64 1
        return $this->filter(
65 1
            $this->getPropertyComparator($propertyName, $value, $useGetters)
66 1
        );
67
    }
68
69
    /**
70
     * @param string $propertyName
71
     * @param $value
72
     * @param bool $useGetters
73
     * @return mixed
74
     */
75 1
    public function findByProperty($propertyName, $value, $useGetters = false)
76
    {
77 1
        return $this->find(
78 1
            $this->getPropertyComparator($propertyName, $value, $useGetters)
79 1
        );
80
    }
81
82
    /**
83
     * @param string $propertyName
84
     * @return static
85
     */
86
    public function sortByProperty($propertyName)
87
    {
88
89
        return $this->sort(function ($itemA, $itemB) use ($propertyName) {
90
            $a = mp\getValue($itemA, $propertyName, 0);
91
            $b = mp\getValue($itemB, $propertyName, 0);
92
            if ($a < $b) {
93
                return -1;
94
            }
95
            return ($a > $b) ? 1 : 0;
96
        });
97
    }
98
99 1
    private function getPropertyComparator($propertyName, $value, $useGetters)
100
    {
101 1
        if ($useGetters) {
102
            return function ($item) use ($propertyName, $value) {
103
                // NAN used as default because NAN !== NAN
104 1
                return mp\getValue($item, $propertyName, NAN) === $value;
105 1
            };
106
        } else {
107 1
            return function ($item) use ($propertyName, $value) {
108 1
                return isset($item->{$propertyName}) && $item->{$propertyName} === $value;
109 1
            };
110
        }
111
    }
112
113
    /**
114
     * Returns array indexed by specified property of collection elements.
115
     * If there is few elements with same property value, last will be used.
116
     *
117
     * @param string $propertyName
118
     * @return array|object[]
119
     */
120
    public function indexByProperty($propertyName)
121
    {
122
        $results = [];
123
        foreach ($this->toArray() as $item) {
124
            $key = mp\getValue($item, $propertyName);
125
            if ($key) {
126
                $results[$key] = $item;
127
            }
128
        }
129
        return $results;
130
    }
131
}
132