     1: <?php
     2: /*************************************************************************************/
     3: /*                                                                                   */
     4: /*      Thelia                                                                       */
     5: /*                                                                                   */
     6: /*      Copyright (c) OpenStudio                                                     */
     7: /*      email : info@thelia.net                                                      */
     8: /*      web : http://www.thelia.net                                                  */
     9: /*                                                                                   */
    10: /*      This program is free software; you can redistribute it and/or modify         */
    11: /*      it under the terms of the GNU General Public License as published by         */
    12: /*      the Free Software Foundation; either version 3 of the License                */
    13: /*                                                                                   */
    14: /*      This program is distributed in the hope that it will be useful,              */
    15: /*      but WITHOUT ANY WARRANTY; without even the implied warranty of               */
    16: /*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
    17: /*      GNU General Public License for more details.                                 */
    18: /*                                                                                   */
    19: /*      You should have received a copy of the GNU General Public License            */
    20: /*      along with this program. If not, see <http://www.gnu.org/licenses/>.         */
    21: /*                                                                                   */
    22: /*************************************************************************************/
    23:
    24: namespace Thelia\Tpex\Element;
    25:
    26: use Thelia\Tpex\Tools;
    27: use Thelia\Tpex\TpexToken;
    28: use Thelia\Tpex\Exception\TpexException;
    29:
    30: class RepeatLoopElement extends BaseElement
    31: {
    32:
    33:     public $name;
    34:     public $args;
    35:
    36:     public function __construct($name)
    37:     {
    38:         $this->name = $name;
    39:     }
    40:
    41:     public function addContent($content)
    42:     {
    43:         $this->content = $content;
    44:     }
    45:
    46:     public function setArgs($args)
    47:     {
    48:         $this->args = $args;
    49:     }
    50:
    51:     public function exec($substitutions = array())
    52:     {
    53:         $val = '';
    54:         $args = $this->replace($substitutions, $this->args);
    55:
    56:         $start = Tools::extractValueParam("start", $args);
    57:         $end = Tools::extractValueParam("end", $args);
    58:         $increment = Tools::extractValueParam("increment", $args);
    59:
    60:         if($start == '') $start = 1;
    61:         if($increment == '') $increment = 1;
    62:
    63:         if ($increment <= 0) {
    64:             throw new TpexException(sprintf("increment must be upper to 0 in %s repeat loop", $this->name));
    65:         } elseif ($increment > 0 && $start > $end) {
    66:             throw new TpexException(sprintf("start parameter must be greater than end parameter in %s repeat loop",
    67:                 $this->name));
    68:         }
    69:
    70:         for ($idx = $start, $count = 1; $idx <= $end; $idx += $increment, $count++) {
    71:             $substitutions["#INDEX"] = $idx;
    72:             $substitutions["#__COUNT__"] = $count;
    73:
    74:             $val .= $this->content->exec($substitutions);
    75:         }
    76:
    77:         return $val;
    78:     }
    79:
    80:     public function type()
    81:     {
    82:         return TpexToken::TPEX_REPEAT_LOOP;
    83:     }
    84: }
    85: