Skip to content

Commit

Permalink
Element::parent // introduce new methods Element::withParent() and El…
Browse files Browse the repository at this point in the history
…ement::parent() to keep track of the parent CollectionElement/Form and track "submission"-state on all Elements.

Element::attributesForView // introduced new method which prepares the "id" and "name" for rendering to take the Element parents into account.
Form, CollectionElement, Element // reduced amount of methods which are overwritten and ensure that with*() methods are not callable after submission.
  • Loading branch information
Chrico committed Sep 19, 2023
1 parent 1a805c2 commit bb27332
Show file tree
Hide file tree
Showing 22 changed files with 297 additions and 170 deletions.
7 changes: 3 additions & 4 deletions src/Element/ChoiceElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,18 @@ class ChoiceElement extends Element implements ChoiceElementInterface
protected ?ChoiceListInterface $list = null;

/**
* @param ChoiceListInterface $list
*
* @return static
* {@inheritDoc}
*/
public function withChoices(ChoiceListInterface $list): static
{
$this->assertNotSubmitted(__METHOD__);
$this->list = $list;

return $this;
}

/**
* @return ChoiceListInterface
* {@inheritDoc}
*/
public function choices(): ChoiceListInterface
{
Expand Down
53 changes: 26 additions & 27 deletions src/Element/CollectionElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,25 @@ class CollectionElement extends Element implements CollectionElementInterface
private array $allErrors = [];

/**
* @param ElementInterface[] $elements
*
* @return static
* {@inheritDoc}
*/
public function withElement(ElementInterface ...$elements): static
{
$this->assertNotSubmitted(__METHOD__);

array_walk(
$elements,
function (ElementInterface $element): void {
$this->elements[$element->name()] = $element;
$element->withParent($this);
}
);

return $this;
}

/**
* @param string $name
*
* @return ElementInterface
* @throws ElementNotFoundException
*
* {@inheritDoc}
*/
public function element(string $name): ElementInterface
{
Expand All @@ -66,9 +63,7 @@ public function element(string $name): ElementInterface
}

/**
* @param string $name
*
* @return bool
* {@inheritDoc}
*/
public function elementExists(string $name): bool
{
Expand All @@ -78,18 +73,24 @@ public function elementExists(string $name): bool
/**
* If the key is "value" and the $value an array, we assign all values to the children.
*
* @param string $key
* @param bool|int|string $value
*
* @return static
* {@inheritDoc}
*/
public function withAttribute(string $key, $value): static
{
$this->assertNotSubmitted(__METHOD__);

if ($key === 'value' && is_array($value)) {
foreach ($this->elements as $name => $element) {
$this->elements[$name]->withValue($value[$name] ?? '');
$assignedValues = [];
foreach ($value as $elementName => $elementValue) {
if (!$this->elementExists($elementName)) {
continue;
}
$this->element($elementName)->withValue($elementValue);
$assignedValues[$elementName] = $elementValue;
}

$this->attributes['value'] = $assignedValues;

return $this;
}

Expand All @@ -101,9 +102,7 @@ public function withAttribute(string $key, $value): static
/**
* Returns a list of values for each element inside the collection.
*
* @param string $key
*
* @return array
* {@inheritDoc}
*/
public function attribute(string $key)
{
Expand All @@ -118,7 +117,7 @@ public function attribute(string $key)
}

/**
* @return array
* {@inheritDoc}
*/
public function elements(): array
{
Expand All @@ -128,16 +127,13 @@ public function elements(): array
/**
* Delegate errors down to the children.
*
* @param array $errors
*
* @return static
* {@inheritDoc}
*/
public function withErrors(array $errors = []): static
{
$this->allErrors = $errors;

foreach ($this->elements as $element) {
$name = $element->name();
foreach ($this->elements as $name => $element) {
if (isset($errors[$name]) && $element instanceof ErrorAwareInterface) {
$element->withErrors((array) $errors[$name]);
unset($errors[$name]);
Expand All @@ -151,7 +147,7 @@ public function withErrors(array $errors = []): static
}

/**
* @return bool
* {@inheritDoc}
*/
public function hasErrors(): bool
{
Expand All @@ -167,6 +163,9 @@ public function hasErrors(): bool
return false;
}

/**
* {@inheritDoc}
*/
public function validate(): bool
{
$isValid = parent::validate();
Expand Down
Loading

0 comments on commit bb27332

Please sign in to comment.