|
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) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
175 |
|
if ($node instanceof StyleSheet) { |
|
|
|
|
|
|
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
|
|
|
|