Passed
Push — master ( dcb940...49cd7f )
by Sebastian
14:22 queued 12:58
created

DatePart::getRangeDelimiter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\Date;
11
12
use Seboettg\CiteProc\CiteProc;
13
use Seboettg\CiteProc\Rendering\Layout;
14
use Seboettg\CiteProc\Rendering\Number;
15
use Seboettg\CiteProc\Styles\AffixesTrait;
16
use Seboettg\CiteProc\Styles\FormattingTrait;
17
use Seboettg\CiteProc\Styles\RangeDelimiterTrait;
18
use Seboettg\CiteProc\Styles\TextCaseTrait;
19
use SimpleXMLElement;
20
21
/**
22
 * Class DatePart
23
 * @package Seboettg\CiteProc\Rendering\Date
24
 *
25
 * @author Sebastian Böttger <[email protected]>
26
 */
27
class DatePart
28
{
29
30
    const DEFAULT_RANGE_DELIMITER = "–";
31
32
    use FormattingTrait,
0 ignored issues
show
Bug introduced by
The trait Seboettg\CiteProc\Styles\AffixesTrait requires the property $single which is not provided by Seboettg\CiteProc\Rendering\Date\DatePart.
Loading history...
33
        AffixesTrait,
34
        TextCaseTrait,
35
        RangeDelimiterTrait;
36
37
    /**
38
     * @var string
39
     */
40
    private $name;
41
42
    /**
43
     * @var string
44
     */
45
    private $form;
46
47
    /**
48
     * @var string
49
     */
50
    private $rangeDelimiter;
51
52
    /**
53
     * @var Date
54
     */
55
    private $parent;
56
57 72
    public function __construct(SimpleXMLElement $node)
58
    {
59 72
        foreach ($node->attributes() as $attribute) {
60 72
            if ("name" === $attribute->getName()) {
61 72
                $this->name = (string) $attribute;
62
            }
63 72
            if ("form" === $attribute->getName()) {
64 33
                $this->form = (string) $attribute;
65
            }
66 72
            if ("range-delimiter" === $attribute->getName()) {
67 1
                $this->rangeDelimiter = (string) $attribute;
68
            }
69
        }
70
71 72
        if (empty($this->rangeDelimiter)) {
72 72
            $this->rangeDelimiter = self::DEFAULT_RANGE_DELIMITER;
73
        }
74
75 72
        $this->initFormattingAttributes($node);
76 72
        $this->initAffixesAttributes($node);
77 72
        $this->initTextCaseAttributes($node);
78 72
    }
79
80
81
    /**
82
     * @param DateTime $date
83
     * @param Date $parent
84
     * @return string
85
     */
86 62
    public function render(DateTime $date, Date $parent)
87
    {
88 62
        $this->parent = $parent; //set parent
89 62
        $text = $this->renderWithoutAffixes($date);
90 62
        return !empty($text) ? $this->addAffixes($text) : "";
91
    }
92
93
    /**
94
     * @param DateTime $date
95
     * @param Date|null $parent
96
     * @return string
97
     */
98 62
    public function renderWithoutAffixes(DateTime $date, Date $parent = null)
99
    {
100 62
        if (!is_null($parent)) {
101 1
            $this->parent = $parent;
102
        }
103 62
        $text = "";
104 62
        switch ($this->name) {
105 62
            case 'year':
106 61
                $text = $this->renderYear($date);
107 61
                break;
108 19
            case 'month':
109 19
                $text = $this->renderMonth($date);
110 19
                break;
111 15
            case 'day':
112 15
                $text = $this->renderDay($date);
113
        }
114
115 62
        return !empty($text) ? $this->format($this->applyTextCase($text)) : "";
116
    }
117
118
    /**
119
     * @return string
120
     */
121
    public function getForm()
122
    {
123
        return $this->form;
124
    }
125
126
    /**
127
     * @return string
128
     */
129 51
    public function getName()
130
    {
131 51
        return $this->name;
132
    }
133
134
    /**
135
     * @return string
136
     */
137 1
    public function getRangeDelimiter()
138
    {
139 1
        return $this->rangeDelimiter;
140
    }
141
142
    /**
143
     * @param DateTime $date
144
     * @return string|int
145
     */
146 61
    protected function renderYear(DateTime $date)
147
    {
148 61
        $text = $date->getYear();
149 61
        if ($text > 0 && $text < 1000) {
150
            $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "ad")->single;
151
            return $text;
152 61
        } elseif ($text < 0) {
153
            $text = $text * -1;
154
            $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "bc")->single;
155
            return $text;
156
        }
157 61
        return $text;
158
    }
159
160
    /**
161
     * @param DateTime $date
162
     * @return string
163
     */
164 19
    protected function renderMonth(DateTime $date)
165
    {
166 19
        if ($date->getMonth() < 1 || $date->getMonth() > 12) {
167 2
            return "";
168
        }
169
170 17
        $text = $date->getMonth();
171
172 17
        $form = !empty($this->form) ? $this->form : "long";
173 17
        switch ($form) {
174 17
            case 'numeric':
175 1
                break;
176 17
            case 'numeric-leading-zeros':
177 3
                $text = sprintf("%02d", $text);
178 3
                break;
179 15
            case 'short':
180 11
            case 'long':
181
            default:
182 15
                $text = $this->monthFromLocale($text, $form);
183 15
                break;
184
        }
185 17
        return $text;
186
    }
187
188
    /**
189
     * @param DateTime $date
190
     * @return int|string
191
     */
192 15
    protected function renderDay(DateTime $date)
193
    {
194 15
        if ($date->getDay() < 1 || $date->getDay() > 31) {
195 2
            return "";
196
        }
197
198 15
        $text = $date->getDay();
199 15
        $form = !empty($this->form) ? $this->form : $this->parent->getForm();
200 15
        switch ($form) {
201 15
            case 'numeric':
202 1
                break;
203 15
            case 'numeric-leading-zeros':
204 3
                $text = sprintf("%02d", $text);
205 3
                break;
206 13
            case 'ordinal':
207
                $limitDayOrdinals =
208
                    CiteProc::getContext()->getLocale()->filter("options", "limit-day-ordinals-to-day-1");
209
                if (!$limitDayOrdinals || Layout::getNumberOfCitedItems() <= 1) {
0 ignored issues
show
introduced by
$limitDayOrdinals is of type stdClass, thus it always evaluated to true.
Loading history...
210
                    $text = Number::ordinal($text);
211
                }
212
        }
213 15
        return $text;
214
    }
215
216
    /**
217
     * @param $text
218
     * @param $form
219
     * @return mixed
220
     */
221 15
    protected function monthFromLocale($text, $form)
222
    {
223 15
        if (empty($form)) {
224
            $form = "long";
225
        }
226 15
        $month = 'month-' . sprintf('%02d', $text);
227 15
        $text = CiteProc::getContext()->getLocale()->filter('terms', $month, $form)->single;
228 15
        return $text;
229
    }
230
}
231