Completed
Push — develop ( 98c4f4...7da5a9 )
by Evan
02:19
created

Model::fromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
cc 1
eloc 3
nc 1
nop 1
rs 9.4285
1
<?php
2
3
namespace Silk\Term;
4
5
use stdClass;
6
use WP_Term;
7
use Silk\Taxonomy\Taxonomy;
8
use Silk\Type\Model as BaseModel;
9
use Illuminate\Support\Collection;
10
use Silk\Exception\WP_ErrorException;
11
use Silk\Term\Exception\TermNotFoundException;
12
use Silk\Term\Exception\TaxonomyMismatchException;
13
14
/**
15
 * @property-read WP_Term $term
16
 * @property int    $term_id
17
 * @property string $name
18
 * @property string $slug
19
 * @property string $term_group
20
 * @property int    $term_taxonomy_id
21
 * @property string $taxonomy
22
 * @property string $description
23
 * @property int    $parent
24
 * @property int    $count
25
 */
26
abstract class Model extends BaseModel
27
{
28
    /**
29
     * The term's taxonomy
30
     * @var string
31
     */
32
    const TAXONOMY = '';
33
34
    /**
35
     * The object type in WordPress
36
     * @var string
37
     */
38
    const OBJECT_TYPE = 'term';
39
40
    /**
41
     * The primary ID property on the object
42
     */
43
    const ID_PROPERTY = 'term_id';
44
45
    /**
46
     * Model Constructor.
47
     *
48
     * @param mixed $term  WP_Term to fill data from
49
     *
50
     * @throws TaxonomyMismatchException
51
     */
52
    public function __construct(WP_Term $term = null)
53
    {
54
        if (! $term) {
55
            $term = new WP_Term(new stdClass);
56
            $term->taxonomy = static::TAXONOMY;
57
        } elseif ($term->taxonomy != static::TAXONOMY) {
58
            throw new TaxonomyMismatchException();
59
        }
60
61
        $this->object = $term;
62
    }
63
64
    /**
65
     * Create a new instance from a WP_Term object.
66
     * @deprecated - use static::make()
67
     *
68
     * @param  WP_Term $term [description]
69
     *
70
     * @return static
71
     */
72
    public static function fromWpTerm(WP_Term $term)
73
    {
74
        return new static($term);
75
    }
76
77
    /**
78
     * Create a new instance from a term ID.
79
     *
80
     * @param  int|string $id  Term ID
81
     *
82
     * @throws TermNotFoundException
83
     *
84
     * @return static
85
     */
86 View Code Duplication
    public static function fromID($id)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        if (! $term = get_term_by('id', (int) $id, static::TAXONOMY)) {
89
            throw new TermNotFoundException("No term found with ID $id.");
90
        }
91
92
        return new static($term);
93
    }
94
95
    /**
96
     * Create a new instance from a slug.
97
     *
98
     * @param  string $slug  Term slug
99
     *
100
     * @throws TermNotFoundException
101
     *
102
     * @return static
103
     */
104 View Code Duplication
    public static function fromSlug($slug)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        if (! $term = get_term_by('slug', $slug, static::TAXONOMY)) {
107
            throw new TermNotFoundException("No term found with slug '$slug'.");
108
        }
109
110
        return new static($term);
111
    }
112
113
    /**
114
     * Check if this term exists in the database.
115
     *
116
     * @return boolean
117
     */
118
    public function exists()
119
    {
120
        return $this->id && ((bool) term_exists((int) $this->id, static::TAXONOMY));
121
    }
122
123
    /**
124
     * Check if this term exists in the database as the child of the given parent.
125
     *
126
     * @param  int|string|object  $parent  integer Parent term ID
127
     *                                     string  Parent term slug or name
128
     *                                     object  The parent term object/model.
129
     *
130
     * @return boolean                     True if the this term and the parent
131
     *                                     exist in the database, and the instance
132
     *                                     is a child of the given parent;
133
     *                                     otherwise false
134
     */
135
    public function isChildOf($parent)
136
    {
137
        if (isset($parent->term_id)) {
138
            $parent = $parent->term_id;
139
        }
140
141
        return (bool) term_exists((int) $this->id, static::TAXONOMY, $parent);
142
    }
143
144
    /**
145
     * Get the parent term instance.
146
     *
147
     * @return static
148
     */
149
    public function parent()
150
    {
151
        return static::fromID($this->object->parent);
152
    }
153
154
    /**
155
     * Get all ancestors of this term as a collection.
156
     *
157
     * @return Collection
158
     */
159
    public function ancestors()
160
    {
161
        return Collection::make(get_ancestors($this->id, static::TAXONOMY, 'taxonomy'))
162
            ->map(function ($term_ID) {
163
                return static::fromID($term_ID);
164
            });
165
    }
166
167
    /**
168
     * Get the Taxonomy model.
169
     *
170
     * @return Taxonomy|\Silk\Taxonomy\Builder
171
     */
172
    public static function taxonomy()
173
    {
174
        return Taxonomy::make(static::TAXONOMY);
175
    }
176
177
    /**
178
     * Get the URL for this term.
179
     *
180
     * @return string|bool
181
     */
182
    public function url()
183
    {
184
        $url = get_term_link($this->id, $this->taxonomy);
185
186
        if (is_wp_error($url)) {
187
            throw new WP_ErrorException($url);
188
        }
189
190
        return $url;
191
    }
192
193
    /**
194
     * Start a new query for terms of this type.
195
     *
196
     * @return QueryBuilder
197
     */
198
    public function newQuery()
199
    {
200
        return QueryBuilder::make()->setModel($this);
201
    }
202
203
    /**
204
     * Save the term to the database.
205
     *
206
     * @throws WP_ErrorException
207
     *
208
     * @return $this
209
     */
210
    public function save()
211
    {
212
        if ($this->id) {
213
            $ids = wp_update_term($this->id, $this->taxonomy, $this->object->to_array());
214
        } else {
215
            $ids = wp_insert_term($this->name, $this->taxonomy, $this->object->to_array());
216
        }
217
218
        if (is_wp_error($ids)) {
219
            throw new WP_ErrorException($ids);
220
        }
221
222
        $this->setId($ids['term_id'])->refresh();
223
224
        return $this;
225
    }
226
227
    /**
228
     * Delete the term from the database.
229
     *
230
     * @return $this
231
     */
232
    public function delete()
233
    {
234
        if (wp_delete_term($this->id, $this->taxonomy)) {
235
            $this->object = new WP_Term(new stdClass);
236
        }
237
238
        return $this;
239
    }
240
241
    /**
242
     * Reload the term object from the database.
243
     *
244
     * @return $this
245
     */
246
    public function refresh()
247
    {
248
        $this->object = WP_Term::get_instance($this->id, $this->taxonomy);
249
250
        return $this;
251
    }
252
}
253