1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace academicpuma\citeproc;
24: use \DOMDocument;
25:
26: class CiteProc {
27:
28: private static $instance;
29: public $bibliography = null;
30: public $citation = null;
31: public $style = null;
32: protected $macros = null;
33: private $info = null;
34: protected $locale = null;
35: protected $style_locale = null;
36: private $mapper = null;
37: public $quash = null;
38: 39: 40: 41:
42: public static function getInstance($xml) {
43:
44: if (self::$instance == null) {
45:
46: self::$instance = new CiteProc($xml);
47: }
48:
49: return self::$instance;
50: }
51:
52: function __construct($csl = NULL, $lang = 'en') {
53: if ($csl) {
54: $this->init($csl, $lang);
55: }
56: }
57:
58: function init($csl, $lang) {
59:
60: $this->mapper = new Mapper();
61: $this->quash = array();
62:
63: $csl_doc = new DOMDocument();
64:
65: if ($csl_doc->loadXML($csl)) {
66:
67: $style_nodes = $csl_doc->getElementsByTagName('style');
68: if ($style_nodes) {
69: foreach ($style_nodes as $style) {
70: $this->style = new Style($style);
71: }
72: }
73:
74: $info_nodes = $csl_doc->getElementsByTagName('info');
75: if ($info_nodes) {
76: foreach ($info_nodes as $info) {
77: $this->info = new Info($info);
78: }
79: }
80:
81: $this->locale = new Locale($lang);
82: $this->locale->set_style_locale($csl_doc);
83:
84:
85: $macro_nodes = $csl_doc->getElementsByTagName('macro');
86: if ($macro_nodes) {
87: $this->macros = new Macros($macro_nodes, $this);
88: }
89:
90: $citation_nodes = $csl_doc->getElementsByTagName('citation');
91: foreach ($citation_nodes as $citation) {
92: $this->citation = new Citation($citation, $this);
93: }
94:
95: $bibliography_nodes = $csl_doc->getElementsByTagName('bibliography');
96: foreach ($bibliography_nodes as $bibliography) {
97: $this->bibliography = new Bibliography($bibliography, $this);
98: }
99: }
100: }
101:
102: function render($data, $mode = NULL) {
103: $text = '';
104: switch ($mode) {
105: case 'citation':
106: $text .= (isset($this->citation)) ? $this->citation->render($data) : '';
107: break;
108: case 'bibliography':
109: default:
110: $text .= (isset($this->bibliography)) ? $this->bibliography->render($data) : '';
111: break;
112: }
113: return $text;
114: }
115:
116: function render_macro($name, $data, $mode) {
117: return $this->macros->render_macro($name, $data, $mode);
118: }
119:
120: function get_locale($type, $arg1, $arg2 = NULL, $arg3 = NULL) {
121: return $this->locale->get_locale($type, $arg1, $arg2, $arg3);
122: }
123:
124: function map_field($field) {
125: if ($this->mapper) {
126: return $this->mapper->map_field($field);
127: }
128: return ($field);
129: }
130:
131: function map_type($field) {
132: if ($this->mapper) {
133: return $this->mapper->map_type($field);
134: }
135: return ($field);
136: }
137:
138: }
139: