Passed
Push — master ( 9180f1...3c41f4 )
by Sebastian
07:32 queued 03:42
created

DatePart::renderDay()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 9.648

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
nc 11
nop 1
dl 0
loc 22
ccs 12
cts 15
cp 0.8
crap 9.648
rs 8.0555
c 1
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 66
    public function __construct(SimpleXMLElement $node)
58
    {
59 66
        foreach ($node->attributes() as $attribute) {
60 66
            if ("name" === $attribute->getName()) {
61 66
                $this->name = (string) $attribute;
62
            }
63 66
            if ("form" === $attribute->getName()) {
64 28
                $this->form = (string) $attribute;
65
            }
66 66
            if ("range-delimiter" === $attribute->getName()) {
67 66
                $this->rangeDelimiter = (string) $attribute;
68
            }
69
        }
70
71 66
        if (empty($this->rangeDelimiter)) {
72 66
            $this->rangeDelimiter = self::DEFAULT_RANGE_DELIMITER;
73
        }
74
75 66
        $this->initFormattingAttributes($node);
76 66
        $this->initAffixesAttributes($node);
77 66
        $this->initTextCaseAttributes($node);
78 66
    }
79
80
81
    /**
82
     * @param DateTime $date
83
     * @param Date $parent
84
     * @return string
85
     */
86 56
    public function render(DateTime $date, Date $parent)
87
    {
88 56
        $this->parent = $parent; //set parent
89 56
        $text = $this->renderWithoutAffixes($date);
90 56
        return !empty($text) ? $this->addAffixes($text) : "";
91
    }
92
93
    /**
94
     * @param DateTime $date
95
     * @param Date|null $parent
96
     * @return string
97
     */
98 56
    public function renderWithoutAffixes(DateTime $date, Date $parent = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$parent" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$parent"; expected 0 but found 1
Loading history...
99
    {
100 56
        if (!is_null($parent)) {
101 1
            $this->parent = $parent;
102
        }
103 56
        $text = "";
104 56
        switch ($this->name) {
105 56
            case 'year':
106 55
                $text = $this->renderYear($date);
107 55
                break;
108 17
            case 'month':
109 17
                $text = $this->renderMonth($date);
110 17
                break;
111 14
            case 'day':
112 14
                $text = $this->renderDay($date);
113
        }
114
115 56
        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 45
    public function getName()
130
    {
131 45
        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 55
    protected function renderYear(DateTime $date)
147
    {
148 55
        $text = $date->getYear();
149 55
        if ($text > 0 && $text < 1000) {
150
            $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "ad")->single;
151
            return $text;
152 55
        } elseif ($text < 0) {
153
            $text = $text * -1;
154
            $text = $text . CiteProc::getContext()->getLocale()->filter("terms", "bc")->single;
155
            return $text;
156
        }
157 55
        return $text;
158
    }
159
160
    /**
161
     * @param DateTime $date
162
     * @return string
163
     */
164 17
    protected function renderMonth(DateTime $date)
165
    {
166 17
        if ($date->getMonth() < 1 || $date->getMonth() > 12) {
167 1
            return "";
168
        }
169
170 16
        $text = $date->getMonth();
171
172 16
        $form = !empty($this->form) ? $this->form : "long";
173
        switch ($form) {
174 16
            case 'numeric':
175 1
                break;
176 16
            case 'numeric-leading-zeros':
177 3
                $text = sprintf("%02d", $text);
178 3
                break;
179 14
            case 'short':
180 11
            case 'long':
181
            default:
182 14
                $text = $this->monthFromLocale($text, $form);
183 14
                break;
184
        }
185 16
        return $text;
186
    }
187
188
    /**
189
     * @param DateTime $date
190
     * @return int|string
191
     */
192 14
    protected function renderDay(DateTime $date)
193
    {
194 14
        if ($date->getDay() < 1 || $date->getDay() > 31) {
195 2
            return "";
196
        }
197
198 14
        $text = $date->getDay();
199 14
        $form = !empty($this->form) ? $this->form : $this->parent->getForm();
200
        switch ($form) {
201 14
            case 'numeric':
202 1
                break;
203 14
            case 'numeric-leading-zeros':
204 3
                $text = sprintf("%02d", $text);
205 3
                break;
206 12
            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 14
        return $text;
214
    }
215
216
    /**
217
     * @param $text
218
     * @param $form
219
     * @return mixed
220
     */
221 14
    protected function monthFromLocale($text, $form)
222
    {
223 14
        if (empty($form)) {
224
            $form = "long";
225
        }
226 14
        $month = 'month-' . sprintf('%02d', $text);
227 14
        $text = CiteProc::getContext()->getLocale()->filter('terms', $month, $form)->single;
228 14
        return $text;
229
    }
230
}
231