public function add(Column $column, string $after_column = null, string $mode = self::ADD_COLUMN_AFTER): self
    {
        $name = $column->getName();
        if ($this->exists($name)) {
            $msg = "Cannot add column '$name', it's already present in column model";
            throw new Exception\DuplicateColumnException(__METHOD__ . ': ' . $msg);
        }

        if ($after_column !== null) {
            // Test existence of column
            if (!$this->exists($after_column)) {
                $msg = "Cannot add column '$name' after '$after_column', column does not exists.";
                throw new Exception\ColumnNotFoundException(__METHOD__ . ': ' . $msg);
            }

            if (!in_array($mode, [self::ADD_COLUMN_BEFORE, self::ADD_COLUMN_AFTER], true)) {
                $msg = "Cannot add column '$name', invalid mode specified '$mode'";
                throw new Exception\InvalidArgumentException(__METHOD__ . ': ' . $msg);
            }

            $new_columns = new ArrayObject();
            foreach ($this->columns as $key => $col) {
                if ($mode === self::ADD_COLUMN_BEFORE && $key === $after_column) {
                    $new_columns->offsetSet($name, $column);
                }
                $new_columns->offsetSet($key, $col);
                if ($mode === self::ADD_COLUMN_AFTER && $key === $after_column) {
                    $new_columns->offsetSet($name, $column);
                }
            }
            $this->columns->exchangeArray($new_columns);
        } else {
            // Simply append
            $this->columns->offsetSet($name, $column);
        }

        return $this;
    }
public function add(Column $column, string $after_column = null, string $mode = self::ADD_COLUMN_AFTER): self
    {
        $name = $column->getName();
        if ($this->exists($name)) {
            $msg = "Cannot add column '$name', it's already present in column model";
            throw new Exception\DuplicateColumnException(__METHOD__ . ': ' . $msg);
        }

        if ($after_column !== null) {
            // Test existence of column
            if (!$this->exists($after_column)) {
                $msg = "Cannot add column '$name' after '$after_column', column does not exists.";
                throw new Exception\ColumnNotFoundException(__METHOD__ . ': ' . $msg);
            }

            if (!in_array($mode, [self::ADD_COLUMN_BEFORE, self::ADD_COLUMN_AFTER], true)) {
                $msg = "Cannot add column '$name', invalid mode specified '$mode'";
                throw new Exception\InvalidArgumentException(__METHOD__ . ': ' . $msg);
            }

            $new_columns = new ArrayObject();
            foreach ($this->columns as $key => $col) {
                if ($mode === self::ADD_COLUMN_BEFORE && $key === $after_column) {
                    $new_columns->offsetSet($name, $column);
                }
                $new_columns->offsetSet($key, $col);
                if ($mode === self::ADD_COLUMN_AFTER && $key === $after_column) {
                    $new_columns->offsetSet($name, $column);
                }
            }
            $this->columns->exchangeArray($new_columns);
        } else {
            // Simply append
            $this->columns->offsetSet($name, $column);
        }

        return $this;
    }
public function add ( Column $ column , string $ after_column = null , string $ mode = self :: ADD_COLUMN_AFTER ) : self { $ name = $ column -> getName ( ) ; if ( $ this -> exists ( $ name ) ) { $ msg = "Cannot add column '$name', it's already present in column model" ; throw new Exception \ DuplicateColumnException ( __METHOD__ . ': ' . $ msg ) ; } if ( $ after_column !== null ) { // Test existence of column if ( ! $ this -> exists ( $ after_column ) ) { $ msg = "Cannot add column '$name' after '$after_column', column does not exists." ; throw new Exception \ ColumnNotFoundException ( __METHOD__ . ': ' . $ msg ) ; } if ( ! in_array ( $ mode , [ self :: ADD_COLUMN_BEFORE , self :: ADD_COLUMN_AFTER ] , true ) ) { $ msg = "Cannot add column '$name', invalid mode specified '$mode'" ; throw new Exception \ InvalidArgumentException ( __METHOD__ . ': ' . $ msg ) ; } $ new_columns = new ArrayObject ( ) ; foreach ( $ this -> columns as $ key => $ col ) { if ( $ mode === self :: ADD_COLUMN_BEFORE && $ key === $ after_column ) { $ new_columns -> offsetSet ( $ name , $ column ) ; } $ new_columns -> offsetSet ( $ key , $ col ) ; if ( $ mode === self :: ADD_COLUMN_AFTER && $ key === $ after_column ) { $ new_columns -> offsetSet ( $ name , $ column ) ; } } $ this -> columns -> exchangeArray ( $ new_columns ) ; } else { // Simply append $ this -> columns -> offsetSet ( $ name , $ column ) ; } return $ this ; }
Add a new column to the column model.

@throws Exception\InvalidArgumentException when mode is not supported
@throws Exception\DuplicateColumnException when column name already exists
@throws Exception\ColumnNotFoundException  when after_column does not exists

@param string $after_column add the new column after this existing one
@param string $mode         change after to before (see self::ADD_COLUMN_AFTER, self::ADD_COLUMN_BEFORE)
Add a new column to the column model .