| Total Complexity | 16 |
| Total Lines | 81 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| 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 | 40 | public function __construct(SimpleXMLElement $node, $parent) |
|
| 45 | { |
||
| 46 | 40 | $this->parent = $parent; |
|
| 47 | 40 | $this->children = new ArrayList(); |
|
| 48 | 40 | $elseIf = []; |
|
| 49 | 40 | foreach ($node->children() as $child) { |
|
| 50 | 40 | switch ($child->getName()) { |
|
| 51 | 40 | case 'if': |
|
| 52 | 40 | $this->children->add("if", new ChooseIf($child, $this)); |
|
| 53 | 40 | break; |
|
| 54 | 39 | case 'else-if': |
|
| 55 | 33 | $elseIf[] = new ChooseElseIf($child, $this); |
|
| 56 | 33 | break; |
|
| 57 | 39 | case 'else': |
|
| 58 | 39 | $this->children->add("else", new ChooseElse($child, $this)); |
|
| 59 | 40 | break; |
|
| 60 | } |
||
| 61 | } |
||
| 62 | 40 | if (!empty($elseIf)) { |
|
| 63 | 33 | $this->children->add("elseif", $elseIf); |
|
| 64 | } |
||
| 65 | 40 | } |
|
| 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 | 34 | public function render($data, $citationNumber = null) |
|
| 73 | { |
||
| 74 | 34 | $arr = []; |
|
| 75 | |||
| 76 | // IF |
||
| 77 | 34 | if ($prevCondition = $this->children->get("if")->match($data)) { |
|
| 78 | 32 | $arr[] = $this->children->get("if")->render($data); |
|
| 79 | |||
| 80 | 32 | } else if (!$prevCondition && $this->children->hasKey("elseif")) { // ELSEIF |
|
| 81 | /** @var ChooseElseIf $child */ |
||
|
3 ignored issues
–
show
|
|||
| 82 | 21 | foreach ($this->children->get("elseif") as $child) { |
|
| 83 | 21 | $condition = $child->match($data); |
|
| 84 | 21 | if ($condition && !$prevCondition) { |
|
| 85 | 12 | $arr[] = $child->render($data); |
|
| 86 | 12 | $prevCondition = true; |
|
| 87 | 12 | break; //break loop as soon as condition matches |
|
| 88 | } |
||
| 89 | 21 | $prevCondition = $condition; |
|
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | //ELSE |
||
| 94 | 34 | if (!$prevCondition && $this->children->hasKey("else")) { |
|
| 95 | 30 | $arr[] = $this->children->get("else")->render($data); |
|
| 96 | |||
| 97 | } |
||
| 98 | 34 | return implode("", $arr); |
|
| 99 | } |
||
| 100 | |||
| 101 | |||
| 102 | /** |
||
|
1 ignored issue
–
show
|
|||
| 103 | * @return mixed |
||
| 104 | */ |
||
| 105 | 5 | public function getParent() |
|
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 |