Passed
Push — new-api ( 5677f6...2a03a6 )
by Sebastian
04:12
created

ChooseIf   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 106
ccs 38
cts 40
cp 0.95
rs 10
c 0
b 0
f 0
wmc 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 5
A getParent() 0 3 1
B match() 0 22 8
A render() 0 13 4
A setParent() 0 3 1
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
10
namespace Seboettg\CiteProc\Rendering\Choose;
11
12
use Seboettg\CiteProc\Constraint\Constraint;
13
use Seboettg\CiteProc\Constraint\Factory;
14
use Seboettg\CiteProc\Data\DataList;
15
use Seboettg\CiteProc\Exception\ClassNotFoundException;
16
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
17
use Seboettg\CiteProc\Rendering\Group;
18
use Seboettg\CiteProc\Rendering\HasParent;
19
use Seboettg\CiteProc\Rendering\Rendering;
20
use Seboettg\Collection\ArrayList;
21
use SimpleXMLElement;
22
23
/**
24
 * Class ChooseIf
25
 * @package Seboettg\CiteProc\Node\Choose
26
 *
27
 * @author Sebastian Böttger <[email protected]>
28
 */
29
class ChooseIf implements Rendering, HasParent
30
{
31
    /**
32
     * @var ArrayList<Constraint>
33
     */
34
    private $constraints;
35
36
    /**
37
     * @var ArrayList
38
     */
39
    protected $children;
40
41
    /**
42
     * @var string
43
     */
44
    private $match;
45
46
    /**
47
     * @var
48
     */
49
    protected $parent;
50
    /**
51
     * @param SimpleXMLElement $node
52
     * @param Choose $parent
53
     * @throws InvalidStylesheetException
54
     * @throws ClassNotFoundException
55
     */
56 63
    public function __construct(SimpleXMLElement $node, $parent)
57
    {
58 63
        $this->parent = $parent;
59 63
        $this->constraints = new ArrayList();
60 63
        $this->children = new ArrayList();
61 63
        $this->match = (string) $node['match'];
62 63
        if (empty($this->match)) {
63 62
            $this->match = Constraint::MATCH_ALL;
64
        }
65 63
        foreach ($node->attributes() as $name => $value) {
66 63
            if ('match' !== $name) {
67 63
                $this->constraints->append(Factory::createConstraint((string) $name, (string) $value, $this->match));
68
            }
69
        }
70 63
        foreach ($node->children() as $child) {
71 63
            $this->children->append(Factory::create($child, $this));
72
        }
73 63
    }
74
    /**
75
     * @param array|DataList $data
76
     * @param null|int $citationNumber
77
     * @return string
78
     */
79 56
    public function render($data, $citationNumber = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationNumber" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationNumber"; expected 0 but found 1
Loading history...
80
    {
81 56
        $ret = [];
82
        /** @var Rendering $child */
83 56
        foreach ($this->children as $child) {
84 56
            $ret[] = $child->render($data, $citationNumber);
85
        }
86 56
        $glue = "";
87 56
        $parent = $this->parent->getParent();
88 56
        if ($parent instanceof Group && $parent->hasDelimiter()) {
89 23
            $glue = $parent->getDelimiter();
90
        }
91 56
        return implode($glue, array_filter($ret));
92
    }
93
    /**
94
     * @param $data
95
     * @param null|int $citationNumber
96
     * @return bool
97
     */
98 56
    public function match($data, $citationNumber = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationNumber" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationNumber"; expected 0 but found 1
Loading history...
99
    {
100 56
        if ($this->constraints->count() === 1) {
101 56
            return $this->constraints->current()->validate($data);
102
        }
103 19
        $result = true;
104
        /** @var Constraint $constraint */
105 19
        foreach ($this->constraints as $constraint) {
106 19
            if ($this->match === "any") {
107 11
                if ($constraint->validate($data, $citationNumber)) {
108 11
                    return true;
109
                }
110
            } else {
111 19
                $result &= $constraint->validate($data, $citationNumber);
112
            }
113
        }
114 19
        if ($this->constraints->count() > 1 && $this->match === "all") {
115 18
            return (bool) $result;
116 12
        } elseif ($this->match === "none") {
117 3
            return !((bool) $result);
118
        }
119 11
        return false;
120
    }
121
122
123
    /**
124
     * @noinspection PhpUnused
125
     * @return Choose
126
     */
127 3
    public function getParent()
128
    {
129 3
        return $this->parent;
130
    }
131
132
    public function setParent($parent)
133
    {
134
        $this->parent = $parent;
135
    }
136
}
137