Passed
Push — new-api ( 42b595 )
by Sebastian
06:18
created

Factory::create()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4.7691

Importance

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