| Total Complexity | 34 |
| Total Lines | 150 |
| Duplicated Lines | 0 % |
| Coverage | 96.61% |
| Changes | 0 | ||
| 1 | <?php |
||
| 37 | class Group implements Rendering, HasParent |
||
| 38 | { |
||
| 39 | use DelimiterTrait, |
||
| 40 | AffixesTrait, |
||
| 41 | DisplayTrait, |
||
| 42 | FormattingTrait, |
||
| 43 | ConsecutivePunctuationCharacterTrait; |
||
| 44 | |||
| 45 | /** |
||
|
1 ignored issue
–
show
|
|||
| 46 | * @var ArrayList |
||
| 47 | */ |
||
| 48 | private $children; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * cs:group may carry the delimiter attribute to separate its child elements |
||
| 52 | * @var |
||
|
1 ignored issue
–
show
|
|||
| 53 | */ |
||
| 54 | private $delimiter = ""; |
||
| 55 | |||
| 56 | private $parent; |
||
| 57 | |||
| 58 | /** |
||
|
1 ignored issue
–
show
|
|||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private $renderedChildsWithVariable = []; |
||
| 62 | |||
| 63 | |||
| 64 | /** |
||
| 65 | * Group constructor. |
||
| 66 | * @param SimpleXMLElement $node |
||
|
3 ignored issues
–
show
|
|||
| 67 | * @param $parent |
||
|
2 ignored issues
–
show
|
|||
| 68 | * @throws InvalidStylesheetException |
||
|
1 ignored issue
–
show
|
|||
| 69 | */ |
||
| 70 | 73 | public function __construct(SimpleXMLElement $node, $parent) |
|
| 71 | { |
||
| 72 | 73 | $this->parent = $parent; |
|
| 73 | 73 | $this->children = new ArrayList(); |
|
| 74 | 73 | foreach ($node->children() as $child) { |
|
| 75 | 73 | $this->children->append(Factory::create($child, $this)); |
|
| 76 | } |
||
| 77 | 73 | $this->initDisplayAttributes($node); |
|
| 78 | 73 | $this->initAffixesAttributes($node); |
|
| 79 | 73 | $this->initDelimiterAttributes($node); |
|
| 80 | 73 | $this->initFormattingAttributes($node); |
|
| 81 | 73 | } |
|
| 82 | |||
| 83 | /** |
||
|
1 ignored issue
–
show
|
|||
| 84 | * @param $data |
||
|
2 ignored issues
–
show
|
|||
| 85 | * @param int|null $citationNumber |
||
|
2 ignored issues
–
show
|
|||
| 86 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 87 | */ |
||
| 88 | 66 | public function render($data, $citationNumber = null) |
|
| 89 | { |
||
| 90 | 66 | $textParts = array(); |
|
| 91 | 66 | $terms = $variables = $haveVariables = $elementCount = 0; |
|
| 92 | 66 | foreach ($this->children as $child) { |
|
| 93 | 66 | $elementCount++; |
|
| 94 | |||
| 95 | 66 | if (($child instanceof Text) && |
|
| 96 | 46 | ($child->getSource() == 'term' || |
|
| 97 | 46 | $child->getSource() == 'value')) { |
|
| 98 | 26 | ++$terms; |
|
| 99 | } |
||
| 100 | |||
| 101 | 66 | if (($child instanceof Label)) { |
|
| 102 | 12 | ++$terms; |
|
| 103 | } |
||
| 104 | 66 | if (method_exists($child, "getSource") && $child->getSource() == 'variable' && |
|
| 105 | 39 | !empty($child->getVariable()) && $child->getVariable() != "date" && |
|
| 106 | 39 | !empty($data->{$child->getVariable()}) |
|
| 107 | ) { |
||
| 108 | 29 | ++$variables; |
|
| 109 | } |
||
| 110 | |||
| 111 | /** @var stdClass $data */ |
||
|
3 ignored issues
–
show
|
|||
| 112 | 66 | $text = $child->render($data, $citationNumber); |
|
| 113 | 66 | $delimiter = $this->delimiter; |
|
| 114 | 66 | if (!empty($text)) { |
|
| 115 | 65 | if ($delimiter && ($elementCount < count($this->children))) { |
|
| 116 | //check to see if the delimiter is already the last character of the text string |
||
| 117 | //if so, remove it so we don't have two of them when the group will be merged |
||
| 118 | 58 | $stext = strip_tags(trim($text)); |
|
| 119 | 58 | if ((strrpos($stext, $delimiter[0]) + 1) == strlen($stext) && strlen($stext) > 1) { |
|
| 120 | 12 | $text = str_replace($stext, '----REPLACE----', $text); |
|
| 121 | 12 | $stext = substr($stext, 0, -1); |
|
| 122 | 12 | $text = str_replace('----REPLACE----', $stext, $text); |
|
| 123 | } |
||
| 124 | } |
||
| 125 | 65 | $textParts[] = $text; |
|
| 126 | |||
| 127 | 65 | if (method_exists($child, "getSource") && $child->getSource() == 'variable' || |
|
| 128 | 64 | (method_exists($child, |
|
| 129 | 64 | "getVariable") && $child->getVariable() != "date" && !empty($child->getVariable()))) { |
|
| 130 | |||
| 131 | 54 | $haveVariables++; |
|
| 132 | } |
||
| 133 | |||
| 134 | 65 | if (method_exists($child, "getSource") && $child->getSource() == 'macro') { |
|
| 135 | 29 | $haveVariables++; |
|
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | 66 | return $this->formatting($textParts, $variables, $haveVariables, $terms); |
|
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
|
1 ignored issue
–
show
|
|||
| 143 | * @return mixed |
||
| 144 | */ |
||
| 145 | 10 | public function getParent() |
|
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
|
1 ignored issue
–
show
|
|||
| 151 | * @param $textParts |
||
|
2 ignored issues
–
show
|
|||
| 152 | * @param $variables |
||
|
2 ignored issues
–
show
|
|||
| 153 | * @param $haveVariables |
||
|
2 ignored issues
–
show
|
|||
| 154 | * @param $terms |
||
|
2 ignored issues
–
show
|
|||
| 155 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 156 | */ |
||
| 157 | 66 | protected function formatting($textParts, $variables, $haveVariables, $terms) |
|
| 177 | } |
||
| 178 | |||
| 179 | 17 | public function hasDelimiter() |
|
| 180 | { |
||
| 181 | 17 | return !empty($this->delimiter); |
|
| 182 | } |
||
| 183 | |||
| 184 | 59 | public function getDelimiter() |
|
| 187 | } |
||
| 188 | } |
||
| 189 |