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

Variables::nameHash()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2.0054

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 13
ccs 8
cts 9
cp 0.8889
crap 2.0054
rs 10
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\Util;
11
12
use InvalidArgumentException;
13
use Seboettg\CiteProc\Exception\CiteProcException;
14
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
15
use Seboettg\CiteProc\Rendering\Name\Names;
16
use SimpleXMLElement;
17
use stdClass;
18
19
/**
20
 * Class Variables
21
 * @package Seboettg\CiteProc\Util
22
 *
23
 * @author Sebastian Böttger <[email protected]>
24
 */
25
class Variables
26
{
27
28
    const NAME_VARIABLES = [
29
        'author', // author
30
        'collection-editor', // editor of the collection holding the item (e.g. the series editor for a book)
31
        'composer', // composer (e.g. of a musical score)
32
        'container-author', // author of the container holding the item (e.g. the book author for a book chapter)
33
        'director', // director (e.g. of a film)
34
        'editor', // editor
35
        'editorial-director', // managing editor (“Directeur de la Publication” in French)
36
        'illustrator', // illustrator (e.g. of a children’s book)
37
        'interviewer', // interviewer (e.g. of an interview)
38
        'original-author', //
39
        'recipient', // recipient (e.g. of a letter)
40
        'reviewed-author'       // author of the item reviewed by the current item
41
    ];
42
43
    const NUMBER_VARIABLES = [
44
        'chapter-number', // chapter number
45
        'collection-number', // number identifying the collection holding the item (e.g. the series number for a book)
46
        'edition', // (container) edition holding the item (e.g. “3” when citing a chapter in the third
47
                              // edition of a book)
48
        'issue', // (container) issue holding the item (e.g. “5” when citing a journal article from
49
                              // journal volume 2, issue 5)
50
        'number', // number identifying the item (e.g. a report number)
51
        'number-of-pages', // total number of pages of the cited item
52
        'number-of-volumes', // total number of volumes, usable for citing multi-volume books and such
53
        'volume'              // (container) volume holding the item (e.g. “2” when citing a chapter from book volume 2)
54
    ];
55
56
    const DATE_VARIABLES = [
57
        'accessed', // date the item has been accessed
58
        'container',
59
        'event-date', // date the related event took place
60
        'issued', // date the item was issued/published
61
        'original-date', // (issue) date of the original version
62
        'submitted'             // date the item (e.g. a manuscript) has been submitted for publication
63
    ];
64
65
    const STANDARD_VARIABLE = [
66
        'abstract', //abstract of the item (e.g. the abstract of a journal article)
67
        'annote', //reader’s notes about the item content
68
        'archive', //archive storing the item
69
        'archive-location', //storage location within an archive (e.g. a box and folder number)
70
        'archive-place', //geographic location of the archive
71
        'authority', //issuing or judicial authority (e.g. “USPTO” for a patent, “Fairfax Circuit Court” for
72
                                //a legal case)
73
        'call-number', //call number (to locate the item in a library)
74
        'citation-label', //label identifying the item in in-text citations of label styles (e.g. “Ferr78”). May
75
                                //be assigned by the CSL processor based on item metadata.
76
        'citation-number', //index (starting at 1) of the cited reference in the bibliography (generated by the CSL
77
                                //processor)
78
        'collection-title', //title of the collection holding the item (e.g. the series title for a book)
79
        'container-title', //title of the container holding the item (e.g. the book title for a book chapter, the
80
                                //journal title for a journal article)
81
        'container-title-short', //short/abbreviated form of “container-title” (also accessible through the “short” form
82
                                //of the “container-title” variable)
83
        'dimensions', //physical (e.g. size) or temporal (e.g. running time) dimensions of the item
84
        'DOI', //Digital Object Identifier (e.g. “10.1128/AEM.02591-07”)
85
        'event', //name of the related event (e.g. the conference name when citing a conference paper)
86
        'event-place', //geographic location of the related event (e.g. “Amsterdam, the Netherlands”)
87
        'first-reference-note-number', //number of a preceding note containing the first reference to the item. Assigned
88
                                // by the CSL processor. The variable holds no value for non-note-based styles, or when
89
                                // the item hasn’t been cited in any preceding notes.
90
        'genre', //class, type or genre of the item (e.g. “adventure” for an adventure movie,
91
                                //“PhD dissertation” for a PhD thesis),
92
        'ISBN', //International Standard Book Number
93
        'ISSN', //International Standard Serial Number
94
        'jurisdiction', //geographic scope of relevance (e.g. “US” for a US patent)
95
        'keyword', //keyword(s) or tag(s) attached to the item
96
        'locator', //a cite-specific pinpointer within the item (e.g. a page number within a book, or a
97
                                //volume in a multi-volume work). Must be accompanied in the input data by a label
98
                                //indicating the locator type (see the Locators term list), which determines which term
99
                                //is rendered by cs:label when the “locator” variable is selected.
100
        'medium', //medium description (e.g. “CD”, “DVD”, etc.)
101
        'note', //(short) inline note giving additional item details (e.g. a concise summary or commentary)
102
        'original-publisher', //original publisher, for items that have been republished by a different publisher
103
        'original-publisher-place', //geographic location of the original publisher (e.g. “London, UK”)
104
        'original-title', //title of the original version (e.g. “Война и мир”, the untranslated Russian title of
105
                                // “War and Peace”)
106
        'page', //range of pages the item (e.g. a journal article) covers in a container (e.g. a journal
107
                                // issue)
108
        'page-first', //first page of the range of pages the item (e.g. a journal article) covers in a
109
                                //container (e.g. a journal issue)
110
        'PMCID', //PubMed Central reference number
111
        'PMID', //PubMed reference number
112
        'publisher', //publisher
113
        'publisher-place', //geographic location of the publisher
114
        'references', //resources related to the procedural history of a legal case
115
        'reviewed-title', //title of the item reviewed by the current item
116
        'scale', //scale of e.g. a map
117
        'section', //container section holding the item (e.g. “politics” for a newspaper article)
118
        'source', //from whence the item originates (e.g. a library catalog or database)
119
        'status', //(publication) status of the item (e.g. “forthcoming”)
120
        'title', //primary title of the item
121
        'title-short', //short/abbreviated form of “title” (also accessible through the “short” form of the
122
                                //“title” variable)
123
        'URL', //Uniform Resource Locator (e.g. “http://aem.asm.org/cgi/content/full/74/9/2766”)
124
        'version', //version of the item (e.g. “2.0.9” for a software program)
125
        'year-suffix', //disambiguating year suffix in author-date styles (e.g. “a” in “Doe, 1999a”)
126
127
128
    ];
129
130
    /**
131
     * @param $name
132
     * @return bool
133
     */
134 27
    public static function isDateVariable($name)
135
    {
136 27
        return in_array($name, self::DATE_VARIABLES);
137
    }
138
139
    /**
140
     * @param $name
141
     * @return bool
142
     */
143 26
    public static function isNumberVariable($name)
144
    {
145 26
        return in_array($name, self::NUMBER_VARIABLES);
146
    }
147
148
    /**
149
     * @param $name
150
     * @return bool
151
     */
152 27
    public static function isNameVariable($name)
153
    {
154 27
        return in_array($name, self::NAME_VARIABLES);
155
    }
156
157
    /**
158
     * @param stdClass $data
159
     * @param string $variable
160
     * @return string
161
     * @throws InvalidStylesheetException
162
     * @throws CiteProcException
163
     */
164 3
    public static function nameHash(stdClass $data, $variable)
165
    {
166 3
        if (!self::isNameVariable($variable)) {
167
            throw new InvalidArgumentException("\"$variable\" is not a valid name variable.");
168
        }
169 3
        $parent = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $parent is dead and can be removed.
Loading history...
170 3
        $names = Names::factory(
171 3
            new SimpleXMLElement(
172 3
                "<names variable=\"$variable\" delimiter=\"-\">" .
173 3
                "<name form=\"long\" sort-separator=\",\" name-as-sort-order=\"all\"/></names>"
174
            )
175
        );
176 3
        return $names->render($data);
177
    }
178
}
179