Passed
Push — new-api ( 4bfe18...7ec1cc )
by Sebastian
05:06
created

NamePart::factory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/*
4
 * citeproc-php
5
 *
6
 * @link        http://github.com/seboettg/citeproc-php for the source repository
7
 * @copyright   Copyright (c) 2016 Sebastian Böttger.
8
 * @license     https://opensource.org/licenses/MIT
9
 */
10
11
namespace Seboettg\CiteProc\Rendering\Name;
12
13
use Seboettg\CiteProc\Styles\StylesRenderer;
14
use SimpleXMLElement;
15
16
/**
17
 * Class NamePart
18
 *
19
 * The cs:name element may contain one or two cs:name-part child elements for name-part-specific formatting.
20
 * cs:name-part must carry the name attribute, set to either “given” or “family”.
21
 *
22
 * If set to “given”, formatting and text-case attributes on cs:name-part affect the “given” and “dropping-particle”
23
 * name-parts. affixes surround the “given” name-part, enclosing any demoted name particles for inverted names.
24
 *
25
 * If set to “family”, formatting and text-case attributes affect the “family” and “non-dropping-particle” name-parts.
26
 * affixes surround the “family” name-part, enclosing any preceding name particles, as well as the “suffix” name-part
27
 * for non-inverted names.
28
 *
29
 * The “suffix” name-part is not subject to name-part formatting. The use of cs:name-part elements does not influence
30
 * which, or in what order, name-parts are rendered.
31
 *
32
 *
33
 * @package Seboettg\CiteProc\Rendering\Name
34
 *
35
 * @author Sebastian Böttger <[email protected]>
36
 */
37
class NamePart
38
{
39
40
    /** @var string */
41
    private $name;
42
43
    /** @var StylesRenderer */
44
    private $stylesRenderer;
45
46 3
    public static function factory(SimpleXMLElement $node): NamePart
47
    {
48 3
        $name = (string)$node['name'];
49 3
        $stylesRenderer = StylesRenderer::factory($node);
50 3
        return new self($name, $stylesRenderer);
51
    }
52
53
54 3
    public function __construct(string $name, StylesRenderer $stylesRenderer)
55
    {
56 3
        $this->name = $name;
57 3
        $this->stylesRenderer = $stylesRenderer;
58 3
    }
59
60
    /**
61
     * @param $data
62
     * @return string
63
     */
64 2
    public function render($data): string
65
    {
66 2
        if (!isset($data->{$this->name})) {
67
            return "";
68
        }
69
70 2
        return $this->stylesRenderer->renderAffixes(
71 2
            $this->stylesRenderer->renderFormatting(
72 2
                $this->stylesRenderer->renderTextCase($data->{$this->name})
73
            )
74
        );
75
    }
76
77
    /**
78
     * @return string
79
     */
80 3
    public function getName(): string
81
    {
82 3
        return $this->name;
83
    }
84
}
85