Completed
Push — master ( 63b79a...6fd4c0 )
by Sebastian
03:22
created

Label::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 15

Duplication

Lines 13
Ratio 61.9 %

Importance

Changes 0
Metric Value
cc 5
eloc 15
nc 5
nop 1
dl 13
loc 21
rs 8.7624
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;
11
use Seboettg\CiteProc\CiteProc;
12
use Seboettg\CiteProc\Styles\AffixesTrait;
13
use Seboettg\CiteProc\Styles\FormattingTrait;
14
use Seboettg\CiteProc\Styles\TextCaseTrait;
15
16
17
/**
18
 * Class Label
19
 * @package Seboettg\CiteProc\Rendering
20
 *
21
 * @author Sebastian Böttger <[email protected]>
22
 */
23
class Label implements RenderingInterface
24
{
25
26
    use AffixesTrait,
27
        FormattingTrait,
28
        TextCaseTrait;
29
30
    private $variable;
31
32
    /**
33
     * Selects the form of the term, with allowed values:
34
     *
35
     *   - “long” - (default), e.g. “page”/”pages” for the “page” term
36
     *   - “short” - e.g. “p.”/”pp.” for the “page” term
37
     *   - “symbol” - e.g. “§”/”§§” for the “section” term
38
     *
39
     * @var string
40
     */
41
    private $form = "";
42
43
    /**
44
     * Sets pluralization of the term, with allowed values:
45
     *
46
     *   - “contextual” - (default), the term plurality matches that of the variable content. Content is considered
47
     *     plural when it contains multiple numbers (e.g. “page 1”, “pages 1-3”, “volume 2”, “volumes 2 & 4”), or, in
48
     *     the case of the “number-of-pages” and “number-of-volumes” variables, when the number is higher than 1
49
     *     (“1 volume” and “3 volumes”).
50
     *   - “always” - always use the plural form, e.g. “pages 1” and “pages 1-3”
51
     *   - “never” - always use the singular form, e.g. “page 1” and “page 1-3”
52
     *
53
     * @var string
54
     */
55
    private $plural = "contextual";
56
57
    /**
58
     * Label constructor.
59
     * @param \SimpleXMLElement $node
60
     */
61
    public function __construct(\SimpleXMLElement $node)
62
    {
63
        /** @var \SimpleXMLElement $attribute */
64 View Code Duplication
        foreach ($node->attributes() as $attribute) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            switch ($attribute->getName()) {
66
                case "variable":
67
                    $this->variable = (string) $attribute;
68
                    break;
69
                case "form":
70
                    $this->form = (string) $attribute;
71
                    break;
72
                case "plural":
73
                    $this->plural = (string) $attribute;
74
                    break;
75
            }
76
        }
77
78
        $this->initFormattingAttributes($node);
79
        $this->initAffixesAttributes($node);
80
        $this->initTextCaseAttributes($node);
81
    }
82
83
    /**
84
     * @param \stdClass $data
85
     * @param int|null $citationNumber
86
     * @return string
87
     */
88
    public function render($data, $citationNumber = null)
89
    {
90
        $lang = (isset($data->language) && $data->language != 'en') ? $data->language : 'en';
91
92
        $text = '';
93
        $variables = explode(' ', $this->variable);
94
        $form = !empty($this->form) ? $this->form : 'long';
95
        $plural = "";
96
        switch ($this->plural) {
97
            case 'never':
98
                $plural = 'single';
99
                break;
100
            case 'always':
101
                $plural = 'multiple';
102
                break;
103
            case 'contextual':
104
            default:
105
        }
106
107
        if ($this->variable === "editortranslator") {
108
            if (isset($data->editor) && isset($data->translator)) {
109
                $plural = $this->getPlural($data, $plural, "editortranslator");
110
                $term = CiteProc::getContext()->getLocale()->filter('terms', "editortranslator", $form);
111
                $pluralForm = $term->{$plural};
112
                if (!empty($pluralForm)) {
113
                    $text = $pluralForm;
114
                }
115
            }
116
        } else {
117
            foreach ($variables as $variable) {
118
119
                if (isset($data->{$variable})) {
120
                    $plural = $this->getPlural($data, $plural, $variable);
121
                    $term = CiteProc::getContext()->getLocale()->filter('terms', $variable, $form);
122
                    $var = $data->{$variable};
123
                    $pluralForm = $term->{$plural};
124
                    if (!empty($var) && !empty($pluralForm)) {
125
                        $text = $pluralForm;
126
                        break;
127
                    }
128
                }
129
            }
130
        }
131
132
        if (empty($text)) {
133
            return "";
134
        }
135
        if ($this->stripPeriods) {
136
            $text = str_replace('.', '', $text);
137
        }
138
        $text = $this->format($this->applyTextCase($text, $lang));
139
        return $this->addAffixes($text);
140
    }
141
142
143
    private function evaluateStringPluralism($data, $variable)
144
    {
145
        $str = $data->{$variable};
146
        $plural = 'single';
147
        if (!empty($str)) {
148
//      $regex = '/(?:[0-9],\s*[0-9]|\s+and\s+|&|([0-9]+)\s*[\-\x2013]\s*([0-9]+))/';
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
149
            switch ($variable) {
150
                case 'page':
151
                    $pageRegex = "/([a-zA-Z]*)([0-9]+)\s*(?:–|-)\s*([a-zA-Z]*)([0-9]+)/";
152
                    $err = preg_match($pageRegex, $str, $m);
153
                    if ($err !== false && count($m) == 0) {
154
                        $plural = 'single';
155
                    } elseif ($err !== false && count($m)) {
156
                        $plural = 'multiple';
157
                    }
158
                    break;
159
                default:
160
                    if (is_numeric($str)) {
161
                        return $str > 1 ? 'multiple' : 'single';
162
                    }
163
            }
164
        }
165
        return $plural;
166
    }
167
168
    /**
169
     * @param string $variable
170
     */
171
    public function setVariable($variable)
172
    {
173
        $this->variable = $variable;
174
    }
175
176
    /**
177
     * @param $data
178
     * @param $plural
179
     * @param $variable
180
     * @return string
181
     */
182
    protected function getPlural($data, $plural, $variable)
183
    {
184
185
        if ($variable === "editortranslator") {
186
            $var = $data->editor;
187
        } else {
188
            $var = $data->{$variable};
189
        }
190
        if (((!isset($this->plural) || empty($plural))) && !empty($var)) {
191
            $count = count($var);
192
            if ($count == 1) {
193
                $plural = 'single';
194
                return $plural;
195
            } elseif ($count > 1) {
196
                $plural = 'multiple';
197
                return $plural;
198
            }
199
            return $plural;
200
        } else {
201
            if ($this->plural != "always") {
202
                $plural = $this->evaluateStringPluralism($data, $variable);
203
                return $plural;
204
            }
205
            return $plural;
206
        }
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getForm()
213
    {
214
        return $this->form;
215
    }
216
217
    /**
218
     * @param string $form
219
     */
220
    public function setForm($form)
221
    {
222
        $this->form = $form;
223
    }
224
225
226
}