1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Type; |
4
|
|
|
|
5
|
|
|
use Silk\Support\Collection; |
6
|
|
|
|
7
|
|
|
abstract class Builder |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The type identifier |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $id; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Registration arguments |
17
|
|
|
* @var Collection |
18
|
|
|
*/ |
19
|
|
|
protected $args; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Labels collection |
23
|
|
|
* @var Labels |
24
|
|
|
*/ |
25
|
|
|
protected $labels; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Default labels |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $labelDefaults = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Builder Constructor. |
35
|
|
|
* |
36
|
|
|
* @param string $id Type identifier |
37
|
|
|
* @param array $args Initial arguments |
38
|
|
|
*/ |
39
|
|
|
public function __construct($id, array $args = []) |
40
|
|
|
{ |
41
|
|
|
$this->id = $id; |
42
|
|
|
$this->args = new Collection($args); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a new instance. |
47
|
|
|
* |
48
|
|
|
* @param string $type |
49
|
|
|
* |
50
|
|
|
* @return static |
51
|
|
|
*/ |
52
|
|
|
public static function make($type) |
53
|
|
|
{ |
54
|
|
|
return new static($type); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Register the type. |
59
|
|
|
* |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
abstract public function register(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Assemble the arguments for registration. |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
protected function assembleArgs() |
70
|
|
|
{ |
71
|
|
|
return $this->args->put('labels', $this->labels())->toArray(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Set the singular label for this post type. |
76
|
|
|
* |
77
|
|
|
* @param string $singular_label |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function oneIs($singular_label) |
82
|
|
|
{ |
83
|
|
|
$this->labels()->setSingular($singular_label); |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the plural label for this post type. |
90
|
|
|
* |
91
|
|
|
* @param string $plural_label |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function manyAre($plural_label) |
96
|
|
|
{ |
97
|
|
|
$this->labels()->setPlural($plural_label); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the labels instance. |
104
|
|
|
* |
105
|
|
|
* @return Labels |
106
|
|
|
*/ |
107
|
|
|
protected function labels() |
108
|
|
|
{ |
109
|
|
|
if (! $this->labels) { |
110
|
|
|
$this->labels = Labels::make( |
111
|
|
|
$this->labelDefaults |
112
|
|
|
)->merge($this->args->get('labels', [])); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->labels; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set a label for the given key. |
120
|
|
|
* |
121
|
|
|
* @param string $key Label key |
122
|
|
|
* @param string $value Label value |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setLabel($key, $value) |
127
|
|
|
{ |
128
|
|
|
$this->labels()->put($key, $value); |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Setter for post type arguments. |
135
|
|
|
* |
136
|
|
|
* @param string $key |
137
|
|
|
* @param mixed $value |
138
|
|
|
* |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
|
|
public function set($key, $value) |
142
|
|
|
{ |
143
|
|
|
$this->args->put($key, $value); |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Getter for post type arguments. |
150
|
|
|
* |
151
|
|
|
* @param string $key |
152
|
|
|
* |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
public function get($key) |
156
|
|
|
{ |
157
|
|
|
return $this->args->get($key); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|