Passed
Branch master (350ff0)
by Chris
18:33
created

AbstractWpModelCollection::getIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\Core\Models;
4
5
abstract class AbstractWpModelCollection
6
{
7
    /**
8
     *
9
     */
10
    protected const ID_KEY = '';
11
12
    /**
13
     *
14
     */
15
    protected const NAME_KEY = '';
16
17
    /**
18
     *
19
     */
20
    protected const SLUG_KEY = '';
21
22
    /**
23
     *
24
     */
25
    protected const OBJECT_TYPE = '';
26
27
    /**
28
     *
29
     */
30
    protected const COLLECTION = '';
31
32
    /**
33
     *
34
     */
35
    public function getCollection(): array
36
    {
37
        return $this->{static::COLLECTION};
38
    }
39
40
    /**
41
     *
42
     */
43
    public function get(string $property): array
44
    {
45
        return array_map(function ($object) use ($property) {
46
            return $object->{$property};
47
        }, $this->{static::COLLECTION});
48
    }
49
50
    /**
51
     *
52
     */
53
    public function getIds(): array
54
    {
55
        return $this->get(static::ID_KEY);
56
    }
57
58
    /**
59
     *
60
     */
61
    public function getNames(): array
62
    {
63
        return $this->get(static::NAME_KEY);
64
    }
65
66
    /**
67
     *
68
     */
69
    public function getSlugs(): array
70
    {
71
        return $this->get(static::SLUG_KEY);
72
    }
73
74
    /**
75
     *
76
     */
77
    public function getMeta(string $metaKey): array
78
    {
79
        $meta = [];
80
81
        foreach ($this->{static::COLLECTION} as $object) {
82
            $id = $object->{static::ID_KEY};
83
84
            $meta[$id] = $this->getObjectMetadata($object, $metaKey);
85
        }
86
87
        return $meta;
88
    }
89
90
    /**
91
     *
92
     */
93
    public function isEmpty(): bool
94
    {
95
        return empty($this->{static::COLLECTION});
96
    }
97
98
    /**
99
     *
100
     */
101
    public function sortByMeta(string $metaKey)
102
    {
103
        $orderArray = [];
104
105
        $collection = $this->{static::COLLECTION};
106
        $id = static::ID_KEY;
107
108
        foreach ($collection as $item) {
109
            $orderArray[$item->{$id}] = $this->getObjectMetadata($item, $metaKey);
110
        }
111
112
        usort($collection, $this->sortByMetaCallback($orderArray));
113
114
        return $collection;
115
    }
116
117
    /**
118
     *
119
     */
120
    public function without(AbstractWpModelCollection $collection)
121
    {
122
        return array_udiff(
123
            $this->getCollection(),
124
            $collection->getCollection(),
125
            $this->diffCallback()
126
        );
127
    }
128
129
    /**
130
     *
131
     */
132
    protected function getObjectMetadata(object $object, string $metaKey)
133
    {
134
        return get_metadata(static::OBJECT_TYPE, $object->{static::ID_KEY}, $metaKey, true);
135
    }
136
137
    /**
138
     *
139
     */
140
    protected function diffCallback()
141
    {
142
        return function ($object1, $object2) {
143
            return $object1->{static::ID_KEY} - $object2->{static::ID_KEY};
144
        };
145
    }
146
147
    /**
148
     * @param array $orderArray Associative array with object ids as keys and desired order as values
149
     *
150
     * @return callable
151
     */
152
    protected function sortByMetaCallback(array $orderArray): callable
153
    {
154
        return function ($a, $b) use ($orderArray) {
155
            // Set value to 0 if one is not provided
156
            $a = (int) $orderArray[$a->{static::ID_KEY}] ?? 0;
157
            $b = (int) $orderArray[$b->{static::ID_KEY}] ?? 0;
158
159
            if ($a === $b) {
160
                return 0;
161
            }
162
163
            if ($a < $b && $a === 0) {
164
                return 1;
165
            }
166
167
            if ($a > $b && $b === 0) {
168
                return -1;
169
            }
170
171
            return $a > $b ? 1 : -1;
172
        };
173
    }
174
}
175