Skip to content

Commit 29730c2

Browse files
author
Daniel Morse
committed
feat: extend 'initialize_props()' to return array with default props in snake_case
1 parent 5294f0f commit 29730c2

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

packages/core-php/src/TwigFunctions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ public static function create_attribute() {
267267

268268
// Loops through _context, returns props that are in schema
269269
public static function initialize_props() {
270-
return new Twig_SimpleFunction('initialize_props', function($items, $schema) {
271-
return Utils::buildPropsArray($items, $schema);
270+
return new Twig_SimpleFunction('initialize_props', function($items, $schema, $isInternal = false) {
271+
return Utils::buildPropsArray($items, $schema, $isInternal);
272272
});
273273
}
274274

packages/core-php/src/Utils.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static function isRemoteUrl($url) {
182182
}
183183

184184
/**
185-
* Check that schema "type" does not equal "array" or "object", nor does it contain an array with either of those values
185+
* Check that schema "type" does not equal "array" or "object", nor does it contain an array with either of those values.
186186
* @param string|array $type - The schema "type" value, can be passed as string or array
187187
* @return boolean - Returns true if "type" is allowed, i.e. no "array" or "object" values
188188
*/
@@ -192,39 +192,37 @@ public static function isAllowedSchemaType($type) {
192192
}
193193
}
194194

195-
196195
/**
197-
* Build an array of props, filter out any props that are not in the schema
196+
* Build an array of props by checking Twig "_context" against the schema, only returns top-level schema props. By default prop keys are converted to kebab-case.
198197
* @param array $items - Twig "_context", all the available template variables
199198
* @param array $schema - The schema object for a particular component
199+
* @param boolean $isInternal - If true default schema values are included in returned array and keys converted to snake_case
200200
* @return array - An associative array of props
201201
*/
202-
public static function buildPropsArray($items, $schema) {
202+
public static function buildPropsArray($items, $schema, $isInternal) {
203203
$props = array();
204204

205205
// If schema has properties to check against
206206
if (!empty($schema["properties"])) {
207-
foreach ($items as $key => $value) {
208-
// If item is in the schema (skip attributes)
209-
if (array_key_exists($key, $schema["properties"]) && $key != "attributes"){
210-
// Check the schema "type", skip over any that contain the value "array" or "object"
211-
if (array_key_exists("type", $schema["properties"][$key]) && self::isAllowedSchemaType($schema["properties"][$key]["type"])){
212-
if (strpos($key, '-')) {
213-
$props[$key] = $value;
214-
} else {
215-
if (strpos($key, '_')) {
216-
$ch = CaseHelperFactory::make(CaseHelperFactory::INPUT_TYPE_SNAKE_CASE);
207+
foreach ($schema["properties"] as $key => $value) {
208+
if ($key != "attributes") {
209+
// If schema prop is in the list of items (skip attributes)
210+
if (array_key_exists($key, $items)){
211+
// Check the schema "type", skip over any that contain the value "array" or "object"
212+
if (array_key_exists("type", $schema["properties"][$key]) && self::isAllowedSchemaType($schema["properties"][$key]["type"])){
213+
if ($isInternal) {
214+
$props[self::convertToSnakeCase($key, self::checkCaseType($key))] = $items[$key];
217215
} else {
218-
$ch = CaseHelperFactory::make(CaseHelperFactory::INPUT_TYPE_CAMEL_CASE);
216+
$props[self::convertToKebabCase($key, self::checkCaseType($key))] = $items[$key];
219217
}
220-
$props[$ch->toKebabCase($key)] = $value;
221218
}
219+
} else if ($isInternal && array_key_exists("default", $schema["properties"][$key])) {
220+
$props[self::convertToSnakeCase($key, self::checkCaseType($key))] = $schema["properties"][$key]["default"];
222221
}
223222
}
224223
}
225224
}
226225

227226
return $props;
228227
}
229-
230228
}

0 commit comments

Comments
 (0)