vendor/hsi/sg-datatablesbundle/Sg/DatatablesBundle/Datatable/Column/ColumnBuilder.php line 78

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the SgDatatablesBundle package.
  4.  *
  5.  * (c) stwe <https://github.com/stwe/DatatablesBundle>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Sg\DatatablesBundle\Datatable\Column;
  11. use Exception;
  12. /**
  13.  * Class ColumnBuilder
  14.  *
  15.  * @package Sg\DatatablesBundle\Datatable\Column
  16.  */
  17. class ColumnBuilder implements ColumnBuilderInterface
  18. {
  19.     /**
  20.      * All columns.
  21.      *
  22.      * @var array
  23.      */
  24.     private $columns;
  25.     /**
  26.      * The multiselect column.
  27.      *
  28.      * @var ColumnInterface
  29.      */
  30.     private $multiselectColumn;
  31.     /**
  32.      * Multiselect column flag.
  33.      *
  34.      * @var boolean
  35.      */
  36.     private $multiselect;
  37.     /**
  38.      * Name of datatable view.
  39.      *
  40.      * @var string
  41.      */
  42.     private $tableName;
  43.     //-------------------------------------------------
  44.     // Ctor.
  45.     //-------------------------------------------------
  46.     /**
  47.      * Ctor.
  48.      *
  49.      * @param string $tableName
  50.      */
  51.     public function __construct($tableName)
  52.     {
  53.         $this->columns = array();
  54.         $this->multiselectColumn = null;
  55.         $this->multiselect = false;
  56.         $this->tableName = $tableName;
  57.     }
  58.     //-------------------------------------------------
  59.     // ColumnBuilderInterface
  60.     //-------------------------------------------------
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function add($data, $alias, array $options = array())
  65.     {
  66.         // Support embeddables forcing the two backslashes in the column name
  67.         if (strpos($data, '\\') !== false) {
  68.             $data = str_replace('\\', '\\\\', $data);
  69.         }
  70.         /**
  71.          * @var AbstractColumn $column
  72.          */
  73.         $column = ColumnFactory::createColumnByAlias($alias);
  74.         $column->setTableName($this->tableName);
  75.         $column->setData($data);
  76.         $column->setDql($data);
  77.         $column->setupOptionsResolver($options);
  78.         $addColumn = $column->isAddIfClosure();
  79.         if (true === $addColumn) {
  80.             $column->setIndex(count($this->columns));
  81.             $this->columns[] = $column;
  82.         }
  83.         if (true === $addColumn && $column instanceof MultiselectColumn) {
  84.             if (false === $this->multiselect) {
  85.                 $this->multiselect = true;
  86.                 $this->multiselectColumn = $column;
  87.             } else {
  88.                 throw new Exception('add(): There is only one multiselect column allowed.');
  89.             }
  90.         }
  91.         return $this;
  92.     }
  93.     /**
  94.      * {@inheritdoc}
  95.      */
  96.     public function removeByKey($key)
  97.     {
  98.         if (is_int($key) && array_key_exists($key, $this->columns)) {
  99.             unset($this->columns[$key]);
  100.         } else {
  101.             throw new Exception('removeColumnByKey(): The array key ' . $key . ' does not exist.');
  102.         }
  103.         $this->columns = array_values($this->columns);
  104.         return $this;
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     public function removeByData($data)
  110.     {
  111.         if (is_string($data)) {
  112.             foreach ($this->columns as $key => $column) {
  113.                 /** @var ColumnInterface $column */
  114.                 if ($data === $column->getDql()) {
  115.                     unset($this->columns[$key]);
  116.                     $this->columns = array_values($this->columns);
  117.                     return $this;
  118.                 }
  119.             }
  120.             throw new Exception('removeColumnByData(): The column with data ' . $data . ' does not exist.');
  121.         }
  122.         return $this;
  123.     }
  124.     /**
  125.      * {@inheritdoc}
  126.      */
  127.     public function getColumns()
  128.     {
  129.         return $this->columns;
  130.     }
  131.     /**
  132.      * {@inheritdoc}
  133.      */
  134.     public function getVirtualColumns()
  135.     {
  136.         $virtualColumns = array();
  137.         foreach ($this->columns as $column) {
  138.             if ($column instanceof VirtualColumn) {
  139.                 $virtualColumns[] = $column->getData();
  140.             }
  141.         }
  142.         return $virtualColumns;
  143.     }
  144.     //-------------------------------------------------
  145.     // Getters && Setters
  146.     //-------------------------------------------------
  147.     /**
  148.      * Get multiselect column.
  149.      *
  150.      * @return ColumnInterface
  151.      */
  152.     public function getMultiselectColumn()
  153.     {
  154.         return $this->multiselectColumn;
  155.     }
  156.     /**
  157.      * Is multiselect.
  158.      *
  159.      * @return boolean
  160.      */
  161.     public function isMultiselect()
  162.     {
  163.         return (boolean) $this->multiselect;
  164.     }
  165. }