@@ -13,11 +13,10 @@ class Component extends BaseData
1313 /**
1414 * @param string $name the component name
1515 */
16- public function __construct (
17- string $ name ,
18- ) {
16+ public function __construct (string $ name )
17+ {
1918 $ this ->data = [];
20- $ this ->data [' name ' ] = $ name ;
19+ $ this ->data [" name " ] = $ name ;
2120 }
2221
2322 /**
@@ -27,171 +26,226 @@ public function __construct(
2726 public static function make (array $ data = []): self
2827 {
2928 $ dataObject = new StoryblokData ($ data );
30- if (!($ dataObject ->hasKey ('name ' ))) {
31- throw new StoryblokFormatException ("Component is not valid, missing the name " );
29+ if (!$ dataObject ->hasKey ("name " )) {
30+ throw new StoryblokFormatException (
31+ "Component is not valid, missing the name " ,
32+ );
3233 }
3334
34- $ component = new Component (
35- $ dataObject ->getString ("name " )
36- );
35+ $ component = new Component ($ dataObject ->getString ("name " ));
3736 $ component ->setData ($ dataObject ->toArray ());
3837 // validate
39- if (! $ component ->isValid ()) {
38+ if (!$ component ->isValid ()) {
4039 if ($ dataObject ->getString ("name " ) === "" ) {
41- throw new StoryblokFormatException ("Component is not valid, missing the name " );
40+ throw new StoryblokFormatException (
41+ "Component is not valid, missing the name " ,
42+ );
4243 }
4344
44- throw new StoryblokFormatException ("Component < " . $ dataObject ->getString ("name " ) . "> is not valid " );
45+ throw new StoryblokFormatException (
46+ "Component < " .
47+ $ dataObject ->getString ("name " ) .
48+ "> is not valid " ,
49+ );
4550 }
4651
4752 return $ component ;
48-
4953 }
5054
5155 public function setName (string $ name ): void
5256 {
53- $ this ->set (' name ' , $ name );
57+ $ this ->set (" name " , $ name );
5458 }
5559
5660 /**
5761 * Technical name used for component property in entries
5862 */
5963 public function name (): string
6064 {
61- return $ this ->getString (' name ' );
65+ return $ this ->getString (" name " );
6266 }
6367
6468 /**
6569 * Component name based on the technical name or display name
6670 */
6771 public function realName (): string
6872 {
69- return $ this ->getString (' real_name ' );
73+ return $ this ->getString (" real_name " );
7074 }
7175
7276 /**
7377 * Name that will be used in the editor interface
7478 */
7579 public function displayName (): string
7680 {
77- return $ this ->getString (' display_name ' );
81+ return $ this ->getString (" display_name " );
7882 }
7983
8084 /**
8185 * Name that will be used in the editor interface
8286 */
8387 public function setDisplayName (string $ displayName ): void
8488 {
85- $ this ->set (' display_name ' , $ displayName );
89+ $ this ->set (" display_name " , $ displayName );
8690 }
8791
8892 /**
8993 * URL to the preview image, if uploaded
9094 */
9195 public function image (): string |null
9296 {
93- return $ this ->getStringNullable (' image ' );
97+ return $ this ->getStringNullable (" image " );
9498 }
9599
96100 /**
97101 * URL to the preview image, if uploaded
98102 */
99103 public function setImage (string $ url ): void
100104 {
101- $ this ->set (' image ' , $ url );
105+ $ this ->set (" image " , $ url );
102106 }
103107
104108 /**
105109 * The field that is for preview in the interface (Preview Field)
106110 */
107111 public function previewField (): string
108112 {
109- return $ this ->getString (' preview_field ' );
113+ return $ this ->getString (" preview_field " );
110114 }
111115
112116 /**
113117 * The field that is for preview in the interface (Preview Field)
114118 */
115119 public function setPreviewField (string $ value ): void
116120 {
117- $ this ->set (' preview_field ' , $ value );
121+ $ this ->set (" preview_field " , $ value );
118122 }
119123
120124 /**
121125 * Creation date
122126 */
123- public function createdAt (string $ format = self ::DEFAULT_DATE_FORMAT ): null |string
124- {
125- return $ this ->getFormattedDateTime ('created_at ' , "" , format: $ format );
127+ public function createdAt (
128+ string $ format = self ::DEFAULT_DATE_FORMAT ,
129+ ): null |string {
130+ return $ this ->getFormattedDateTime ("created_at " , "" , format: $ format );
126131 }
127132
128133 /**
129134 * Latest update date
130135 */
131- public function updatedAt (string $ format = self ::DEFAULT_DATE_FORMAT ): null |string
132- {
133- return $ this ->getFormattedDateTime ('updated_at ' , "" , format: $ format );
136+ public function updatedAt (
137+ string $ format = self ::DEFAULT_DATE_FORMAT ,
138+ ): null |string {
139+ return $ this ->getFormattedDateTime ("updated_at " , "" , format: $ format );
134140 }
135141
136142 /**
137143 * The numeric ID in string format "12345678"
138144 */
139145 public function id (): string
140146 {
141- return $ this ->getString (' id ' );
147+ return $ this ->getString (" id " );
142148 }
143149
144150 /**
145151 * True if the component can be used as a Content Type
152+ * for example if is a content-type or a universal
146153 */
147154 public function isRoot (): bool
148155 {
149- return $ this ->getBoolean (' is_root ' );
156+ return $ this ->getBoolean (" is_root " );
150157 }
151158
152159 /**
153160 * If the component can be used as a Content Type
154161 */
155162 public function setRoot (bool $ isRoot = true ): void
156163 {
157- $ this ->set ('is_root ' , $ isRoot );
164+ $ this ->set ("is_root " , $ isRoot );
165+ }
166+
167+ /**
168+ * True if the component can be nested inside other components
169+ */
170+ public function isNestable (): bool
171+ {
172+ return $ this ->getBoolean ("is_nestable " );
173+ }
174+
175+ /**
176+ * If the component can be nested inside other components
177+ */
178+ public function setNestable (bool $ isNestable = true ): void
179+ {
180+ $ this ->set ("is_nestable " , $ isNestable );
181+ }
182+
183+ /**
184+ * True if the component is a content type (is_root only)
185+ */
186+ public function isContentType (): bool
187+ {
188+ return $ this ->getBoolean ("is_root " ) &&
189+ !$ this ->getBoolean ("is_nestable " );
190+ }
191+
192+ /**
193+ * True if the component is universal (is_root and is_nestable)
194+ */
195+ public function isUniversal (): bool
196+ {
197+ return $ this ->getBoolean ("is_root " ) && $ this ->getBoolean ("is_nestable " );
198+ }
199+
200+ /**
201+ * Returns the component type as a string:
202+ * "universal", "content-type", "nestable", or "".
203+ */
204+ public function getComponentTypeDetail (): string
205+ {
206+ return match (true ) {
207+ $ this ->isUniversal () => "universal " ,
208+ $ this ->isContentType () => "content-type " ,
209+ $ this ->isNestable () => "nestable " ,
210+ default => "" ,
211+ };
158212 }
159213
160214 public function uuid (): string
161215 {
162- return $ this ->getString (' uuid ' );
216+ return $ this ->getString (" uuid " );
163217 }
164218
165219 /**
166220 * @return mixed[]
167221 */
168222 public function getSchema (): array
169223 {
170- return $ this ->getArray (' schema ' );
224+ return $ this ->getArray (" schema " );
171225 }
172226
173227 /**
174228 * @param mixed[] $schema
175229 */
176230 public function setSchema (array $ schema ): void
177231 {
178- $ this ->set (' schema ' , $ schema );
232+ $ this ->set (" schema " , $ schema );
179233 }
180234
181235 /**
182236 * @param mixed[] $fieldAttributes
183237 */
184238 public function setField (string $ name , array $ fieldAttributes ): void
185239 {
186- $ this ->set (' schema. ' . $ name , $ fieldAttributes );
240+ $ this ->set (" schema. " . $ name , $ fieldAttributes );
187241 }
188242
189243 /**
190244 * Validates if the component data contains all required fields and valid values
191245 */
192246 public function isValid (): bool
193247 {
194- return $ this ->hasKey (' name ' );
248+ return $ this ->hasKey (" name " );
195249 }
196250
197251 /**
@@ -200,9 +254,7 @@ public function isValid(): bool
200254 */
201255 public function setTags (Tags $ tags ): self
202256 {
203-
204257 return $ this ->setTagsFromArray ($ tags ->getTagsArray ());
205-
206258 }
207259
208260 /**
0 commit comments