Passed
Push — master ( 279db1...661b73 )
by Sebastian
18:02 queued 11:37
created

ChooseIf::getParent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/*
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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\ConstraintInterface;
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\HasParent;
18
use Seboettg\CiteProc\Rendering\Rendering;
19
use Seboettg\Collection\ArrayList;
20
use SimpleXMLElement;
21
22
23
/**
24
 * Class ChooseIf
25
 * @package Seboettg\CiteProc\Node\Choose
1 ignored issue
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
26
 *
27
 * @author Sebastian Böttger <[email protected]>
28
 */
3 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
29
class ChooseIf implements Rendering, HasParent
30
{
31
32
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
     * @var ArrayList<ConstraintInterface>
34
     */
35
    private $constraints;
0 ignored issues
show
Coding Style introduced by
Private member variable "constraints" must be prefixed with an underscore
Loading history...
36
37
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @var ConstraintInterface
39
     */
40
    private $constraint;
0 ignored issues
show
Coding Style introduced by
Private member variable "constraint" must be prefixed with an underscore
Loading history...
41
42
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
43
     * @var ArrayList
44
     */
45
    protected $children;
46
47
    private $match;
0 ignored issues
show
Coding Style introduced by
Private member variable "match" must be prefixed with an underscore
Loading history...
48
49
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
50
     * @var
51
     */
52
    protected $parent;
53
54
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
55
     * @param SimpleXMLElement $node
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
56
     * @param Choose $parent
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 11 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
57
     * @throws InvalidStylesheetException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
58
     * @throws ClassNotFoundException
1 ignored issue
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
59
     */
60
    public function __construct(SimpleXMLElement $node, $parent)
61
    {
62
        $this->parent = $parent;
63
        $this->constraints = new ArrayList();
64
        $this->children = new ArrayList();
65
        $this->match = (string) $node['match'];
66
67
        if (empty($this->match)) {
68
            $this->match = "all";
69
        }
70
71
        foreach ($node->attributes() as $name => $value) {
72
            if ('match' !== $name) {
73
                $this->constraints->append(Factory::createConstraint((string) $name, (string) $value));
74
            }
75
        }
76
77
        foreach ($node->children() as $child) {
78
            $this->children->append(Factory::create($child, $this));
79
        }
80
    }
81
82
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
83
     * @param array|DataList $data
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
84
     * @param null|int $citationNumber
3 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 7 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
85
     * @return string
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
86
     */
87
    public function render($data, $citationNumber = null)
88
    {
89
        $ret = [];
90
        /** @var Rendering $child */
3 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
91
        foreach ($this->children as $child) {
92
            $ret[] = $child->render($data, $citationNumber);
93
        }
94
        return implode("", $ret);
95
    }
96
97
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $data should have a doc-comment as per coding-style.
Loading history...
98
     * @param $data
1 ignored issue
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
99
     * @param null|int $citationNumber
2 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
100
     * @return bool
1 ignored issue
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
101
     */
102
    public function match($data, $citationNumber = null)
103
    {
104
        if (isset($this->constraint)) {
105
            return $this->constraint->validate($data);
106
        }
107
108
        $result = true;
109
110
        /** @var ConstraintInterface $constraint */
3 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
111
        foreach ($this->constraints as $constraint) {
112
            if ($this->match === "any") {
113
                if ($constraint->validate($data, $citationNumber)) {
114
                    return true;
115
                }
116
            } else {
117
                $result &= $constraint->validate($data, $citationNumber);
118
            }
119
        }
120
121
        if ($this->match === "all") {
122
            return (bool) $result;
123
        } else if ($this->match === "none") {
124
            return !$result;
125
        }
126
        return false;
127
    }
128
129
    /**
1 ignored issue
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
130
     * @return Choose
131
     */
132
    public function getParent()
133
    {
134
        return $this->parent;
135
    }
136
137
}
138