vendor/sg/datatablesbundle/Response/DatatableFormatter.php line 87

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\Response;
  11. use Doctrine\ORM\Tools\Pagination\Paginator;
  12. use Sg\DatatablesBundle\Datatable\Column\ColumnInterface;
  13. use Sg\DatatablesBundle\Datatable\DatatableInterface;
  14. use Symfony\Component\PropertyAccess\PropertyAccess;
  15. use Symfony\Component\PropertyAccess\PropertyAccessor;
  16. class DatatableFormatter
  17. {
  18.     /**
  19.      * The output array.
  20.      *
  21.      * @var array
  22.      */
  23.     private $output;
  24.     /**
  25.      * The PropertyAccessor.
  26.      * Provides functions to read and write from/to an object or array using a simple string notation.
  27.      *
  28.      * @var PropertyAccessor
  29.      */
  30.     private $accessor;
  31.     public function __construct()
  32.     {
  33.         $this->output = ['data' => []];
  34.         $this->accessor = PropertyAccess::createPropertyAccessor();
  35.     }
  36.     //-------------------------------------------------
  37.     // Formatter
  38.     //-------------------------------------------------
  39.     /**
  40.      * Create the output array.
  41.      */
  42.     public function runFormatter(Paginator $paginator, DatatableInterface $datatable)
  43.     {
  44.         $lineFormatter = $datatable->getLineFormatter();
  45.         $columns = $datatable->getColumnBuilder()->getColumns();
  46.         foreach ($paginator as $row) {
  47.             // Adding custom DQL fields make PARTIAL columns stored in key 0
  48.             if (isset($row[0])) {
  49.                 $row = array_merge($row, $row[0]);
  50.                 unset($row[0]);
  51.             }
  52.             // Format custom DQL fields output ('custom.dql.name' => $row['custom']['dql']['name'] = 'value')
  53.             foreach ($columns as $column) {
  54.                 // @noinspection PhpUndefinedMethodInspection
  55.                 if (true === $column->isCustomDql()) {
  56.                     /** @noinspection PhpUndefinedMethodInspection */
  57.                     $columnAlias = str_replace('.', '_', $column->getData());
  58.                     /** @noinspection PhpUndefinedMethodInspection */
  59.                     $columnPath = '['.str_replace('.', '][', $column->getData()).']';
  60.                     // @noinspection PhpUndefinedMethodInspection
  61.                     if ($columnAlias !== $column->getData()) {
  62.                         $this->accessor->setValue($row, $columnPath, $row[$columnAlias]);
  63.                         unset($row[$columnAlias]);
  64.                     }
  65.                 }
  66.             }
  67.             // 1. Set (if necessary) the custom data source for the Columns with a 'data' option
  68.             foreach ($columns as $column) {
  69.                 /** @noinspection PhpUndefinedMethodInspection */
  70.                 $dql = $column->getDql();
  71.                 /** @noinspection PhpUndefinedMethodInspection */
  72.                 $data = $column->getData();
  73.                 // @noinspection PhpUndefinedMethodInspection
  74.                 if (false === $column->isAssociation()) {
  75.                     if (null !== $dql && $dql !== $data && false === \array_key_exists($data, $row)) {
  76.                         $row[$data] = $row[$dql];
  77.                         unset($row[$dql]);
  78.                     }
  79.                 }
  80.             }
  81.             // 2. Call the the lineFormatter to format row items
  82.             if (null !== $lineFormatter && \is_callable($lineFormatter)) {
  83.                 $row = \call_user_func($datatable->getLineFormatter(), $row);
  84.             }
  85.             /** @var ColumnInterface $column */
  86.             foreach ($columns as $column) {
  87.                 // 3. Add some special data to the output array. For example, the visibility of actions.
  88.                 $column->addDataToOutputArray($row);
  89.                 // 4. Call Columns renderContent method to format row items (e.g. for images or boolean values)
  90.                 $column->renderCellContent($row);
  91.             }
  92.             foreach ($columns as $column) {
  93.                 if (! $column->getSentInResponse()) {
  94.                     unset($row[$column->getDql()]);
  95.                 }
  96.             }
  97.             $this->output['data'][] = $row;
  98.         }
  99.     }
  100.     //-------------------------------------------------
  101.     // Getters && Setters
  102.     //-------------------------------------------------
  103.     /**
  104.      * @return array
  105.      */
  106.     public function getOutput()
  107.     {
  108.         return $this->output;
  109.     }
  110. }