Passed
Push — new-api ( 18d26d...074931 )
by Sebastian
04:59
created

Factory::create()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 9.0146

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 2
b 0
f 0
nc 10
nop 2
dl 0
loc 24
ccs 9
cts 16
cp 0.5625
crap 9.0146
rs 9.2222
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\Util;
11
12
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
13
use Seboettg\CiteProc\Rendering\Choose\Choose;
14
use Seboettg\CiteProc\Rendering\Date\Date;
15
use Seboettg\CiteProc\Rendering\Date\DatePart;
16
use Seboettg\CiteProc\Rendering\Group;
17
use Seboettg\CiteProc\Rendering\Label\Label;
18
use Seboettg\CiteProc\Rendering\Name\EtAl;
19
use Seboettg\CiteProc\Rendering\Name\Name;
20
use Seboettg\CiteProc\Rendering\Name\NamePart;
21
use Seboettg\CiteProc\Rendering\Name\Names;
22
use Seboettg\CiteProc\Rendering\Number\Number;
23
use Seboettg\CiteProc\Rendering\Text\Text;
24
use Seboettg\CiteProc\StyleSheet;
25
use SimpleXMLElement;
26
27
/**
28
 * Class Factory
29
 * @package Seboettg\CiteProc\Util
30
 *
31
 * @author Sebastian Böttger <[email protected]>
32
 */
33
class Factory
34
{
35
    const CITE_PROC_NODE_NAMESPACE = "Seboettg\\CiteProc\\Rendering";
36
37
    /**
38
     * @var array
39
     */
40
    private static $nodes = [
41
42
        'layout'        => "\\Layout",
43
        'text'          => "\\Text\\Text",
44
        "macro"         => "\\Macro",
45
        "number"        => "\\Number\\Number",
46
        "label"         => "\\Label\\Label",
47
        "group"         => "\\Group",
48
        "choose"        => "\\Choose\\Choose",
49
        "if"            => "\\Choose\\ChooseIf",
50
        "else-if"       => "\\Choose\\ChooseElseIf",
51
        "else"          => "\\Choose\\ChooseElse",
52
        'date'          => "\\Date\\Date",
53
        "date-part"     => "\\Date\\DatePart",
54
        "names"         => "\\Name\\Names",
55
        "name"          => "\\Name\\Name",
56
        "name-part"     => "\\Name\\NamePart",
57
        "substitute"    => "\\Name\\Substitute",
58
        "et-al"         => "\\Name\\EtAl"
59
    ];
60
61
    private static $factories = [
62
        Label::class,
63
        Number::class,
64
        Text::class,
65
        Date::class,
66
        Group::class,
67
        EtAl::class,
68
        DatePart::class,
69
        NamePart::class,
70
        Names::class,
71
        Name::class,
72
        Choose::class,
73
    ];
74
75
    /**
76
     * @param SimpleXMLElement $node
77
     * @param mixed $param
78
     * @return mixed
79
     * @throws InvalidStylesheetException
80
     */
81 177
    public static function create(SimpleXMLElement $node, $param = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$param" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$param"; expected 0 but found 1
Loading history...
82
    {
83 177
        if ($node instanceof StyleSheet) {
0 ignored issues
show
introduced by
$node is never a sub-type of Seboettg\CiteProc\StyleSheet.
Loading history...
84
            $node = ($node)();
85
        }
86 177
        $nodeClass = self::CITE_PROC_NODE_NAMESPACE . self::$nodes[$node->getName()];
87 177
        if (!class_exists($nodeClass)) {
88
            throw new InvalidStylesheetException("For node {$node->getName()} ".
89
                "does not exist any counterpart class \"".$nodeClass.
90
                "\". The given stylesheet seems to be invalid.");
91
        }
92
93 177
        if (in_array($nodeClass, self::$factories)) {
94 177
            $params = [$node];
95 177
            if ($param !== null) {
96 162
                $params[] = $param;
97
            }
98 177
            return call_user_func([$nodeClass, "factory"], ...$params);
99
        }
100
101
        if ($param !== null) {
102
            return new $nodeClass($node, $param);
103
        }
104
        return new $nodeClass($node);
105
    }
106
}
107