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