Passed
Push — develop ( e162a8...209be3 )
by Sebastian
14:13 queued 09:03
created

ChooseIf   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Test Coverage

Coverage 98%

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 115
ccs 49
cts 50
cp 0.98
rs 10
c 0
b 0
f 0
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 5
A getParent() 0 3 1
A match() 0 36 5
A render() 0 13 4
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 69
    public function __construct(SimpleXMLElement $node, Choose $parent)
52
    {
53 69
        $this->parent = $parent;
54 69
        $this->constraints = new ArrayList();
55 69
        $this->children = new ArrayList();
56 69
        $this->match = (string) $node['match'];
57 69
        if (empty($this->match)) {
58 68
            $this->match = Constraint::MATCH_ALL;
59
        }
60 69
        foreach ($node->attributes() as $name => $value) {
61 69
            if ('match' !== $name) {
62 69
                $this->constraints->append(Factory::createConstraint((string) $name, (string) $value, $this->match));
63
            }
64
        }
65 69
        foreach ($node->children() as $child) {
66 69
            $this->children->append(Factory::create($child, $this));
67
        }
68 69
    }
69
    /**
70
     * @param array|DataList $data
71
     * @param null|int $citationNumber
72
     * @return string
73
     */
74 62
    public function render($data, $citationNumber = null): string
75
    {
76 62
        $ret = [];
77
        /** @var Rendering $child */
78 62
        foreach ($this->children as $child) {
79 62
            $ret[] = $child->render($data, $citationNumber);
80
        }
81 62
        $glue = "";
82 62
        $parent = $this->parent->getParent();
83 62
        if ($parent instanceof Group && $parent->hasDelimiter()) {
84 26
            $glue = $parent->getDelimiter();
85
        }
86 62
        return implode($glue, array_filter($ret));
87
    }
88
    /**
89
     * @param $data
90
     * @param null|int $citationNumber
91
     * @return bool
92
     */
93 62
    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 62
        if ($this->constraints->count() === 1) {
96 61
            return $this->constraints->current()->validate($data);
97
        }
98
99 22
        switch ($this->match) {
100 22
            case Constraint::MATCH_ANY:
101 13
                return $this->constraints
102
                    ->map(function (Constraint $constraint) use ($data) {
103 13
                        return $constraint->validate($data);
104 13
                    })
105
                    ->filter(function (bool $match) {
106 13
                        return $match === true;
107 13
                    })
108 13
                    ->count() > 0;
109 21
            case Constraint::MATCH_ALL:
110 20
                return $this->constraints
111
                    ->map(function (Constraint $constraint) use ($data) {
112 20
                        return $constraint->validate($data);
113 20
                    })
114
                    ->filter(function (bool $match) {
115 20
                        return $match === true;
116 20
                    })
117 20
                    ->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 5
    public function getParent(): Choose
137
    {
138 5
        return $this->parent;
139
    }
140
}
141