Completed
Pull Request — master (#17)
by Evan
02:25
created

PostTypeBuilder::assembleArgs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 11
rs 9.4285
1
<?php
2
3
namespace Silk\Post;
4
5
use Silk\Post\PostTypeLabels;
6
use Illuminate\Support\Collection;
7
use Silk\Post\Exception\InvalidPostTypeNameException;
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 Post Type Labels
30
     * @var PostTypeLabels
31
     */
32
    protected $labels;
33
34
    /**
35
     * PostTypeBuilder constructor
36
     *
37
     * @param string $slug  post type slug
38
     * @param array $args   initial registration arguments
39
     */
40
    public function __construct($slug, array $args = [])
41
    {
42
        if (strlen($slug) < 1 || strlen($slug) > 20) {
43
            throw new InvalidPostTypeNameException('Post type names must be between 1 and 20 characters in length.');
44
        }
45
46
        $this->slug = $slug;
47
        $this->args = new Collection($args);
48
    }
49
50
    /**
51
     * Create a new instance
52
     *
53
     * @param  string $slug
54
     *
55
     * @return static
56
     */
57
    public static function make($slug)
58
    {
59
        return new static($slug);
60
    }
61
62
    /**
63
     * Specify which features the post type supports
64
     * @param  mixed $features  array of features
65
     *         string ...$features  features as parameters
66
     *
67
     * @return $this
68
     */
69
    public function supports($features)
70
    {
71
        if (! is_array($features)) {
72
            $features = func_get_args();
73
        }
74
75
        return $this->set('supports', $features);
76
    }
77
78
    /**
79
     * Set the post type as publicly available
80
     *
81
     * @return $this
82
     */
83
    public function open()
84
    {
85
        return $this->set('public', true);
86
    }
87
88
    /**
89
     * Set the post type as non-publicly available
90
     *
91
     * @return $this
92
     */
93
    public function closed()
94
    {
95
        return $this->set('public', false);
96
    }
97
98
    /**
99
     * Enable admin interface
100
     *
101
     * @return $this
102
     */
103
    public function withUI()
104
    {
105
        return $this->set('show_ui', true);
106
    }
107
108
    /**
109
     * Disable admin interface
110
     *
111
     * @return $this
112
     */
113
    public function noUI()
114
    {
115
        return $this->set('show_ui', false);
116
    }
117
118
    /**
119
     * Set the singular label for this post type
120
     *
121
     * @param  string $singular_label
122
     *
123
     * @return $this
124
     */
125
    public function oneIs($singular_label)
126
    {
127
        $this->labels()->setSingular($singular_label);
128
129
        return $this;
130
    }
131
132
    /**
133
     * Set the plural label for this post type
134
     *
135
     * @param  string $plural_label
136
     *
137
     * @return $this
138
     */
139
    public function manyAre($plural_label)
140
    {
141
        $this->labels()->setPlural($plural_label);
142
143
        return $this;
144
    }
145
146
    /**
147
     * Setter for post type arguments
148
     *
149
     * @param string $key
150
     * @param mixed $value
151
     *
152
     * @return $this
153
     */
154
    public function set($key, $value)
155
    {
156
        $this->args->put($key, $value);
157
158
        return $this;
159
    }
160
161
    /**
162
     * Getter for post type arguments
163
     *
164
     * @param  string $key
165
     *
166
     * @return mixed
167
     */
168
    public function get($key)
169
    {
170
        return $this->args->get($key);
171
    }
172
173
    /**
174
     * Register the post type
175
     *
176
     * @return PostType
177
     */
178
    public function register()
179
    {
180
        $object = register_post_type($this->slug, $this->assembleArgs());
181
182
        return new PostType($object);
183
    }
184
185
    /**
186
     * Assemble the arguments for post type registration.
187
     *
188
     * @return array
189
     */
190
    protected function assembleArgs()
191
    {
192
        $registered_labels = $this->args->get('labels', []);
193
194
        /**
195
         * Override any generated labels with those that were passed in arguments.
196
         */
197
        $labels = array_merge($this->labels()->toArray(), $registered_labels);
198
199
        return $this->args->put('labels', $labels)->toArray();
200
    }
201
202
    /**
203
     * Get the labels instance.
204
     *
205
     * @return PostTypeLabels
206
     */
207
    protected function labels()
208
    {
209
        if (! $this->labels) {
210
            $this->labels = new PostTypeLabels;
211
        }
212
213
        return $this->labels;
214
    }
215
216
    /**
217
     * Magic Getter
218
     *
219
     * @param  string $property  The accessed property
220
     *
221
     * @return mixed
222
     */
223
    public function __get($property)
0 ignored issues
show
Unused Code introduced by
The parameter $property is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
224
    {
225
        return null;
226
    }
227
}
228