| Total Complexity | 16 |
| Total Lines | 81 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
| 1 | <?php |
||
| 27 | class Choose implements Rendering, HasParent |
||
| 28 | { |
||
| 29 | |||
| 30 | /** |
||
|
1 ignored issue
–
show
|
|||
| 31 | * @var ArrayList |
||
| 32 | */ |
||
| 33 | private $children; |
||
| 34 | |||
| 35 | private $parent; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Choose constructor. |
||
| 39 | * @param SimpleXMLElement $node |
||
|
3 ignored issues
–
show
|
|||
| 40 | * @param $parent |
||
|
1 ignored issue
–
show
|
|||
| 41 | * @throws ClassNotFoundException |
||
|
1 ignored issue
–
show
|
|||
| 42 | * @throws InvalidStylesheetException |
||
|
1 ignored issue
–
show
|
|||
| 43 | */ |
||
| 44 | public function __construct(SimpleXMLElement $node, $parent) |
||
| 45 | { |
||
| 46 | $this->parent = $parent; |
||
| 47 | $this->children = new ArrayList(); |
||
| 48 | $elseIf = []; |
||
| 49 | foreach ($node->children() as $child) { |
||
| 50 | switch ($child->getName()) { |
||
| 51 | case 'if': |
||
| 52 | $this->children->add("if", new ChooseIf($child, $this)); |
||
| 53 | break; |
||
| 54 | case 'else-if': |
||
| 55 | $elseIf[] = new ChooseElseIf($child, $this); |
||
| 56 | break; |
||
| 57 | case 'else': |
||
| 58 | $this->children->add("else", new ChooseElse($child, $this)); |
||
| 59 | break; |
||
| 60 | } |
||
| 61 | } |
||
| 62 | if (!empty($elseIf)) { |
||
| 63 | $this->children->add("elseif", $elseIf); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
|
1 ignored issue
–
show
|
|||
| 68 | * @param array|DataList $data |
||
|
2 ignored issues
–
show
|
|||
| 69 | * @param null|int $citationNumber |
||
|
3 ignored issues
–
show
|
|||
| 70 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 71 | */ |
||
| 72 | public function render($data, $citationNumber = null) |
||
| 73 | { |
||
| 74 | $arr = []; |
||
| 75 | |||
| 76 | // IF |
||
| 77 | if ($prevCondition = $this->children->get("if")->match($data)) { |
||
| 78 | $arr[] = $this->children->get("if")->render($data); |
||
| 79 | |||
| 80 | } else if (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF |
||
| 81 | /** @var ChooseElseIf $child */ |
||
|
3 ignored issues
–
show
|
|||
| 82 | foreach ($this->children->get("elseif") as $child) { |
||
| 83 | $condition = $child->match($data); |
||
| 84 | if ($condition && !$prevCondition) { |
||
| 85 | $arr[] = $child->render($data); |
||
| 86 | $prevCondition = true; |
||
| 87 | break; //break loop as soon as condition matches |
||
| 88 | } |
||
| 89 | $prevCondition = $condition; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | //ELSE |
||
| 94 | if (!$prevCondition && $this->children->hasKey("else")) { |
||
| 95 | $arr[] = $this->children->get("else")->render($data); |
||
| 96 | |||
| 97 | } |
||
| 98 | return implode("", $arr); |
||
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
|
1 ignored issue
–
show
|
|||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | public function getParent() |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 |