Completed
Branch master (162e0f)
by Evan
02:29
created

Model::__get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
cc 3
eloc 6
nc 3
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\Query\QueryBuilder;
9
use Silk\Database\ActiveRecord;
10
use Illuminate\Support\Collection;
11
use Silk\Term\Exception\TermNotFoundException;
12
use Silk\Term\Exception\TaxonomyMismatchException;
13
14
/**
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 ActiveRecord
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
    use QueryBuilder;
45
46
    /**
47
     * Model Constructor.
48
     *
49
     * @param mixed $term  WP_Term to fill data from
50
     *
51
     * @throws TaxonomyMismatchException
52
     */
53
    public function __construct(WP_Term $term = null)
54
    {
55
        if (! $term) {
56
            $term = new WP_Term(new stdClass);
57
            $term->taxonomy = static::TAXONOMY;
58
        } elseif ($term->taxonomy != static::TAXONOMY) {
59
            throw new TaxonomyMismatchException();
60
        }
61
62
        $this->object = $term;
63
    }
64
65
    /**
66
     * Create a new instance from a WP_Term object.
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
    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 static::fromWpTerm($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
    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 static::fromWpTerm($term);
111
    }
112
113
    /**
114
     * Create a new instance from an array of attributes.
115
     *
116
     * @param  array  $attributes [description]
117
     *
118
     * @return static
119
     */
120
    public static function fromArray(array $attributes)
121
    {
122
        return new static(
123
            new WP_Term((object) $attributes)
124
        );
125
    }
126
127
    /**
128
     * Create a new term, and get the instance for it.
129
     *
130
     * @param  array $attributes  Term attributes
131
     *
132
     * @return static
133
     */
134
    public static function create(array $attributes = [])
135
    {
136
        return static::fromArray(
137
            Collection::make($attributes)
138
                ->except([static::ID_PROPERTY, 'term_taxonomy_id'])
139
                ->put('taxonomy', static::TAXONOMY)
140
                ->toArray()
141
        )->save();
142
    }
143
144
    /**
145
     * Check if this term exists in the database.
146
     *
147
     * @return boolean
148
     */
149
    public function exists()
150
    {
151
        return $this->id && ((bool) term_exists((int) $this->id, static::TAXONOMY));
152
    }
153
154
    /**
155
     * Check if this term exists in the database as the child of the given parent.
156
     *
157
     * @param  int|string|object  $parent  integer Parent term ID
158
     *                                     string  Parent term slug or name
159
     *                                     object  The parent term object/model.
160
     *
161
     * @return boolean                     True if the this term and the parent
162
     *                                     exist in the database, and the instance
163
     *                                     is a child of the given parent;
164
     *                                     otherwise false
165
     */
166
    public function isChildOf($parent)
167
    {
168
        if (isset($parent->term_id)) {
169
            $parent = $parent->term_id;
170
        }
171
172
        return (bool) term_exists((int) $this->id, static::TAXONOMY, $parent);
173
    }
174
175
    /**
176
     * Get the parent term instance.
177
     *
178
     * @return static
179
     */
180
    public function parent()
181
    {
182
        return static::fromID($this->object->parent);
183
    }
184
185
    /**
186
     * Get all ancestors of this term as a collection.
187
     *
188
     * @return Collection
189
     */
190
    public function ancestors()
191
    {
192
        return Collection::make(get_ancestors($this->id, static::TAXONOMY, 'taxonomy'))
193
            ->map(function ($term_ID) {
194
                return static::fromID($term_ID);
195
            });
196
    }
197
198
    /**
199
     * Get the Taxonomy model.
200
     *
201
     * @return Taxonomy
202
     */
203
    public function taxonomy()
204
    {
205
        return Taxonomy::make($this->taxonomy);
206
    }
207
208
    /**
209
     * Start a new query for terms of this type.
210
     *
211
     * @return TermQueryBuilder
212
     */
213
    public function newQuery()
214
    {
215
        return (new TermQueryBuilder)->setModel($this);
216
    }
217
218
    /**
219
     * Get the array of actions and their respective handler classes.
220
     *
221
     * @return array
222
     */
223
    protected function actionClasses()
224
    {
225
        return [
226
            'save'   => Action\TermSaver::class,
227
            'load'   => Action\TermLoader::class,
228
            'delete' => Action\TermDeleter::class,
229
        ];
230
    }
231
}
232