Completed
Push — master ( 69f162...acb4bc )
by Sebastian
02:58
created

Text::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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;
11
use Seboettg\CiteProc\CiteProc;
12
use Seboettg\CiteProc\Exception\CiteProcException;
13
use Seboettg\CiteProc\Styles\AffixesTrait;
14
use Seboettg\CiteProc\Styles\ConsecutivePunctuationCharacterTrait;
15
use Seboettg\CiteProc\Styles\DisplayTrait;
16
use Seboettg\CiteProc\Styles\FormattingTrait;
17
use Seboettg\CiteProc\Styles\QuotesTrait;
18
use Seboettg\CiteProc\Styles\TextCaseTrait;
19
20
21
/**
22
 * Class Term
23
 * @package Seboettg\CiteProc\Node\Style
24
 *
25
 * @author Sebastian Böttger <[email protected]>
26
 */
27
class Text implements RenderingInterface
28
{
29
    use FormattingTrait,
30
        AffixesTrait,
31
        TextCaseTrait,
32
        DisplayTrait,
33
        ConsecutivePunctuationCharacterTrait;
34
35
    /**
36
     * @var string
37
     */
38
    private $toRenderType;
39
40
    /**
41
     * @var string
42
     */
43
    private $toRenderTypeValue;
44
45
    private $form = "long";
46
47
    public function __construct(\SimpleXMLElement $node)
48
    {
49
        foreach ($node->attributes() as $attribute) {
50
            $name = $attribute->getName();
51
            if (in_array($name, ['value', 'variable', 'macro', 'term'])) {
52
                $this->toRenderType = $name;
53
                $this->toRenderTypeValue = (string) $attribute;
54
            }
55
            if ($name === "form") {
56
                $this->form = (string) $attribute;
57
            }
58
        }
59
        $this->initFormattingAttributes($node);
60
        $this->initDisplayAttributes($node);
61
        $this->initTextCaseAttributes($node);
62
        $this->initAffixesAttributes($node);
63
    }
64
65
    /**
66
     * @param \stdClass $data
67
     * @param int|null $citationNumber
68
     * @return string
69
     */
70
    public function render($data, $citationNumber = null)
71
    {
72
        $lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en';
73
74
        $renderedText = "";
75
        switch ($this->toRenderType) {
76
            case 'value':
77
                $renderedText = $this->applyTextCase($this->toRenderTypeValue, $lang);
78
                break;
79
            case 'variable':
80
                // check if there is an attribute with prefix short or long e.g. shortTitle or longAbstract
81
                // test case group_ShortOutputOnly.json
82
                if (in_array($this->form, ["short", "long"])) {
83
                    $attrWithPrefix = $this->form . ucfirst($this->toRenderTypeValue);
84
                    if (isset($data->{$attrWithPrefix}) && !empty($data->{$attrWithPrefix})) {
85
                        $renderedText = $this->applyTextCase($data->{$attrWithPrefix}, $lang);
86
                    }
87
                }
88
                if (!empty($data->{$this->toRenderTypeValue})) {
89
                    $renderedText = $this->applyTextCase($data->{$this->toRenderTypeValue}, $lang);
90
                }
91
                // for test sort_BibliographyCitationNumberDescending.json
92
                if ($this->toRenderTypeValue === "citation-number" && !is_null($citationNumber)) {
93
                    $renderedText = strval($citationNumber + 1);
94
                }
95
                break;
96
            case 'macro':
97
                $macro = CiteProc::getContext()->getMacro($this->toRenderTypeValue);
98
                if (is_null($macro)) {
99
                    try {
100
                        throw new CiteProcException("Macro \"" . $this->toRenderTypeValue . "\" does not exist.");
101
                    } catch (CiteProcException $e) {
102
                        $renderedText = "";
103
                    }
104
                } else {
105
                    $renderedText = $macro->render($data);
106
                }
107
                break;
108
            case 'term':
109
                $term = CiteProc::getContext()->getLocale()->filter("terms", $this->toRenderTypeValue, $this->form)->single;
110
                $renderedText = !empty($term) ? $this->applyTextCase($term, $lang) : "";
111
        }
112
        if (!empty($renderedText)) {
113
            //$renderedText = $this->applyTextCase($renderedText);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
114
            $text = $this->format($renderedText);
115
            $res = $this->addAffixes($text, $this->quotes);
0 ignored issues
show
Unused Code introduced by
The call to Text::addAffixes() has too many arguments starting with $this->quotes.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
116
            if (!empty($res)) {
117
                $res = $this->removeConsecutiveChars($res);
118
            }
119
            return $res;
120
        }
121
        return "";
122
    }
123
124
    public function rendersVariable()
125
    {
126
        return $this->toRenderType === "variable" || $this->toRenderType === "macro";
127
    }
128
129
    public function getSource()
130
    {
131
        return $this->toRenderTypeValue;
132
    }
133
134
    public function getVariable()
135
    {
136
        return $this->toRenderTypeValue;
137
    }
138
}