1: <?php
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24: namespace Thelia\Tpex\Element;
25:
26: use Symfony\Component\EventDispatcher\EventDispatcher;
27: use Thelia\Tpex\Tools;
28: use Thelia\Tpex\TpexToken;
29: use Thelia\Tpex\Exception\ElementNotFoundException;
30: use Thelia\Tpex\Exception\InvalidElementException;
31:
32: use Symfony\Component\HttpFoundation\Request;
33: use Symfony\Component\EventDispatcher\EventDispatcherInterface;
34:
35: class TestLoopElement extends BaseElement
36: {
37:
38: public $name;
39: public $args;
40: public $testLoop;
41:
42: protected $request;
43: protected $dispatcher;
44:
45: public function __construct($name, array $testLoop, Request $request, EventDispatcherInterface $dispatcher)
46: {
47: $this->name = $name;
48: $this->content = array();
49: $this->testLoop = $testLoop;
50:
51: $this->request = $request;
52: $this->dispatcher = $dispatcher;
53: }
54:
55: public function setArgs($args)
56: {
57: $this->args = $args;
58: }
59:
60: public function addContent($content)
61: {
62: $this->content[] = $content;
63: }
64:
65: public function exec($substitutions = array())
66: {
67: $args = $this->replace($substitutions, $this->args);
68:
69: if ($this->execLoop($args)) {
70: return $this->content[0]->exec($substitutions);
71: } else {
72: return $this->content[1]->exec($substitutions);
73: }
74:
75: }
76:
77: public function execLoop($args)
78: {
79: $test = Tools::extractValueParam("test", $args);
80:
81: $loop = $this->constructLoop($test);
82:
83: return $loop->exec(Tools::extractValueParam("variable", $args), Tools::extractValueParam("value", $args));
84: }
85:
86: public function constructLoop($name)
87: {
88: if (!isset($this->testLoop[$name])) {
89: throw new ElementNotFoundException(sprintf("%s test loop does not exists", $name));
90: }
91:
92: $class = new \ReflectionClass($this->testLoop[$name]);
93:
94: if ($class->isSubclassOf("Thelia\Tpex\Element\TestLoop\BaseTestLoop") === false) {
95: throw new InvalidElementException(sprintf("%s Loop class have to extends Thelia\Tpex\Element\TestLoop\BaseTestLoop",
96: $name));
97: }
98:
99: return $class->newInstance($this->request, $this->dispatcher);
100: }
101:
102: public function type()
103: {
104: return TpexToken::TPEX_TEST_LOOP;
105: }
106: }
107: