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