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