Passed
Push — new-api ( 7cf340...18d26d )
by Sebastian
12:35 queued 08:17
created

Factory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 46
c 5
b 0
f 0
dl 0
loc 71
ccs 12
cts 16
cp 0.75
rs 10
wmc 6

1 Method

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