Open Journal Systems  3.3.0
Group.php
1 <?php
2 /*
3  * citeproc-php
4  *
5  * @link http://github.com/seboettg/citeproc-php for the source repository
6  * @copyright Copyright (c) 2016 Sebastian Böttger.
7  * @license https://opensource.org/licenses/MIT
8  */
9 
11 
15 use Seboettg\CiteProc\Styles\DelimiterTrait;
21 use SimpleXMLElement;
22 
36 class Group implements Rendering, HasParent
37 {
38  use DelimiterTrait,
39  AffixesTrait,
40  DisplayTrait,
41  FormattingTrait,
42  ConsecutivePunctuationCharacterTrait;
43 
47  private $children;
48 
54  private $delimiter = "";
55 
56  private $parent;
57 
61  private $renderedChildsWithVariable = [];
62 
63 
71  public function __construct(SimpleXMLElement $node, $parent)
72  {
73  $this->parent = $parent;
74  $this->children = new ArrayList();
75  foreach ($node->children() as $child) {
76  $this->children->append(Factory::create($child, $this));
77  }
78  $this->initDisplayAttributes($node);
79  $this->initAffixesAttributes($node);
80  $this->initDelimiterAttributes($node);
81  $this->initFormattingAttributes($node);
82  }
83 
89  public function render($data, $citationNumber = null)
90  {
91  $textParts = [];
92  $terms = $variables = $haveVariables = $elementCount = 0;
93  foreach ($this->children as $child) {
94  $elementCount++;
95 
96  if (($child instanceof Text)
97  && ($child->getSource() == 'term'
98  || $child->getSource() == 'value')
99  ) {
100  ++$terms;
101  }
102 
103  if (($child instanceof Label)) {
104  ++$terms;
105  }
106  if (method_exists($child, "getSource") && $child->getSource() == 'variable'
107  && !empty($child->getVariable()) && $child->getVariable() != "date"
108  && !empty($data->{$child->getVariable()})
109  ) {
110  ++$variables;
111  }
112 
113  $text = $child->render($data, $citationNumber);
114  $delimiter = $this->delimiter;
115  if (!empty($text)) {
116  if ($delimiter && ($elementCount < count($this->children))) {
117  //check to see if the delimiter is already the last character of the text string
118  //if so, remove it so we don't have two of them when the group will be merged
119  $stext = strip_tags(trim($text));
120  if ((strrpos($stext, $delimiter[0]) + 1) == strlen($stext) && strlen($stext) > 1) {
121  $text = str_replace($stext, '----REPLACE----', $text);
122  $stext = substr($stext, 0, -1);
123  $text = str_replace('----REPLACE----', $stext, $text);
124  }
125  }
126  $textParts[] = $text;
127 
128  if (method_exists($child, "getSource") && $child->getSource() == 'variable'
129  || (method_exists(
130  $child,
131  "getVariable"
132  ) && $child->getVariable() != "date" && !empty($child->getVariable()))
133  ) {
134  $haveVariables++;
135  }
136 
137  if (method_exists($child, "getSource") && $child->getSource() == 'macro') {
138  $haveVariables++;
139  }
140  }
141  }
142  return $this->formatting($textParts, $variables, $haveVariables, $terms);
143  }
144 
148  public function getParent()
149  {
150  return $this->parent;
151  }
152 
160  protected function formatting($textParts, $variables, $haveVariables, $terms)
161  {
162  if (empty($textParts)) {
163  return "";
164  }
165 
166  if ($variables && !$haveVariables) {
167  return ""; // there has to be at least one other none empty value before the term is output
168  }
169 
170  if (count($textParts) == $terms) {
171  return ""; // there has to be at least one other none empty value before the term is output
172  }
173 
174  $text = StringHelper::implodeAndPreventConsecutiveChars($this->delimiter, $textParts);
175 
176  if (!empty($text)) {
177  return $this->wrapDisplayBlock($this->addAffixes($this->format(($text))));
178  }
179 
180  return "";
181  }
182 
186  public function hasDelimiter()
187  {
188  return !empty($this->delimiter);
189  }
190 
194  public function getDelimiter()
195  {
196  return $this->delimiter;
197  }
198 }
Seboettg\CiteProc\Util\StringHelper\implodeAndPreventConsecutiveChars
static implodeAndPreventConsecutiveChars($delimiter, $arrayOfStrings)
Definition: StringHelper.php:159
Seboettg\CiteProc\Styles\DisplayTrait
trait DisplayTrait
Definition: DisplayTrait.php:20
Seboettg\CiteProc\Styles\AffixesTrait
trait AffixesTrait
Definition: AffixesTrait.php:21
Seboettg\CiteProc\Styles\FormattingTrait
trait FormattingTrait
Definition: FormattingTrait.php:21
Seboettg\CiteProc\Rendering\Group\formatting
formatting($textParts, $variables, $haveVariables, $terms)
Definition: Group.php:165
Seboettg\CiteProc\Exception\InvalidStylesheetException
Definition: InvalidStylesheetException.php:10
Seboettg\CiteProc\Rendering\HasParent
Definition: HasParent.php:17
Seboettg\CiteProc\Rendering\Group
Definition: Group.php:36
Seboettg\CiteProc\Util\StringHelper
Definition: StringHelper.php:22
Seboettg\CiteProc\Rendering\Group\getParent
getParent()
Definition: Group.php:153
Seboettg\CiteProc\Rendering\Group\hasDelimiter
hasDelimiter()
Definition: Group.php:191
Seboettg\CiteProc\Rendering
Seboettg\CiteProc\Rendering\Group\render
render($data, $citationNumber=null)
Definition: Group.php:94
Seboettg\CiteProc\Rendering\Label
Definition: Label.php:25
Seboettg\CiteProc\Rendering\Group\getDelimiter
getDelimiter()
Definition: Group.php:199
Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait
trait ConsecutivePunctuationCharacterTrait
Definition: ConsecutivePunctuationCharacterTrait.php:18
Seboettg\CiteProc\Rendering\Text
Definition: Text.php:35
Seboettg\CiteProc\Rendering\Group\__construct
__construct(SimpleXMLElement $node, $parent)
Definition: Group.php:76
Seboettg\CiteProc\Rendering\Rendering
Definition: Rendering.php:22
Seboettg\Collection\ArrayList
Definition: ArrayList.php:20
Seboettg\CiteProc\Util\Factory
Definition: Util/Factory.php:21
Seboettg\CiteProc\Util\Factory\create
static create($node, $param=null)
Definition: Util/Factory.php:55