Passed
Push — release/2.5.2 ( bd9268 )
by Sebastian
03:30 queued 01:02
created

ChooseIf::render()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 13
ccs 9
cts 9
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Rendering\Choose;
12
13
use Seboettg\CiteProc\Constraint\Constraint;
14
use Seboettg\CiteProc\Constraint\Factory;
15
use Seboettg\CiteProc\Data\DataList;
16
use Seboettg\CiteProc\Exception\ClassNotFoundException;
17
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
18
use Seboettg\CiteProc\Rendering\Group;
19
use Seboettg\CiteProc\Rendering\HasParent;
20
use Seboettg\CiteProc\Rendering\Rendering;
21
use Seboettg\Collection\ArrayList;
22
use SimpleXMLElement;
23
24
class ChooseIf implements Rendering, HasParent
25
{
26
    /**
27
     * @var ArrayList<Constraint>|Constraint[]
28
     */
29
    private $constraints;
30
31
    /**
32
     * @var ArrayList
33
     */
34
    protected $children;
35
36
    /**
37
     * @var string
38
     */
39
    private $match;
40
41
    /**
42
     * @var
43
     */
44
    protected $parent;
45
    /**
46
     * @param SimpleXMLElement $node
47
     * @param Choose $parent
48
     * @throws InvalidStylesheetException
49
     * @throws ClassNotFoundException
50
     */
51 72
    public function __construct(SimpleXMLElement $node, Choose $parent)
52
    {
53 72
        $this->parent = $parent;
54 72
        $this->constraints = new ArrayList();
55 72
        $this->children = new ArrayList();
56 72
        $this->match = (string) $node['match'];
57 72
        if (empty($this->match)) {
58 71
            $this->match = Constraint::MATCH_ALL;
59
        }
60 72
        foreach ($node->attributes() as $name => $value) {
61 72
            if ('match' !== $name) {
62 72
                $this->constraints->append(Factory::createConstraint((string) $name, (string) $value, $this->match));
63
            }
64
        }
65 72
        foreach ($node->children() as $child) {
66 72
            $this->children->append(Factory::create($child, $this));
67
        }
68 72
    }
69
    /**
70
     * @param array|DataList $data
71
     * @param null|int $citationNumber
72
     * @return string
73
     */
74 65
    public function render($data, $citationNumber = null): string
75
    {
76 65
        $ret = [];
77
        /** @var Rendering $child */
78 65
        foreach ($this->children as $child) {
79 65
            $ret[] = $child->render($data, $citationNumber);
80
        }
81 65
        $glue = "";
82 65
        $parent = $this->parent->getParent();
83 65
        if ($parent instanceof Group && $parent->hasDelimiter()) {
84 28
            $glue = $parent->getDelimiter();
85
        }
86 65
        return implode($glue, array_filter($ret));
87
    }
88
    /**
89
     * @param $data
90
     * @param null|int $citationNumber
91
     * @return bool
92
     */
93 65
    public function match($data, int $citationNumber = null): bool
0 ignored issues
show
Unused Code introduced by
The parameter $citationNumber is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

93
    public function match($data, /** @scrutinizer ignore-unused */ int $citationNumber = null): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95 65
        if ($this->constraints->count() === 1) {
96 64
            return $this->constraints->current()->validate($data);
97
        }
98
99 24
        switch ($this->match) {
100 24
            case Constraint::MATCH_ANY:
101 15
                return $this->constraints
102
                    ->map(function (Constraint $constraint) use ($data) {
103 15
                        return $constraint->validate($data);
104 15
                    })
105
                    ->filter(function (bool $match) {
106 15
                        return $match === true;
107 15
                    })
108 15
                    ->count() > 0;
109 23
            case Constraint::MATCH_ALL:
110 22
                return $this->constraints
111
                    ->map(function (Constraint $constraint) use ($data) {
112 22
                        return $constraint->validate($data);
113 22
                    })
114
                    ->filter(function (bool $match) {
115 22
                        return $match === true;
116 22
                    })
117 22
                    ->count() === $this->constraints->count();
118 5
            case Constraint::MATCH_NONE:
119 5
                return !$this->constraints
120
                    ->map(function (Constraint $constraint) use ($data) {
121 5
                        return $constraint->validate($data);
122 5
                    })
123
                    ->filter(function (bool $match) {
124 5
                        return $match === false;
125 5
                    })
126 5
                    ->count() === $this->constraints->count();
127
        }
128
        return false;
129
    }
130
131
132
    /**
133
     * @noinspection PhpUnused
134
     * @return Choose
135
     */
136
    public function getParent(): Choose
137
    {
138
        return $this->parent;
139
    }
140
}
141