function merge(array $data = null)
 {
     if (is_null($data) || is_null($this->model)) {
         return;
     }
     $data = array_normalizeEmptyValues($data, true);
     if (is_array($this->model)) {
         array_recursiveMergeInto($this->model, $data);
     } else {
         if (is_object($this->model)) {
             extend($this->model, $data);
         } else {
             throw new FatalException(sprintf("Can't merge data into a model of type <kbd>%s</kbd>", gettype($this->model)));
         }
     }
 }
Пример #2
0
/**
 * Merges properties from a source object (or array) into a target object.
 *
 * <p>Assignments are recursive.
 * <p>If the target property is an object implementing ArrayAccess, the assignment is performed via `[]`, otherwise it's
 * performed via `->`.
 *
 * @param object       $target
 * @param object|array $src
 * @throws InvalidArgumentException If any of the arguments is not of one of the expected types.
 */
function extend($target, $src)
{
    $c = $target instanceof ArrayAccess;
    if (isset($src)) {
        if (is_iterable($src)) {
            if (is_object($target)) {
                foreach ($src as $k => $v) {
                    // iterates both objects and arrays
                    if (isset($target->{$k}) && (is_array($v) || is_object($v))) {
                        if (is_object($target->{$k})) {
                            extend($target->{$k}, $v);
                        } elseif (is_array($target->{$k})) {
                            array_recursiveMergeInto($target->{$k}, $v);
                        } elseif ($c) {
                            $target[$k] = $v;
                        } else {
                            $target->{$k} = $v;
                        }
                    } elseif ($c) {
                        $target[$k] = $v;
                    } else {
                        $target->{$k} = $v;
                    }
                }
            } else {
                throw new InvalidArgumentException('Invalid target argument');
            }
        } else {
            throw new InvalidArgumentException('Invalid source argument');
        }
    }
}