Skip to content

Commit

Permalink
Aggregate entity multiple value support
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Stříbrný committed May 11, 2016
1 parent e2f0385 commit 18380bf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 28 deletions.
19 changes: 5 additions & 14 deletions src/Components/AjaxSelect/Entities/AbstractEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,17 @@ public function parseOptions(array $options) {
* @param array $values
* @return bool[] $value => $isValid
*/
public function areValidValues($values) {
$result = [ ];

if (!is_array($values)) {
$values = [ $values ];
}

foreach ($values as $value) {
$result[$value] = $this->isValidValue($value);
}

return $result;
}
public abstract function areValidValues(array $values);

/**
* @internal
* @param mixed $value
* @return bool
*/
public abstract function isValidValue($value);
public function isValidValue($value) {
$areValid = $this->areValidValues([ $value ]);
return !empty($areValid[$value]);
}

/**
* @internal
Expand Down
28 changes: 14 additions & 14 deletions src/Components/AjaxSelect/Entities/AggregateEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,29 +163,29 @@ public function prefix($prefix, $value) {
return $prefix . $this->prefixSeparator . $value;
}

/**
* @internal
* @param mixed|array $value
* @return bool
*/
public function isValidValue($value) {
$byPrefix = $this->groupByPrefix($value);
public function areValidValues(array $values) {
// all values are invalid by default
$result = array_combine($values, array_fill(0, count($values), FALSE));

$byPrefix = $this->groupByPrefix($values);
if ($byPrefix === FALSE) {
return FALSE;
// invalid prefix(es), sorry
return $result;
}

// check if all nested entities report their values as valid
foreach ($byPrefix as $prefix => $values) {
foreach ($byPrefix as $prefix => $nestedValues) {
$entity = $this->entities[$prefix];
$areValid = $entity->areValidValues($nestedValues);

if (!$entity->isValidValue($values)) {
// if at least one does not, value is invalid
return FALSE;
foreach ($areValid as $value => $isValid) {
if ($isValid) {
$key = $this->prefix($prefix, $value);
$result[$key] = TRUE;
}
}
}

return TRUE;
return $result;
}

/**
Expand Down

0 comments on commit 18380bf

Please sign in to comment.