Skip to content

Commit

Permalink
feat: testing the concept (#360)
Browse files Browse the repository at this point in the history
* init: testing the concept

* test: adding test data

* feat: better message and allowing multiple acceptable types

* feat: better error handling

---------

Co-authored-by: Niclas Norin <niclas.norin@helsingborg.se>
Co-authored-by: Sebastian Thulin <sebastianthulin@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 13, 2024
1 parent cedff78 commit 2436e1b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion source/php/Component/Notice/Notice.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function init()
if (empty($id)) {
$this->data['id'] = uniqid();
}

if (in_array($type, ['success', 'warning', 'danger', 'info'])) {
$this->data['classList'][] = $this->getBaseClass() . "--" . $type;
} else {
Expand Down
6 changes: 6 additions & 0 deletions source/php/Component/Notice/notice.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
"icon": "The icon name as a string.",
"stretch": "If true, the notice will stretch to the full width of the viewport."
},
"types": {
"type": "string",
"message": "object",
"icon": "string",
"stretch": "boolean"
},
"view": "notice.blade.php",
"dependency": {
"sass": {
Expand Down
36 changes: 31 additions & 5 deletions source/php/Register.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public function __construct($engine)
* @param string|null $view The optional view name for the component.
* @throws \Exception if the provided slug is reserved or invalid.
*/
public function add($slug, $defaultArgs, $view = null)
public function add($slug, $defaultArgs, $argsTypes = false, $view = null)
{
//Create utility data object
if (is_null($this->data)) {
$this->data = (object) array();
}

//Prohibit reserved names
if (in_array($slug, $this->reservedNames)) {
throw new \Exception("Invalid slug (" . $slug . ") provided, cannot be used as a view name since it is reserved for internal purposes.");
Expand All @@ -52,7 +52,8 @@ public function add($slug, $defaultArgs, $view = null)
'slug' => (string) $slug,
'args' => (object) $defaultArgs,
'view' => (string) $slug . DIRECTORY_SEPARATOR . $view,
'controller' => (string) $slug
'controller' => (string) $slug,
'argsTypes' => (object) $argsTypes
);

//Add include alias
Expand Down Expand Up @@ -90,7 +91,7 @@ public function addControllerPath($path, $prepend = true): array
/**
* Registers components directory
*
* @return string The sluts of all registered components
* @return string The slugs of all registered components
*/
public function registerInternalComponents($path): array
{
Expand All @@ -116,6 +117,7 @@ public function registerInternalComponents($path): array
$this->add(
$config['slug'],
$config['default'],
$config['types'] ?? (object) [],
$config['view'] ? $config['view'] : $config['slug'] . "blade.php"
);

Expand Down Expand Up @@ -181,8 +183,10 @@ function ($view) use ($component) {
$controllerName = $this->camelCase(
$this->cleanViewName($component->slug)
);



$viewData = $this->accessProtected($view, 'data');
$this->handleTypingsErrors($viewData, $component->argsTypes, $component->slug);

// Get controller data
$controllerArgs = (array) $this->getControllerArgs(
Expand All @@ -198,6 +202,28 @@ function ($view) use ($component) {
}
}

public function handleTypingsErrors($viewData, $argsTypes = false, $componentSlug) {
if (empty((array) $argsTypes) || (empty($viewData) && !is_array($viewData))) {
return;
}

foreach ($viewData as $key => $value) {
if (isset($argsTypes->{$key})) {
$types = explode('|', $argsTypes->{$key});

if (!in_array(gettype($value), $types)) {
$this->triggerError('The parameter <b>"' . $key . '"</b> in the <b>' . $componentSlug . '</b> component should be of type <b>"' . $argsTypes->{$key} . '"</b> but was recieved as type <b>"' . gettype($value) . '"</b>.');
}
} else {
$this->triggerError('The parameter ' . '<b>"' . $key . '"</b> is not recognized in the component <b>"' . $componentSlug .'"</b>');
}
}
}

private function triggerError($message = "") {
trigger_error($message, E_USER_WARNING);
}

/**
* Proxy for accessing provate props
*
Expand Down

0 comments on commit 2436e1b

Please sign in to comment.