Open Journal Systems  3.3.0
ChooseIf.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 
21 use SimpleXMLElement;
22 
29 class ChooseIf implements Rendering, HasParent
30 {
34  private $constraints;
35 
39  protected $children;
40 
44  private $match;
45 
49  protected $parent;
56  public function __construct(SimpleXMLElement $node, $parent)
57  {
58  $this->parent = $parent;
59  $this->constraints = new ArrayList();
60  $this->children = new ArrayList();
61  $this->match = (string) $node['match'];
62  if (empty($this->match)) {
64  }
65  foreach ($node->attributes() as $name => $value) {
66  if ('match' !== $name) {
67  $this->constraints->append(Factory::createConstraint((string) $name, (string) $value, $this->match));
68  }
69  }
70  foreach ($node->children() as $child) {
71  $this->children->append(Factory::create($child, $this));
72  }
73  }
79  public function render($data, $citationNumber = null)
80  {
81  $ret = [];
83  foreach ($this->children as $child) {
84  $ret[] = $child->render($data, $citationNumber);
85  }
86  $glue = "";
87  $parent = $this->parent->getParent();
88  if ($parent instanceof Group && $parent->hasDelimiter()) {
89  $glue = $parent->getDelimiter();
90  }
91  return implode($glue, $ret);
92  }
98  public function match($data, $citationNumber = null)
99  {
100  if ($this->constraints->count() === 1) {
101  return $this->constraints->current()->validate($data);
102  }
103  $result = true;
105  foreach ($this->constraints as $constraint) {
106  if ($this->match === "any") {
107  if ($constraint->validate($data, $citationNumber)) {
108  return true;
109  }
110  } else {
111  $result &= $constraint->validate($data, $citationNumber);
112  }
113  }
114  if ($this->constraints->count() > 1 && $this->match === "all") {
115  return (bool) $result;
116  } elseif ($this->match === "none") {
117  return !((bool) $result);
118  }
119  return false;
120  }
121 
122 
127  public function getParent()
128  {
129  return $this->parent;
130  }
131 }
Seboettg\CiteProc\Constraint\Constraint\MATCH_ALL
const MATCH_ALL
Definition: Constraint.php:25
Seboettg\CiteProc\Rendering\Choose
Definition: Choose.php:10
Seboettg\CiteProc\Rendering\Choose\ChooseIf\render
render($data, $citationNumber=null)
Definition: ChooseIf.php:88
Seboettg\CiteProc\Exception\InvalidStylesheetException
Definition: InvalidStylesheetException.php:10
Seboettg\CiteProc\Rendering\HasParent
Definition: HasParent.php:17
Seboettg\CiteProc\Rendering\Choose\ChooseIf\getParent
getParent()
Definition: ChooseIf.php:136
Seboettg\CiteProc\Constraint\Constraint
Definition: Constraint.php:19
Seboettg\CiteProc\Rendering\Group
Definition: Group.php:36
Seboettg\CiteProc\Constraint\Factory
Definition: Constraint/Factory.php:20
Seboettg\CiteProc\Exception\ClassNotFoundException
Definition: ClassNotFoundException.php:22
Seboettg\CiteProc\Rendering\Choose\ChooseIf\__construct
__construct(SimpleXMLElement $node, $parent)
Definition: ChooseIf.php:65
Seboettg\CiteProc\Constraint\Factory\createConstraint
static createConstraint($name, $value, $match)
Definition: Constraint/Factory.php:31
Seboettg\CiteProc\Data\DataList
Definition: DataList.php:22
Seboettg\CiteProc\Rendering\Choose\ChooseIf\$parent
$parent
Definition: ChooseIf.php:58
Seboettg\CiteProc\Rendering\Choose\ChooseIf\$children
$children
Definition: ChooseIf.php:45
Seboettg\CiteProc\Rendering\Rendering
Definition: Rendering.php:22
Seboettg\Collection\ArrayList
Definition: ArrayList.php:20
Seboettg\CiteProc\Util\Factory\create
static create($node, $param=null)
Definition: Util/Factory.php:55
Seboettg\CiteProc\Rendering\Choose\ChooseIf
Definition: ChooseIf.php:29
Seboettg\CiteProc\Rendering\Choose\ChooseIf\match
match($data, $citationNumber=null)
Definition: ChooseIf.php:107