Skip to content

Commit

Permalink
Merge pull request #3 from gathercontent/fix/empy-labels
Browse files Browse the repository at this point in the history
Fix/empy labels
  • Loading branch information
lukerenfrew authored Aug 11, 2021
2 parents 2b3c1bf + 4039c53 commit c7d807b
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/GatherContent/ConfigValueObject/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@

final class Validator
{
public static function notBlank($value, $message = null, string $propertyPath = null): bool
{
if (false === $value || (empty($value) && '0' != $value) ) {

$message = sprintf(
$message ?: 'Value "%s" is blank, but was expected to contain a value.',
(string) $value
);

throw new \Assert\InvalidArgumentException($value, $message, Assertion::INVALID_NOT_BLANK, $propertyPath);
}

return true;
}

public function validate($config)
{
$this->validateConfigNotEmpty($config);
Expand All @@ -17,7 +32,7 @@ public function validate($config)
private function validateConfigNotEmpty($config)
{
Assertion::isArray($config, 'Config must be array');
Assertion::notEmpty($config, 'Config must not be empty');
$this->notBlank($config, 'Config must not be empty');
}

private function validateTabFormat($config)
Expand All @@ -34,8 +49,8 @@ private function validateTabFormat($config)
Assertion::string($tab->name, 'Tab name attribute must be string');
Assertion::boolean($tab->hidden, 'Tab hidden attribute must be boolean');
Assertion::isArray($tab->elements, 'Tab elements attribute must be array');
Assertion::notEmpty($tab->label, 'Tab label attribute must not be empty');
Assertion::notEmpty($tab->name, 'Tab name attribute must not be empty');
$this->notBlank($tab->label, 'Tab label attribute must not be empty');
$this->notBlank($tab->name, 'Tab name attribute must not be empty');
}
}

Expand Down Expand Up @@ -67,7 +82,7 @@ private function validateElementFormat($config)
Assertion::string($element->type, 'Element type attribute must be string');
Assertion::string($element->name, 'Element name attribute must be string');
Assertion::inArray($element->type, array('text', 'files', 'section', 'choice_radio', 'choice_checkbox'), 'Element must be of a supported type');
Assertion::notEmpty($element->name, 'Element name attribute must not be empty');
$this->notBlank($element->name, 'Element name attribute must not be empty');

switch ($element->type) {

Expand Down Expand Up @@ -116,7 +131,7 @@ private function validateTextElement($element)
Assertion::string($element->limit_type, 'Element limit_type attribute must be string');
Assertion::integer($element->limit, 'Element limit attribute must be integer');
Assertion::boolean($element->plain_text, 'Element plain_text attribute must be boolean');
Assertion::notEmpty($element->label, 'Element label attribute must not be empty');
$this->notBlank($element->label, 'Element label attribute must not be empty');
Assertion::inArray($element->limit_type, array('words', 'chars'), 'Element limit_type attribute value must be either "words" or "chars"');
Assertion::min($element->limit, 0, 'Element limit attribute must not be negative');
}
Expand All @@ -130,7 +145,7 @@ private function validateFilesElement($element)
Assertion::boolean($element->required, 'Element required attribute must be boolean');
Assertion::string($element->label, 'Element label attribute must be string');
Assertion::string($element->microcopy, 'Element microcopy attribute must be string');
Assertion::notEmpty($element->label, 'Element label attribute must not be empty');
$this->notBlank($element->label, 'Element label attribute must not be empty');
}

private function validateSectionElement($element)
Expand All @@ -140,7 +155,7 @@ private function validateSectionElement($element)
Assertion::eq(count(get_object_vars($element)), 4, 'Element must not have additional attributes');
Assertion::string($element->title, 'Element title attribute must be string');
Assertion::string($element->subtitle, 'Element subtitle attribute must be string');
Assertion::notEmpty($element->title, 'Element title attribute must not be empty');
$this->notBlank($element->title, 'Element title attribute must not be empty');
}

private function validateChoiceRadioElement($element)
Expand All @@ -156,8 +171,8 @@ private function validateChoiceRadioElement($element)
Assertion::string($element->microcopy, 'Element microcopy attribute must be string');
Assertion::boolean($element->other_option, 'Element other_option attribute must be boolean');
Assertion::isArray($element->options, 'Element options attribute must be array');
Assertion::notEmpty($element->label, 'Element label attribute must not be empty');
Assertion::notEmpty($element->options, 'Element must have at least one option');
$this->notBlank($element->label, 'Element label attribute must not be empty');
$this->notBlank($element->options, 'Element must have at least one option');

foreach ($element->options as $option) {
$this->validateOptionFormatForChoiceRadio($option, $element);
Expand Down Expand Up @@ -213,8 +228,8 @@ private function validateChoiceCheckboxElement($element)
Assertion::string($element->label, 'Element label attribute must be string');
Assertion::string($element->microcopy, 'Element microcopy attribute must be string');
Assertion::isArray($element->options, 'Element options attribute must be array');
Assertion::notEmpty($element->label, 'Element label attribute must not be empty');
Assertion::notEmpty($element->options, 'Element must have at least one option');
$this->notBlank($element->label, 'Element label attribute must not be empty');
$this->notBlank($element->options, 'Element must have at least one option');

foreach ($element->options as $option) {
$this->validateOptionFormatForChoiceCheckbox($option);
Expand Down Expand Up @@ -250,8 +265,8 @@ private function validateOptionFormatForChoiceRadio($option, $element)
Assertion::string($option->name, 'Option name attribute must be string');
Assertion::string($option->label, 'Option label attribute must be string');
Assertion::boolean($option->selected, 'Option selected attribute must be boolean');
Assertion::notEmpty($option->name, 'Option name attribute must not be empty');
Assertion::notEmpty($option->label, 'Option label attribute must not be empty');
$this->notBlank($option->name, 'Option name attribute must not be empty');
$this->notBlank($option->label, 'Option label attribute must not be empty');

if ($element->other_option && json_encode($option) == json_encode(end($element->options))) {

Expand All @@ -274,8 +289,8 @@ private function validateOptionFormatForChoiceCheckbox($option)
Assertion::string($option->name, 'Option name attribute must be string');
Assertion::string($option->label, 'Option label attribute must be string');
Assertion::boolean($option->selected, 'Option selected attribute must be boolean');
Assertion::notEmpty($option->name, 'Option name attribute must not be empty');
Assertion::notEmpty($option->label, 'Option label attribute must not be empty');
$this->notBlank($option->name, 'Option name attribute must not be empty');
$this->notBlank($option->label, 'Option label attribute must not be empty');
Assertion::eq(count(get_object_vars($option)), 3, 'Option must not have additional attributes');
}

Expand Down

0 comments on commit c7d807b

Please sign in to comment.