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

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