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

Key::factory()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 9
nop 1
dl 0
loc 19
ccs 13
cts 13
cp 1
crap 5
rs 9.5555
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\Style\Sort;
11
12
use Seboettg\CiteProc\CiteProc;
13
use Seboettg\CiteProc\Util\Variables;
14
use SimpleXMLElement;
15
16
/**
17
 * Class Key
18
 *
19
 * The cs:sort element must contain one or more cs:key child elements. The sort key, set as an attribute on cs:key, must
20
 * be a variable (see Appendix IV - Variables) or macro name. For each cs:key element, the sort direction can be set to
21
 * either “ascending” (default) or “descending” with the sort attribute. The attributes names-min, names-use-first, and
22
 * names-use-last may be used to override the values of the corresponding et-al-min/et-al-subsequent-min,
23
 * et-al-use-first/et-al-subsequent-use-first and et-al-use-last attributes, and affect all names generated via macros
24
 * called by cs:key.
25
 *
26
 * @package Seboettg\CiteProc\Style\Sort
27
 *
28
 * @author Sebastian Böttger <[email protected]>
29
 */
30
class Key implements SortKey
31
{
32
    /**
33
     * variable name or macro
34
     * @var string
35
     */
36
    private $variable;
37
38
    /**
39
     * the sort direction can be set to either “ascending” (default) or “descending” with the sort attribute
40
     * @var string
41
     */
42
    private $sort = "ascending";
43
44
    /**
45
     * macro name
46
     * @var string
47
     */
48
    private $macro;
49
50
    /**
51
     * only relevant for date ranges
52
     * @var int
53
     */
54
    private $rangePart = 1;
55
56 60
    public static function factory(SimpleXMLElement $node)
57
    {
58 60
        $variable = $macro = null;
59 60
        $sort = "ascending";
60
        /** @var SimpleXMLElement $attribute */
61 60
        foreach ($node->attributes() as $attribute) {
62 60
            $name = $attribute->getName();
63 60
            if ($name === "variable") {
64 44
                $variable = (string) $attribute;
65
            }
66 60
            if ($name === "sort") {
67 22
                $sort = (string) $attribute;
68
            }
69 60
            if ($name === "macro") {
70 40
                $variable = "macro";
71 60
                $macro = (string) $attribute;
72
            }
73
        }
74 60
        return new self($variable, $macro, $sort);
75
    }
76
77
    /**
78
     * Key constructor.
79
     * The cs:sort element must contain one or more cs:key child elements. The sort key, set as an attribute on cs:key,
80
     * must be a variable (see Appendix IV - Variables) or macro name. For each cs:key element, the sort direction can
81
     * be set to either “ascending” (default) or “descending” with the sort attribute.
82
     *
83
     * TODO: The attributes names-min, names-use-first, and names-use-last may be used to override the values of the
84
     * corresponding et-al-min/et-al-subsequent-min, et-al-use-first/et-al-subsequent-use-first and et-al-use-last
85
     * attributes, and affect all names generated via macros called by cs:key.
86
     *
87
     * @param string|null $variable
88
     * @param string|null $macro
89
     * @param string|null $sort
90
     */
91 60
    public function __construct(?string $variable, ?string $macro, ?string $sort)
92
    {
93 60
        $this->variable = $variable;
94 60
        $this->macro = $macro;
95 60
        $this->sort = $sort;
96 60
    }
97
98
    /**
99
     * @return string
100
     */
101 27
    public function getVariable()
102
    {
103 27
        return $this->variable;
104
    }
105
106
    /**
107
     * @return string (ascending|descending)
108
     */
109 27
    public function getSort()
110
    {
111 27
        return $this->sort;
112
    }
113
114
    /**
115
     * @return string
116
     */
117 14
    public function getMacro()
118
    {
119 14
        return $this->macro;
120
    }
121
122
    /**
123
     * @return bool
124
     */
125 27
    public function isNameVariable()
126
    {
127 27
        return Variables::isNameVariable($this->variable);
128
    }
129
130
    /**
131
     * @return bool
132
     */
133 26
    public function isNumberVariable()
134
    {
135 26
        return Variables::isNumberVariable($this->variable);
136
    }
137
138
    /**
139
     * @return bool
140
     */
141 27
    public function isDateVariable()
142
    {
143 27
        return Variables::isDateVariable($this->variable);
144
    }
145
146
    /**
147
     * @return bool
148
     */
149 21
    public function isMacro()
150
    {
151 21
        return $this->variable === "macro" && !empty(CiteProc::getContext()->getMacro($this->macro));
152
    }
153
154
    /**
155
     * @param $rangePart
156
     */
157 1
    public function setRangePart($rangePart)
158
    {
159 1
        $this->rangePart = $rangePart;
160 1
    }
161
162
    /**
163
     * @return int
164
     */
165 5
    public function getRangePart()
166
    {
167 5
        return $this->rangePart;
168
    }
169
}
170