Completed
Push — master ( 0f0a32...5d3cd3 )
by Sebastian
02:20
created

NamePart::render()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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