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

TermCollection::isDiff()   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 1
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\Term;
4
5
use WP_Term;
6
use WP_Term_Query;
7
8
class TermCollection
9
{
10
    /**
11
     * @var WP_Term[]
12
     */
13
    protected $terms;
14
15
    /**
16
     *
17
     */
18
    public function __construct(WP_Term ...$terms)
19
    {
20
        $this->terms = $terms;
21
    }
22
23
    /**
24
     * @return WP_Term[]
25
     */
26
    public function getTerms(): array
27
    {
28
        return $this->terms;
29
    }
30
31
    /**
32
     *
33
     */
34
    public function get(string $property)
35
    {
36
        return array_map(function (WP_Term $term) use ($property) {
37
            return $term->{$property};
38
        }, $this->terms);
39
    }
40
41
    /**
42
     *
43
     */
44
    public function getIds()
45
    {
46
        return $this->get('term_id');
47
    }
48
49
    /**
50
     *
51
     */
52
    public function getNames()
53
    {
54
        return $this->get('name');
55
    }
56
57
    /**
58
     *
59
     */
60
    public function getSlugs()
61
    {
62
        return $this->get('slug');
63
    }
64
65
    /**
66
     *
67
     */
68
    public function append(WP_Term $term)
69
    {
70
        $this->terms[] = $term;
71
    }
72
73
    /**
74
     *
75
     */
76
    public function isEmpty(): bool
77
    {
78
        return empty($this->terms);
79
    }
80
81
    /**
82
     *
83
     */
84
    protected function diffCallback()
85
    {
86
        return function (WP_Term $term1, WP_Term $term2) {
87
            return $term1->term_id - $term2->term_id;
88
        };
89
    }
90
91
    /**
92
     *
93
     */
94
    public function without(TermCollection $collection)
95
    {
96
        return array_udiff(
97
            $this->getTerms(),
98
            $collection->getTerms(),
99
            $this->diffCallback()
100
        );
101
    }
102
103
    /**
104
     *
105
     */
106
    public function notIn(TermCollection $collection)
107
    {
108
        return array_udiff(
109
            $collection->getTerms(),
110
            $this->getTerms(),
111
            $this->diffCallback()
112
        );
113
    }
114
115
    /**
116
     *
117
     */
118
    public function diff(TermCollection $collection): array
119
    {
120
        $primary = $this->getTerms();
121
        $secondary = $collection->getTerms();
122
        $cb = $this->diffCallback();
123
124
        /*
125
         * if both primary and secondary are empty this will return false
126
         * because the "array_diff" family of functions returns an empty array
127
         * if the first array provided is empty itself. if both arrays are
128
         * empty this will return an empty array as there is no difference.
129
         */
130
        return !empty($primary)
131
            ? array_udiff($primary, $secondary, $cb)
132
            : array_udiff($secondary, $primary, $cb);
133
    }
134
135
    /**
136
     *
137
     */
138
    public function isDiff(TermCollection $collection): bool
139
    {
140
        return (bool) $this->diff($collection);
141
    }
142
143
    /**
144
     *
145
     */
146
    public static function fromQuery(WP_Term_Query $query): TermCollection
147
    {
148
        return new static(...$query->get_terms());
149
    }
150
151
    /**
152
     *
153
     */
154
    public static function create(array $args): TermCollection
155
    {
156
        return static::fromQuery(new WP_Term_Query($args));
157
    }
158
159
    /**
160
     *
161
     */
162
    public static function fromIds(int ...$ids): TermCollection
163
    {
164
        return static::create([
165
            'include' => $ids,
166
            'hide_empty' => false
167
        ]);
168
    }
169
}
170