public static function set($data, $key, $value, $delimiter = '.', $storeType = 'array')
    {
        $nodes = static::getPathNodes($key, $delimiter);

        if (empty($nodes)) {
            return $data;
        }

        /**
         * A closure as inner function to create data store.
         *
         * @param string $type Type name.
         *
         * @return  array
         *
         * @throws \InvalidArgumentException
         */
        $createStore = function ($type) {
            if (strtolower($type) === 'array') {
                return [];
            }

            if (class_exists($type)) {
                return new $type();
            }

            throw new \InvalidArgumentException(sprintf('Type or class: %s not exists', $type));
        };

        $dataTmp = &$data;

        foreach ($nodes as $node) {
            if (is_object($dataTmp)) {
                // If this node not exists, create new one.
                if (empty($dataTmp->$node)) {
                    $dataTmp->$node = $createStore($storeType);
                }

                $dataTmp = &$dataTmp->$node;
            } elseif (is_array($dataTmp)) {
                // If this node not exists, create new one.
                if (empty($dataTmp[$node])) {
                    $dataTmp[$node] = $createStore($storeType);
                }

                $dataTmp = &$dataTmp[$node];
            } else {
                // If a node is value but path is not go to the end, we replace this value as a new store.
                // Then next node can insert new value to this store.
                $dataTmp = &$createStore($storeType);
            }
        }

        // Now, path go to the end, means we get latest node, set value to this node.
        $dataTmp = $value;

        return $data;
    }
public static function set($data, $key, $value, $delimiter = '.', $storeType = 'array')
    {
        $nodes = static::getPathNodes($key, $delimiter);

        if (empty($nodes)) {
            return $data;
        }

        /**
         * A closure as inner function to create data store.
         *
         * @param string $type Type name.
         *
         * @return  array
         *
         * @throws \InvalidArgumentException
         */
        $createStore = function ($type) {
            if (strtolower($type) === 'array') {
                return [];
            }

            if (class_exists($type)) {
                return new $type();
            }

            throw new \InvalidArgumentException(sprintf('Type or class: %s not exists', $type));
        };

        $dataTmp = &$data;

        foreach ($nodes as $node) {
            if (is_object($dataTmp)) {
                // If this node not exists, create new one.
                if (empty($dataTmp->$node)) {
                    $dataTmp->$node = $createStore($storeType);
                }

                $dataTmp = &$dataTmp->$node;
            } elseif (is_array($dataTmp)) {
                // If this node not exists, create new one.
                if (empty($dataTmp[$node])) {
                    $dataTmp[$node] = $createStore($storeType);
                }

                $dataTmp = &$dataTmp[$node];
            } else {
                // If a node is value but path is not go to the end, we replace this value as a new store.
                // Then next node can insert new value to this store.
                $dataTmp = &$createStore($storeType);
            }
        }

        // Now, path go to the end, means we get latest node, set value to this node.
        $dataTmp = $value;

        return $data;
    }
public static function set ( $ data , $ key , $ value , $ delimiter = '.' , $ storeType = 'array' ) { $ nodes = static :: getPathNodes ( $ key , $ delimiter ) ; if ( empty ( $ nodes ) ) { return $ data ; } /**
         * A closure as inner function to create data store.
         *
         * @param string $type Type name.
         *
         * @return  array
         *
         * @throws \InvalidArgumentException
         */ $ createStore = function ( $ type ) { if ( strtolower ( $ type ) === 'array' ) { return [ ] ; } if ( class_exists ( $ type ) ) { return new $ type ( ) ; } throw new \ InvalidArgumentException ( sprintf ( 'Type or class: %s not exists' , $ type ) ) ; } ; $ dataTmp = & $ data ; foreach ( $ nodes as $ node ) { if ( is_object ( $ dataTmp ) ) { // If this node not exists, create new one. if ( empty ( $ dataTmp -> $ node ) ) { $ dataTmp -> $ node = $ createStore ( $ storeType ) ; } $ dataTmp = & $ dataTmp -> $ node ; } elseif ( is_array ( $ dataTmp ) ) { // If this node not exists, create new one. if ( empty ( $ dataTmp [ $ node ] ) ) { $ dataTmp [ $ node ] = $ createStore ( $ storeType ) ; } $ dataTmp = & $ dataTmp [ $ node ] ; } else { // If a node is value but path is not go to the end, we replace this value as a new store. // Then next node can insert new value to this store. $ dataTmp = & $ createStore ( $ storeType ) ; } } // Now, path go to the end, means we get latest node, set value to this node. $ dataTmp = $ value ; return $ data ; }
Set value into array or object. The key can be path type.

@param mixed  $data      An array or object to set data.
@param string $key       Path name separate by dot.
@param mixed  $value     Value to set into array or object.
@param string $delimiter Separator to split path.
@param string $storeType The new store data type, default is `array`. you can set object class name.

@return  array|object
@throws \InvalidArgumentException

@since   2.0
Set value into array or object . The key can be path type .