Jarves\Admin\Form\Form::mapData PHP Method

mapData() public method

public mapData ( array $defaultData = [], null | string[] $filterFields = null ) : array
$defaultData array
$filterFields null | string[]
return array
    public function mapData($defaultData = [], $filterFields = null)
    {
        $data = $this->getData();
        if ($filterFields) {
            $filterFields = array_flip($filterFields);
        }
        $values = [];
        foreach ($this->getFields() as $field) {
            $key = lcfirst($field->getId());
            if (isset($data[$key])) {
                $value = $data[$key];
            } else {
                $value = Tools::getArrayPath($data, $key);
            }
            if (null === $value && $defaultData) {
                $value = isset($defaultData[$key]) ? $defaultData[$key] : null;
            }
            if (null === $value && $field->getDefault()) {
                $value = $field->getDefault();
            }
            if ($field['customValue'] && method_exists($this, $method = $field['customValue'])) {
                $value = $this->{$method}($field, $key);
            }
            $field->setValue($value);
        }
        foreach ($this->getFields() as $field) {
            $key = $field->getId();
            if ($field['noSave']) {
                continue;
            }
            if ($field->getSaveOnlyFilled() && ($field->getValue() === '' || $field->getValue() === null)) {
                continue;
            }
            if ($field->getCustomSave() && method_exists($this, $method = $field->getCustomSave())) {
                $this->{$method}($values, $values, $field);
                continue;
            }
            $startKey = explode('.', $key)[0];
            if (!$filterFields || isset($filterFields[$startKey])) {
                if (!($errors = $field->validate())) {
                    $field->mapValues($values);
                } else {
                    $restException = new ValidationFailedException(sprintf('Field `%s` has a invalid value. [%s]', $key, json_encode($errors)), 420);
                    $restException->setData(['fields' => [$field->getId() => $errors]]);
                    throw $restException;
                }
            }
        }
        return $values;
    }

Usage Example

Example #1
0
 /**
  * Maps all $data to its field values (Admin\Type*::mapValues)
  * Iterates only through all defined fields in $fields.
  *
  * @param  array $data
  * @param  string[] $filterFields Field name list to map, empty for all
  * @param  mixed $defaultData Default data. Is used if a field is not defined through _POST or _GET
  *
  * @return array
  * @throws \Jarves\Exceptions\InvalidFieldValueException
  */
 public function mapData(array $data, array $filterFields = null, $defaultData = null)
 {
     $fields = $this->_fields;
     $form = new Form($fields);
     $form->setData($data);
     $values = $form->mapData($defaultData, $filterFields);
     return $values;
 }