Passed
Push — new-api ( 44902a...f133eb )
by Sebastian
04:49
created

Factory::create()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.7283

Importance

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