Completed
Push — master ( 409ce2...ca0be7 )
by Evan
07:40
created

PostTypeBuilder::set()   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
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 9.4285
1
<?php
2
3
namespace Silk\Post;
4
5
use Illuminate\Support\Collection;
6
use Silk\Post\Exception\InvalidPostTypeNameException;
7
use Silk\Post\PostTypeBuilder;
8
9
/**
10
 * @property-read string $slug
11
 * @property-read string $one
12
 * @property-read string $many
13
 */
14
class PostTypeBuilder
15
{
16
    /**
17
     * The post type slug.
18
     * @var string
19
     */
20
    protected $slug;
21
22
    /**
23
     * The arguments to be passed when registering the post type.
24
     * @var Collection
25
     */
26
    protected $args;
27
28
    /**
29
     * The label for a single instance of this post type.
30
     * @var string
31
     */
32
    protected $labelSingular;
33
34
    /**
35
     * The label for a many instances of this post type.
36
     * @var string
37
     */
38
    protected $labelPlural;
39
40
    /**
41
     * PostTypeBuilder constructor
42
     *
43
     * @param string $slug  post type slug
44
     * @param array $args   initial registration arguments
45
     */
46
    public function __construct($slug, array $args = [])
47
    {
48
        if (strlen($slug) < 1 || strlen($slug) > 20) {
49
            throw new InvalidPostTypeNameException('Post type names must be between 1 and 20 characters in length.');
50
        }
51
52
        $this->slug = $slug;
53
        $this->args = new Collection($args);
54
    }
55
56
    /**
57
     * Create a new instance
58
     *
59
     * @param  string $slug
60
     *
61
     * @return static
62
     */
63
    public static function make($slug)
64
    {
65
        return new static($slug);
66
    }
67
68
    /**
69
     * Specify which features the post type supports
70
     * @param  mixed $features  array of features
71
     *         string ...$features  features as parameters
72
     *
73
     * @return $this
74
     */
75
    public function supports($features)
76
    {
77
        if (! is_array($features)) {
78
            $features = func_get_args();
79
        }
80
81
        return $this->set('supports', $features);
82
    }
83
84
    /**
85
     * Set the post type as publicly available
86
     *
87
     * @return $this
88
     */
89
    public function open()
90
    {
91
        return $this->set('public', true);
92
    }
93
94
    /**
95
     * Set the post type as non-publicly available
96
     *
97
     * @return $this
98
     */
99
    public function closed()
100
    {
101
        return $this->set('public', false);
102
    }
103
104
    /**
105
     * Enable admin interface
106
     *
107
     * @return $this
108
     */
109
    public function withUI()
110
    {
111
        return $this->set('show_ui', true);
112
    }
113
114
    /**
115
     * Disable admin interface
116
     *
117
     * @return $this
118
     */
119
    public function noUI()
120
    {
121
        return $this->set('show_ui', false);
122
    }
123
124
    /**
125
     * Set the singular label for this post type
126
     *
127
     * @param  string $singular_label
128
     *
129
     * @return $this
130
     */
131
    public function oneIs($singular_label)
132
    {
133
        $this->labelSingular = $singular_label;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Set the plural label for this post type
140
     *
141
     * @param  string $plural_label
142
     *
143
     * @return $this
144
     */
145
    public function manyAre($plural_label)
146
    {
147
        $this->labelPlural = $plural_label;
148
149
        return $this;
150
    }
151
152
    /**
153
     * Setter for post type arguments
154
     *
155
     * @param string $key
156
     * @param mixed $value
157
     */
158
    public function set($key, $value)
159
    {
160
        $this->args->put($key, $value);
161
162
        return $this;
163
    }
164
165
    /**
166
     * Getter for post type arguments
167
     *
168
     * @param  string $key
169
     *
170
     * @return mixed
171
     */
172
    public function get($key)
173
    {
174
        return $this->args->get($key);
175
    }
176
177
    /**
178
     * Register the post type
179
     *
180
     * @return PostType
181
     */
182
    public function register()
183
    {
184
        $object = register_post_type($this->slug, $this->args->toArray());
185
186
        if (is_wp_error($object)) {
187
            throw new WP_ErrorException($object);
188
        }
189
190
        return new PostType($object);
191
    }
192
193
    /**
194
     * Magic Getter
195
     *
196
     * @param  string $property
197
     *
198
     * @return mixed
199
     */
200
    public function __get($property)
201
    {
202
        switch ($property) :
203
            case 'slug': return $this->slug;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
204
            case 'one':  return $this->labelSingular;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
205
            case 'many': return $this->labelPlural;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
206
        endswitch;
207
208
        return null;
209
    }
210
}
211