getArrayPath() public static method

(['a' => ['b' => 5]], 'a.b') = 5
public static getArrayPath ( array $array, string $dotPath ) : mixed
$array array
$dotPath string
return mixed
Example #1
0
 /**
  * @param array         $defaultData
  * @param null|string[] $filterFields
  *
  * @return array
  * @throws ValidationFailedException
  */
 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;
 }
Example #2
0
 /**
  * @param Request|array $requestData
  * *@return array
  */
 public function collectData($requestData)
 {
     if ($requestData instanceof Request) {
         $requestData = $requestData->request->all();
     }
     $fields = $this->_fields;
     $values = [];
     foreach ($fields as $field) {
         $key = lcfirst($field->getId());
         if (isset($requestData[$key])) {
             $value = $requestData[$key];
         } else {
             $value = Tools::getArrayPath($requestData, $key);
         }
         $values[$key] = $value;
     }
     return $values;
 }