vendor/codefog/contao-haste/library/Haste/Generator/RowClass.php line 101

Open in your IDE?
  1. <?php
  2. /**
  3.  * Haste utilities for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2012-2013 Codefog & terminal42 gmbh
  6.  *
  7.  * @package    Haste
  8.  * @link       http://github.com/codefog/contao-haste/
  9.  * @license    http://opensource.org/licenses/lgpl-3.0.html LGPL
  10.  */
  11. namespace Haste\Generator;
  12. use Contao\FormPassword;
  13. class RowClass
  14. {
  15.     /**
  16.      * Class generator options
  17.      */
  18.     const NAME      1;
  19.     const KEY       2;
  20.     const COUNT     4;
  21.     const EVENODD   8;
  22.     const FIRSTLAST 16;
  23.     const ROW       32;
  24.     const COL       64;
  25.     protected $strKey;
  26.     protected $intColumns 0;
  27.     protected $intOptions 0;
  28.     protected $strName;
  29.     protected $strKeyPrefix;
  30.     protected $strCountPrefix;
  31.     protected $strEvenOddPrefix;
  32.     protected $strFirstLastPrefix;
  33.     protected $strRowPrefix;
  34.     protected $strColPrefix;
  35.     protected function __construct($strKey)
  36.     {
  37.         $this->strKey $strKey;
  38.     }
  39.     public function addCustom($strName)
  40.     {
  41.         if ($strName == '') {
  42.             throw new \InvalidArgumentException('Name is missing');
  43.         }
  44.         $this->strName standardize((string) $strName);
  45.         $this->intOptions $this->intOptions | static::NAME;
  46.         return $this;
  47.     }
  48.     public function addArrayKey($strPrefix='')
  49.     {
  50.         $this->strKeyPrefix standardize((string) $strPrefix);
  51.         $this->intOptions $this->intOptions | static::KEY;
  52.         return $this;
  53.     }
  54.     public function addCount($strPrefix)
  55.     {
  56.         if ($strPrefix == '') {
  57.             throw new \InvalidArgumentException('Prefix is missing');
  58.         }
  59.         $this->strCountPrefix standardize((string) $strPrefix);
  60.         $this->intOptions $this->intOptions | static::COUNT;
  61.         return $this;
  62.     }
  63.     public function addEvenOdd($strPrefix='')
  64.     {
  65.         $this->strEvenOddPrefix standardize((string) $strPrefix);
  66.         $this->intOptions $this->intOptions | static::EVENODD;
  67.         return $this;
  68.     }
  69.     public function addFirstLast($strPrefix='')
  70.     {
  71.         $this->strFirstLastPrefix standardize((string) $strPrefix);
  72.         $this->intOptions $this->intOptions | static::FIRSTLAST;
  73.         return $this;
  74.     }
  75.     public function addGridRows($intPerColumn)
  76.     {
  77.         $this->intColumns = (int) $intPerColumn;
  78.         $this->intOptions $this->intOptions | static::ROW;
  79.         return $this;
  80.     }
  81.     public function addGridCols($intPerColumn)
  82.     {
  83.         $this->intColumns = (int) $intPerColumn;
  84.         $this->intOptions $this->intOptions | static::COL;
  85.         return $this;
  86.     }
  87.     /**
  88.      * Generate row class for an array
  89.      *
  90.      * @param array $arrData data rows
  91.      *
  92.      * @return array
  93.      */
  94.     public function applyTo(array &$arrData)
  95.     {
  96.         $hasColumns = ($this->intColumns 1);
  97.         $total count($arrData) - 1;
  98.         $current 0;
  99.         $row 0;
  100.         $col 0;
  101.         $rows 0;
  102.         $cols 0;
  103.         if ($hasColumns)
  104.         {
  105.             $rows ceil(count($arrData) / $this->intColumns) - 1;
  106.             $cols $this->intColumns 1;
  107.         }
  108.         /** @type mixed $varValue */
  109.         foreach ($arrData as $k => $varValue)
  110.         {
  111.             if ($hasColumns && $current && $current $this->intColumns == 0)
  112.             {
  113.                 ++$row;
  114.                 $col 0;
  115.             }
  116.             // Increase total before generating class to prevent "last" on the first input field
  117.             if (!$hasColumns && $varValue instanceof FormPassword) {
  118.                 ++$total;
  119.             }
  120.             $class $this->generateClass($current$total$k$hasColumns$row$col$rows$cols);
  121.             if (is_array($varValue))
  122.             {
  123.                 $arrData[$k][$this->strKey] = trim(($arrData[$k][$this->strKey] ?? '') . $class);
  124.             }
  125.             elseif (is_object($varValue))
  126.             {
  127.                 // Generate class on confirmation field
  128.                 if (!$hasColumns && $varValue instanceof FormPassword) {
  129.                     ++$current;
  130.                     $varValue->rowClassConfirm $this->generateClass($current$total$k);
  131.                 }
  132.                 $varValue->{$this->strKey} = trim($varValue->{$this->strKey} . $class);
  133.                 $arrData[$k] = $varValue;
  134.             }
  135.             else
  136.             {
  137.                 $arrData[$k] = '<span class="' trim($class) . '">' $varValue '</span>';
  138.             }
  139.             ++$col;
  140.             ++$current;
  141.         }
  142.         return $this;
  143.     }
  144.     /**
  145.      * Generate CSS class
  146.      * @param   int
  147.      * @param   int
  148.      * @param   string
  149.      * @param   bool
  150.      * @param   int
  151.      * @param   int
  152.      * @param   int
  153.      * @param   int
  154.      * @return  string
  155.      */
  156.     protected function generateClass($current$total$k$hasColumns=false$row=0$col=0$rows=0$cols=0)
  157.     {
  158.         $class '';
  159.         if ($this->intOptions & static::NAME)
  160.         {
  161.             $class .= ' ' $this->strName;
  162.         }
  163.         if ($this->intOptions & static::KEY)
  164.         {
  165.             $class .= ' ' $this->strKeyPrefix $k;
  166.         }
  167.         if ($this->intOptions & static::COUNT)
  168.         {
  169.             $class .= ' ' $this->strCountPrefix $current;
  170.         }
  171.         if ($this->intOptions & static::EVENODD)
  172.         {
  173.             $class .= ' ' $this->strEvenOddPrefix . ($current%'odd' 'even');
  174.         }
  175.         if ($this->intOptions & static::FIRSTLAST)
  176.         {
  177.             $class .= ($current == ' ' $this->strFirstLastPrefix 'first' '') . ($current == $total ' ' $this->strFirstLastPrefix 'last' '');
  178.         }
  179.         if ($hasColumns && $this->intOptions & static::ROW)
  180.         {
  181.             $class .= ' row_'.$row . ($row%' row_odd' ' row_even') . ($row == ' row_first' '') . ($row == $rows ' row_last' '');
  182.         }
  183.         if ($hasColumns && $this->intOptions & static::COL)
  184.         {
  185.             $class .= ' col_'.$col . ($col%' col_odd' ' col_even') . ($col == ' col_first' '') . ($col == $cols ' col_last' '');
  186.         }
  187.         return $class;
  188.     }
  189.     public static function withKey($strKey)
  190.     {
  191.         if ($strKey == '') {
  192.             throw new \InvalidArgumentException('Key is missing');
  193.         }
  194.         return new static($strKey);
  195.     }
  196. }