Completed
Branch refactor/model-abstraction (ca5995)
by Evan
02:38
created

Model::actionClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
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\Exception\WP_ErrorException;
12
use Silk\Term\Exception\TermNotFoundException;
13
use Silk\Term\Exception\TaxonomyMismatchException;
14
15
/**
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 ActiveRecord
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
    use QueryBuilder;
46
47
    /**
48
     * Model Constructor.
49
     *
50
     * @param mixed $term  WP_Term to fill data from
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
     *
67
     * @param  WP_Term $term [description]
68
     *
69
     * @return static
70
     */
71
    public static function fromWpTerm(WP_Term $term)
72
    {
73
        return new static($term);
74
    }
75
76
    /**
77
     * Create a new instance from a term ID.
78
     *
79
     * @param  int|string $id  Term ID
80
     *
81
     * @return static
82
     */
83
    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...
84
    {
85
        if (! $term = get_term_by('id', (int) $id, static::TAXONOMY)) {
86
            throw new TermNotFoundException("No term found with ID $id.");
87
        }
88
89
        return static::fromWpTerm($term);
90
    }
91
92
    /**
93
     * Create a new instance from a slug.
94
     *
95
     * @param  string $slug  Term slug
96
     *
97
     * @return static
98
     */
99
    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...
100
    {
101
        if (! $term = get_term_by('slug', $slug, static::TAXONOMY)) {
102
            throw new TermNotFoundException("No term found with slug '$slug'.");
103
        }
104
105
        return static::fromWpTerm($term);
106
    }
107
108
    /**
109
     * Create a new instance from an array of attributes.
110
     *
111
     * @param  array  $attributes [description]
112
     *
113
     * @return static
114
     */
115
    public static function fromArray(array $attributes)
116
    {
117
        return new static(
118
            new WP_Term((object) $attributes)
119
        );
120
    }
121
122
    /**
123
     * Create a new term, and get the instance for it.
124
     *
125
     * @param  array $attributes  Term attributes
126
     *
127
     * @return static
128
     */
129
    public static function create(array $attributes = [])
130
    {
131
        return static::fromArray(
132
            Collection::make($attributes)
133
                ->except([static::ID_PROPERTY, 'term_taxonomy_id'])
134
                ->put('taxonomy', static::TAXONOMY)
135
                ->toArray()
136
        )->save();
137
    }
138
139
    /**
140
     * Check if this term exists in the database.
141
     *
142
     * @return boolean
143
     */
144
    public function exists()
145
    {
146
        return $this->id && ((bool) term_exists((int) $this->id, static::TAXONOMY));
147
    }
148
149
    /**
150
     * Check if this term exists in the database as the child of the given parent.
151
     *
152
     * @param  int|string|object  $parent  integer Parent term ID
153
     *                                     string  Parent term slug or name
154
     *                                     object  The parent term object/model.
155
     *
156
     * @return boolean                     True if the this term and the parent
157
     *                                     exist in the database, and the instance
158
     *                                     is a child of the given parent;
159
     *                                     otherwise false
160
     */
161
    public function isChildOf($parent)
162
    {
163
        if (isset($parent->term_id)) {
164
            $parent = $parent->term_id;
165
        }
166
167
        return (bool) term_exists((int) $this->id, static::TAXONOMY, $parent);
168
    }
169
170
    /**
171
     * Get the parent term instance.
172
     *
173
     * @return static
174
     */
175
    public function parent()
176
    {
177
        return static::fromID($this->object->parent);
178
    }
179
180
    /**
181
     * Get all ancestors of this term as a collection.
182
     *
183
     * @return Collection
184
     */
185
    public function ancestors()
186
    {
187
        return Collection::make(get_ancestors($this->id, static::TAXONOMY, 'taxonomy'))
188
            ->map(function ($term_ID) {
189
                return static::fromID($term_ID);
190
            });
191
    }
192
193
    /**
194
     * Get the Taxonomy model.
195
     *
196
     * @return Taxonomy
197
     */
198
    public function taxonomy()
199
    {
200
        return Taxonomy::make($this->taxonomy);
201
    }
202
203
    /**
204
     * Start a new query for terms of this type.
205
     *
206
     * @return TermQueryBuilder
207
     */
208
    public function newQuery()
209
    {
210
        return (new TermQueryBuilder)->setModel($this);
211
    }
212
213
    /**
214
     * Get the array of actions and their respective handler classes.
215
     *
216
     * @return array
217
     */
218
    protected function actionClasses()
219
    {
220
        return [
221
            'save'   => Action\TermSaver::class,
222
            'load'   => Action\TermLoader::class,
223
            'delete' => Action\TermDeleter::class,
224
        ];
225
    }
226
}
227