Completed
Push — master ( 3eefa2...fea31f )
by Sebastian
03:06
created

Key::getMacro()   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\Style\Sort;
11
use Seboettg\CiteProc\CiteProc;
12
use Seboettg\CiteProc\Util\Variables;
13
14
15
/**
16
 * Class Key
17
 *
18
 * The cs:sort element must contain one or more cs:key child elements. The sort key, set as an attribute on cs:key, must
19
 * be a variable (see Appendix IV - Variables) or macro name. For each cs:key element, the sort direction can be set to
20
 * either “ascending” (default) or “descending” with the sort attribute. The attributes names-min, names-use-first, and
21
 * names-use-last may be used to override the values of the corresponding et-al-min/et-al-subsequent-min,
22
 * et-al-use-first/et-al-subsequent-use-first and et-al-use-last attributes, and affect all names generated via macros
23
 * called by cs:key.
24
 *
25
 * @package Seboettg\CiteProc\Style\Sort
26
 *
27
 * @author Sebastian Böttger <[email protected]>
28
 */
29
class Key implements SortKey
30
{
31
    /**
32
     * variable name or macro
33
     * @var string
34
     */
35
    private $variable;
36
37
    /**
38
     * the sort direction can be set to either “ascending” (default) or “descending” with the sort attribute
39
     * @var string
40
     */
41
    private $sort = "ascending";
42
43
    /**
44
     * macro name
45
     * @var string
46
     */
47
    private $macro;
48
49
    /**
50
     * Key constructor.
51
     * The cs:sort element must contain one or more cs:key child elements. The sort key, set as an attribute on cs:key,
52
     * must be a variable (see Appendix IV - Variables) or macro name. For each cs:key element, the sort direction can
53
     * be set to either “ascending” (default) or “descending” with the sort attribute.
54
     *
55
     * TODO: The attributes names-min, names-use-first, and names-use-last may be used to override the values of the
56
     * corresponding et-al-min/et-al-subsequent-min, et-al-use-first/et-al-subsequent-use-first and et-al-use-last
57
     * attributes, and affect all names generated via macros called by cs:key.
58
     *
59
     * @param \SimpleXMLElement $node
60
     */
61
    public function __construct(\SimpleXMLElement $node)
62
    {
63
        /** @var \SimpleXMLElement $attribute */
64
        foreach ($node->attributes() as $attribute) {
65
            $name = $attribute->getName();
66
            if ($name === "variable") {
67
                $this->variable = (string) $attribute;
68
            }
69
            if ($name === "sort") {
70
                $this->sort = (string) $attribute;
71
            }
72
            if ($name === "macro") {
73
                $this->variable = "macro";
74
                $this->macro = (string) $attribute;
75
            }
76
        }
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getVariable()
83
    {
84
        return $this->variable;
85
    }
86
87
    /**
88
     * @return string (ascending|descending)
89
     */
90
    public function getSort()
91
    {
92
        return $this->sort;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getMacro()
99
    {
100
        return $this->macro;
101
    }
102
103
    /**
104
     * @return bool
105
     */
106
    public function isNameVariable()
107
    {
108
        return Variables::isNameVariable($this->variable);
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    public function isNumberVariable()
115
    {
116
        return Variables::isNumberVariable($this->variable);
117
    }
118
119
    /**
120
     * @return bool
121
     */
122
    public function isDateVariable()
123
    {
124
        return Variables::isDateVariable($this->variable);
125
    }
126
127
    /**
128
     * @return bool
129
     */
130
    public function isMacro()
131
    {
132
        return $this->variable === "macro" && !empty(CiteProc::getContext()->getMacro($this->macro));
133
    }
134
}