Completed
Pull Request — master (#20)
by Evan
02:36
created

Taxonomy::terms()   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
c 1
b 0
f 1
dl 0
loc 4
cc 1
eloc 2
nc 1
nop 0
rs 10
1
<?php
2
3
namespace Silk\Taxonomy;
4
5
use Silk\Type\Type;
0 ignored issues
show
introduced by
Use classes must be in alphabetical order.
Loading history...
6
use Silk\PostType\PostType;
7
use Silk\Term\QueryBuilder;
8
use Illuminate\Support\Collection;
9
use Silk\Exception\WP_ErrorException;
10
use Silk\Taxonomy\Exception\InvalidTaxonomyNameException;
11
use Silk\Taxonomy\Exception\NonExistentTaxonomyException;
12
13
/**
14
 * @property-read bool     $_builtin
15
 * @property-read stdClass $cap
16
 * @property-read string   $description
17
 * @property-read bool     $hierarchical
18
 * @property-read string   $label
19
 * @property-read stdClass $labels
20
 * @property-read callable $meta_box_cb
21
 * @property-read string   $name
22
 * @property-read array    $object_type
23
 * @property-read bool     $public
24
 * @property-read bool     $publicly_queryable
25
 * @property-read string   $query_var
26
 * @property-read array    $rewrite
27
 * @property-read bool     $show_admin_column
28
 * @property-read bool     $show_in_menu
29
 * @property-read bool     $show_in_nav_menus
30
 * @property-read bool     $show_in_quick_edit
31
 * @property-read bool     $show_tagcloud
32
 * @property-read bool     $show_ui
33
 * @property-read callable $update_count_callback
34
 */
35
class Taxonomy extends Type
36
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
37
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
38
     * Taxonomy Constructor.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
39
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
40
     * @param object $taxonomy The taxonomy object
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
41
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
42
     * @throws NonExistentTaxonomyException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
NonExistentTaxonomyException => \Silk\Taxonomy\Exception\NonExistentTaxonomyException
Loading history...
43
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
44
    public function __construct($taxonomy)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
45
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
46
        if (empty($taxonomy->name) || ! static::exists($taxonomy->name)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
47
            throw new NonExistentTaxonomyException;
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Calling class constructors must always include parentheses
Loading history...
48
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
49
50
        $this->object = $taxonomy;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
51
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
52
53
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
54
     * Create a new instance using the taxonomy identifier.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
55
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
56
     * @param  string $identifier Taxonomy name/identifier
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
57
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
58
     * @throws InvalidTaxonomyNameException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
InvalidTaxonomyNameException => \Silk\Taxonomy\Exception\InvalidTaxonomyNameException
Loading history...
59
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
60
     * @return static|Builder
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Invalid class name "Builder"
Loading history...
61
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
62
    public static function make($identifier)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
63
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
64
        if (static::exists($identifier)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
65
            return static::load($identifier);
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
66
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
67
68
        if (! $identifier || strlen($identifier) > 32) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
69
            throw new InvalidTaxonomyNameException('Taxonomy names must be between 1 and 32 characters in length.');
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
70
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
71
72
        return new Builder($identifier);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
73
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
74
75
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
76
     * Create a new instance from an existing taxonomy.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
77
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
78
     * @param  string $identifier  The taxonomy identifier
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
79
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
80
     * @throws NonExistentTaxonomyException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
NonExistentTaxonomyException => \Silk\Taxonomy\Exception\NonExistentTaxonomyException
Loading history...
81
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
82
     * @return static
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
83
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
84
    public static function load($identifier)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
85
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
86
        if (! $object = get_taxonomy($identifier)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
Conditional inline assignment not allowed
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
87
            throw new NonExistentTaxonomyException("No taxonomy exists with name '$identifier'.");
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
88
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
89
90
        return new static($object);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
91
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
92
93
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
94
     * Check if the given taxonomy exists.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
95
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
96
     * @param  string $id The taxonomy key/identifier
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
97
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
98
     * @return bool
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
99
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
100
    public static function exists($id)
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
101
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
102
        return taxonomy_exists($id);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
103
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
104
105
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
106
     * Start a new query for terms of this taxonomy.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
107
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
108
     * @return QueryBuilder
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
QueryBuilder => \Silk\Term\QueryBuilder
Loading history...
109
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
110
    public function terms()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
111
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
112
        return (new QueryBuilder)->forTaxonomy($this->id);
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
113
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
114
115
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
116
     * Get all post types associated with this taxonomy.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
117
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
118
     * @return Collection
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Collection => \Illuminate\Support\Collection
Loading history...
119
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
120
    public function postTypes()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
121
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
122
        return Collection::make($this->object_type)
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
123
            ->map(function ($post_type) {
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
124
                return PostType::load($post_type);
0 ignored issues
show
introduced by
16 spaces found, expected 4 tabs
Loading history...
125
            });
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
126
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
127
128
    /**
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
129
     * Unregister the taxonomy.
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
130
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
131
     * @throws NonExistentTaxonomyException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
NonExistentTaxonomyException => \Silk\Taxonomy\Exception\NonExistentTaxonomyException
Loading history...
132
     * @throws WP_ErrorException
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
WP_ErrorException => \Silk\Exception\WP_ErrorException
Loading history...
133
     *
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
134
     * @return $this
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
135
     */
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
136
    public function unregister()
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
137
    {
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
introduced by
Opening brace of a function must be on the same line as the definition
Loading history...
138
        if (! $this->exists($this->id)) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
introduced by
No whitespace should be between cast and variable.
Loading history...
139
            throw new NonExistentTaxonomyException;
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
introduced by
Calling class constructors must always include parentheses
Loading history...
140
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
141
142
        if (is_wp_error($error = unregister_taxonomy($this->id))) {
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
143
            throw new WP_ErrorException($error);
0 ignored issues
show
introduced by
12 spaces found, expected 3 tabs
Loading history...
144
        }
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
145
146
        return $this;
0 ignored issues
show
introduced by
8 spaces found, expected 2 tabs
Loading history...
147
    }
0 ignored issues
show
introduced by
4 spaces found, expected 1 tabs
Loading history...
148
}
0 ignored issues
show
introduced by
Closing brace of a class must have a new line between itself and the last content.
Loading history...
149