public function insert($name, $content, $parent = null, $before = false)
    {
        if (!is_string($name)) {
            throw new \Exception('Invalid body segment key ("' . gettype($name) . '")');
        }

        if ((null !== $parent) && !is_string($parent)) {
            throw new \Exception('Invalid body segment parent key ("' . gettype($parent) . '")');
        }

        if (isset($this->_body[$name])) {
            unset($this->_body[$name]);
        }

        if ((null === $parent) || !isset($this->_body[$parent])) {
            return $this->append($name, $content);
        }

        $ins  = array($name => (string) $content);
        $keys = array_keys($this->_body);
        $loc  = array_search($parent, $keys);
        if (!$before) {
            // Increment location if not inserting before
            ++$loc;
        }

        if (0 === $loc) {
            // If location of key is 0, we're prepending
            $this->_body = $ins + $this->_body;
        } elseif ($loc >= (count($this->_body))) {
            // If location of key is maximal, we're appending
            $this->_body = $this->_body + $ins;
        } else {
            // Otherwise, insert at location specified
            $pre  = array_slice($this->_body, 0, $loc);
            $post = array_slice($this->_body, $loc);
            $this->_body = $pre + $ins + $post;
        }

        return $this;
    }
public function insert($name, $content, $parent = null, $before = false)
    {
        if (!is_string($name)) {
            throw new \Exception('Invalid body segment key ("' . gettype($name) . '")');
        }

        if ((null !== $parent) && !is_string($parent)) {
            throw new \Exception('Invalid body segment parent key ("' . gettype($parent) . '")');
        }

        if (isset($this->_body[$name])) {
            unset($this->_body[$name]);
        }

        if ((null === $parent) || !isset($this->_body[$parent])) {
            return $this->append($name, $content);
        }

        $ins  = array($name => (string) $content);
        $keys = array_keys($this->_body);
        $loc  = array_search($parent, $keys);
        if (!$before) {
            // Increment location if not inserting before
            ++$loc;
        }

        if (0 === $loc) {
            // If location of key is 0, we're prepending
            $this->_body = $ins + $this->_body;
        } elseif ($loc >= (count($this->_body))) {
            // If location of key is maximal, we're appending
            $this->_body = $this->_body + $ins;
        } else {
            // Otherwise, insert at location specified
            $pre  = array_slice($this->_body, 0, $loc);
            $post = array_slice($this->_body, $loc);
            $this->_body = $pre + $ins + $post;
        }

        return $this;
    }
public function insert ( $ name , $ content , $ parent = null , $ before = false ) { if ( ! is_string ( $ name ) ) { throw new \ Exception ( 'Invalid body segment key ("' . gettype ( $ name ) . '")' ) ; } if ( ( null !== $ parent ) && ! is_string ( $ parent ) ) { throw new \ Exception ( 'Invalid body segment parent key ("' . gettype ( $ parent ) . '")' ) ; } if ( isset ( $ this -> _body [ $ name ] ) ) { unset ( $ this -> _body [ $ name ] ) ; } if ( ( null === $ parent ) || ! isset ( $ this -> _body [ $ parent ] ) ) { return $ this -> append ( $ name , $ content ) ; } $ ins = array ( $ name => ( string ) $ content ) ; $ keys = array_keys ( $ this -> _body ) ; $ loc = array_search ( $ parent , $ keys ) ; if ( ! $ before ) { // Increment location if not inserting before ++ $ loc ; } if ( 0 === $ loc ) { // If location of key is 0, we're prepending $ this -> _body = $ ins + $ this -> _body ; } elseif ( $ loc >= ( count ( $ this -> _body ) ) ) { // If location of key is maximal, we're appending $ this -> _body = $ this -> _body + $ ins ; } else { // Otherwise, insert at location specified $ pre = array_slice ( $ this -> _body , 0 , $ loc ) ; $ post = array_slice ( $ this -> _body , $ loc ) ; $ this -> _body = $ pre + $ ins + $ post ; } return $ this ; }
Insert a named segment into the body content array

@param  string $name
@param  string $content
@param  string $parent
@param  boolean $before Whether to insert the new segment before or
after the parent. Defaults to false (after)
@return \G4\Http\Response
Insert a named segment into the body content array