Passed
Push — new-api ( f151f9...5a646f )
by Sebastian
04:44
created

Variable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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\Constraint;
12
13
use Seboettg\CiteProc\Config\RenderingMode;
14
use Seboettg\CiteProc\Rendering\Observer\RenderingObserver;
15
use Seboettg\CiteProc\Rendering\Observer\RenderingObserverTrait;
16
use stdClass;
17
18
/**
19
 * Class Variable
20
 * @package Seboettg\CiteProc\Choose\Constraint
21
 *
22
 * @author Sebastian Böttger <[email protected]>
23
 */
24
/** @noinspection PhpUnused */
25
class Variable extends AbstractConstraint implements RenderingObserver
26
{
27
    use RenderingObserverTrait;
28
29 50
    public function __construct(string $value, string $match = "any")
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$match" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$match"; expected 0 but found 1
Loading history...
30
    {
31 50
        parent::__construct($value, $match);
32 50
        $this->initObserver();
33 50
    }
34
35
    /**
36
     * @param string $variable
37
     * @param stdClass $data
38
     * @return bool
39
     */
40 40
    protected function matchForVariable(string $variable, stdClass $data): bool
41
    {
42 40
        $variableExistInCitationItem = false;
43 40
        if ($this->mode->equals(RenderingMode::CITATION()) && isset($data->id)) {
44 8
            $id = $data->id;
45
            $citationItem = $this->citationItems->filter(function ($item) use ($id) {
46 3
                return $item->id === $id;
47 8
            })->current();
48 8
            if (!empty($citationItem)) {
49 3
                $variableExistInCitationItem = !empty($citationItem->{$variable});
50
            }
51
        }
52 40
        return !empty($data->{$variable}) || $variableExistInCitationItem;
53
    }
54
}
55