Skip to content

Commit

Permalink
feat(model-structure-property): improve ModelStructureProperty.php
Browse files Browse the repository at this point in the history
Added support for validating model structures
  • Loading branch information
JoelAlphonso authored and mcaskill committed Mar 5, 2024
1 parent c2f385e commit 47c1ae0
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/property/src/Charcoal/Property/ModelStructureProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Charcoal\Model\MetadataInterface;
use Charcoal\Model\ModelInterface;
use Charcoal\Model\Model;
use Charcoal\Validator\ValidatableInterface;
// From 'charcoal-factory'
use Charcoal\Factory\FactoryInterface;
// From 'charcoal-property'
Expand Down Expand Up @@ -167,6 +168,60 @@ public function type()
return 'model-structure';
}

/**
* {@inheritdoc}
*/
public function validationMethods()
{
return array_merge(parent::validationMethods(), [
'modelStructure',
]);
}

/**
* @return boolean
*/
public function validateModelStructure(): bool
{
$result = true;

$model = $this->structureProto();
if (
!($model instanceof ModelInterface) ||
!($model instanceof ValidatableInterface)
) {
return $result;
}

$val = $this->val();

if ($this['multiple']) {
$objs = (array)$this->structureVal($val);
$val = [];
if (!empty($objs)) {
$val = [];
foreach ($objs as $obj) {
if ($obj->validate() === false) {
$validator = $obj->validator();
$this->validator()->merge($validator/*, $ident*/);
$result = false;
}
}
}

return $result;
}

$obj = $this->structureVal($val);
if ($obj && $obj->validate() === false) {
$validator = $obj->validator();
$this->validator()->merge($validator/*, $ident*/);
$result = false;
}

return $result;
}

/**
* Retrieve the property's structure.
*
Expand Down

0 comments on commit 47c1ae0

Please sign in to comment.