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) |
|
|
|
|
57
|
|
|
{ |
58
|
171 |
|
if ($node instanceof StyleSheet) { |
|
|
|
|
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
|
|
|
|