Completed
Pull Request — master (#20)
by Evan
05:00 queued 02:33
created

Model::typeId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 4
rs 10
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\Term\Exception\TermNotFoundException;
11
use Silk\Term\Exception\TaxonomyMismatchException;
12
13
/**
14
 * @property-read WP_Term $term
15
 * @property int    $term_id
16
 * @property string $name
17
 * @property string $slug
18
 * @property string $term_group
19
 * @property int    $term_taxonomy_id
20
 * @property string $taxonomy
21
 * @property string $description
22
 * @property int    $parent
23
 * @property int    $count
24
 */
25
abstract class Model extends BaseModel
26
{
27
    /**
28
     * The term's taxonomy
29
     * @var string
30
     */
31
    const TAXONOMY = '';
32
33
    /**
34
     * The object type in WordPress
35
     * @var string
36
     */
37
    const OBJECT_TYPE = 'term';
38
39
    /**
40
     * The primary ID property on the object
41
     */
42
    const ID_PROPERTY = 'term_id';
43
44
    /**
45
     * Model Constructor.
46
     *
47
     * @param mixed $term  WP_Term to fill data from
48
     *
49
     * @throws TaxonomyMismatchException
50
     */
51
    public function __construct(WP_Term $term = null)
52
    {
53
        if (! $term) {
54
            $term = new WP_Term(new stdClass);
55
            $term->taxonomy = static::TAXONOMY;
56
        } elseif ($term->taxonomy != static::TAXONOMY) {
57
            throw new TaxonomyMismatchException();
58
        }
59
60
        $this->object = $term;
61
    }
62
63
    /**
64
     * Create a new instance from a WP_Term object.
65
     *
66
     * @param  WP_Term $term [description]
67
     *
68
     * @return static
69
     */
70
    public static function fromWpTerm(WP_Term $term)
71
    {
72
        return new static($term);
73
    }
74
75
    /**
76
     * Create a new instance from a term ID.
77
     *
78
     * @param  int|string $id  Term ID
79
     *
80
     * @throws TermNotFoundException
81
     *
82
     * @return static
83
     */
84
    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...
85
    {
86
        if (! $term = get_term_by('id', (int) $id, static::TAXONOMY)) {
87
            throw new TermNotFoundException("No term found with ID $id.");
88
        }
89
90
        return static::fromWpTerm($term);
91
    }
92
93
    /**
94
     * Create a new instance from a slug.
95
     *
96
     * @param  string $slug  Term slug
97
     *
98
     * @throws TermNotFoundException
99
     *
100
     * @return static
101
     */
102
    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...
103
    {
104
        if (! $term = get_term_by('slug', $slug, static::TAXONOMY)) {
105
            throw new TermNotFoundException("No term found with slug '$slug'.");
106
        }
107
108
        return static::fromWpTerm($term);
109
    }
110
111
    /**
112
     * Create a new instance from an array of attributes.
113
     *
114
     * @param  array  $attributes [description]
115
     *
116
     * @return static
117
     */
118
    public static function fromArray(array $attributes)
119
    {
120
        return new static(
121
            new WP_Term((object) $attributes)
122
        );
123
    }
124
125
    /**
126
     * Create a new term, and get the instance for it.
127
     *
128
     * @param  array $attributes  Term attributes
129
     *
130
     * @return static
131
     */
132
    public static function create(array $attributes = [])
133
    {
134
        return static::fromArray(
135
            Collection::make($attributes)
136
                ->except([static::ID_PROPERTY, 'term_taxonomy_id'])
137
                ->put('taxonomy', static::TAXONOMY)
138
                ->toArray()
139
        )->save();
140
    }
141
142
    /**
143
     * Check if this term exists in the database.
144
     *
145
     * @return boolean
146
     */
147
    public function exists()
148
    {
149
        return $this->id && ((bool) term_exists((int) $this->id, static::TAXONOMY));
150
    }
151
152
    /**
153
     * Check if this term exists in the database as the child of the given parent.
154
     *
155
     * @param  int|string|object  $parent  integer Parent term ID
156
     *                                     string  Parent term slug or name
157
     *                                     object  The parent term object/model.
158
     *
159
     * @return boolean                     True if the this term and the parent
160
     *                                     exist in the database, and the instance
161
     *                                     is a child of the given parent;
162
     *                                     otherwise false
163
     */
164
    public function isChildOf($parent)
165
    {
166
        if (isset($parent->term_id)) {
167
            $parent = $parent->term_id;
168
        }
169
170
        return (bool) term_exists((int) $this->id, static::TAXONOMY, $parent);
171
    }
172
173
    /**
174
     * Get the parent term instance.
175
     *
176
     * @return static
177
     */
178
    public function parent()
179
    {
180
        return static::fromID($this->object->parent);
181
    }
182
183
    /**
184
     * Get all ancestors of this term as a collection.
185
     *
186
     * @return Collection
187
     */
188
    public function ancestors()
189
    {
190
        return Collection::make(get_ancestors($this->id, static::TAXONOMY, 'taxonomy'))
191
            ->map(function ($term_ID) {
192
                return static::fromID($term_ID);
193
            });
194
    }
195
196
    /**
197
     * Get the taxonomy identifier for the model.
198
     *
199
     * @return string
200
     */
201
    public static function typeId()
202
    {
203
        return static::taxonomy()->id;
204
    }
205
206
    /**
207
     * Get the Taxonomy model.
208
     *
209
     * @return Taxonomy|\Silk\Taxonomy\Builder
210
     */
211
    public static function taxonomy()
212
    {
213
        return Taxonomy::make(static::TAXONOMY);
214
    }
215
216
    /**
217
     * Start a new query for terms of this type.
218
     *
219
     * @return QueryBuilder
220
     */
221
    public function newQuery()
222
    {
223
        return (new QueryBuilder)->setModel($this);
224
    }
225
226
    /**
227
     * Get the array of actions and their respective handler classes.
228
     *
229
     * @return array
230
     */
231
    protected function actionClasses()
232
    {
233
        return [
234
            'save'   => Action\TermSaver::class,
235
            'load'   => Action\TermLoader::class,
236
            'delete' => Action\TermDeleter::class,
237
        ];
238
    }
239
}
240