|
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\Rendering\Name; |
|
11
|
|
|
|
|
12
|
|
|
use Seboettg\CiteProc\Exception\CiteProcException; |
|
13
|
|
|
use Seboettg\CiteProc\Styles\AffixesTrait; |
|
14
|
|
|
use Seboettg\CiteProc\Styles\FormattingTrait; |
|
15
|
|
|
use Seboettg\CiteProc\Styles\TextCaseTrait; |
|
16
|
|
|
use SimpleXMLElement; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class NamePart |
|
20
|
|
|
* |
|
21
|
|
|
* The cs:name element may contain one or two cs:name-part child elements for name-part-specific formatting. |
|
22
|
|
|
* cs:name-part must carry the name attribute, set to either “given” or “family”. |
|
23
|
|
|
* |
|
24
|
|
|
* If set to “given”, formatting and text-case attributes on cs:name-part affect the “given” and “dropping-particle” |
|
25
|
|
|
* name-parts. affixes surround the “given” name-part, enclosing any demoted name particles for inverted names. |
|
26
|
|
|
* |
|
27
|
|
|
* If set to “family”, formatting and text-case attributes affect the “family” and “non-dropping-particle” name-parts. |
|
28
|
|
|
* affixes surround the “family” name-part, enclosing any preceding name particles, as well as the “suffix” name-part |
|
29
|
|
|
* for non-inverted names. |
|
30
|
|
|
* |
|
31
|
|
|
* The “suffix” name-part is not subject to name-part formatting. The use of cs:name-part elements does not influence |
|
32
|
|
|
* which, or in what order, name-parts are rendered. |
|
33
|
|
|
* |
|
34
|
|
|
* |
|
35
|
|
|
* @package Seboettg\CiteProc\Rendering\Name |
|
|
|
|
|
|
36
|
|
|
* |
|
37
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
38
|
|
|
*/ |
|
|
|
|
|
|
39
|
|
|
class NamePart |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
use FormattingTrait, |
|
|
|
|
|
|
43
|
|
|
TextCaseTrait, |
|
44
|
|
|
AffixesTrait; |
|
45
|
|
|
|
|
46
|
|
|
private $name; |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
/** |
|
|
|
|
|
|
49
|
|
|
* @var Name |
|
50
|
|
|
*/ |
|
51
|
|
|
private $parent; |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* NamePart constructor. |
|
55
|
|
|
* @param SimpleXMLElement $node |
|
|
|
|
|
|
56
|
|
|
* @param Name $parent |
|
|
|
|
|
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function __construct(SimpleXMLElement $node, $parent) |
|
59
|
|
|
{ |
|
60
|
3 |
|
$this->parent = $parent; |
|
61
|
|
|
|
|
62
|
|
|
/** @var SimpleXMLElement $attribute */ |
|
|
|
|
|
|
63
|
3 |
|
foreach ($node->attributes() as $attribute) { |
|
64
|
3 |
|
if ($attribute->getName() === 'name') { |
|
65
|
3 |
|
$this->name = (string) $attribute; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
3 |
|
$this->initFormattingAttributes($node); |
|
70
|
3 |
|
$this->initTextCaseAttributes($node); |
|
71
|
3 |
|
$this->initAffixesAttributes($node); |
|
72
|
3 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
|
|
|
|
|
75
|
|
|
* @param $data |
|
|
|
|
|
|
76
|
|
|
* @return mixed |
|
|
|
|
|
|
77
|
|
|
* @throws CiteProcException |
|
|
|
|
|
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function render($data) |
|
80
|
|
|
{ |
|
81
|
2 |
|
if (!isset($data->{$this->name})) { |
|
82
|
|
|
return ""; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
switch ($this->name) { |
|
86
|
|
|
|
|
87
|
|
|
/* If set to “given”, formatting and text-case attributes on cs:name-part affect the “given” and |
|
88
|
|
|
“dropping-particle” name-parts. affixes surround the “given” name-part, enclosing any demoted name particles |
|
89
|
|
|
for inverted names.*/ |
|
90
|
2 |
|
case 'given': |
|
|
|
|
|
|
91
|
2 |
|
return $this->addAffixes($this->format($this->applyTextCase($data->given))); |
|
92
|
|
|
|
|
93
|
|
|
/* if name set to “family”, formatting and text-case attributes affect the “family” and |
|
94
|
|
|
“non-dropping-particle” name-parts. affixes surround the “family” name-part, enclosing any preceding name |
|
95
|
|
|
particles, as well as the “suffix” name-part for non-inverted names.*/ |
|
96
|
2 |
|
case 'family': |
|
|
|
|
|
|
97
|
2 |
|
return $this->addAffixes($this->format($this->applyTextCase($data->family))); |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
throw new CiteProcException("This shouldn't happen."); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
|
|
|
|
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
3 |
|
public function getName() |
|
107
|
|
|
{ |
|
108
|
3 |
|
return $this->name; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |