/** * @description Sets the field value based upon the path. * @param array $pathToField * @param mixed $value * @return $this */ public function setByArrayPath($pathToField, $value) { return setValueByArrayPath($this, $pathToField, $value); }
/** * @param array|object $doc * @param string[] $fieldPath * @param mixed $value * @return mixed the updated document * @throws UnableToSetFieldException */ function setValueByArrayPath($doc, $fieldPath, $value) { $fieldPath = (array) $fieldPath; // Force a copy if it is an ArrayObject. if (empty($fieldPath)) { return $value; } // Special case for nulls, treat them like empty arrays. $doc = is_null($doc) ? array() : $doc; $fieldName = array_shift($fieldPath); $subDoc = getValue($doc, $fieldName, array()); $subDocUpdated = setValueByArrayPath($subDoc, $fieldPath, $value); if ($subDocUpdated !== $subDoc) { $doc = setValue($doc, $fieldName, $subDocUpdated); } return $doc; }