1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* citeproc-php |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
6
|
|
|
* @copyright Copyright (c) 2020 Sebastian Böttger. |
7
|
|
|
* @license https://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Seboettg\CiteProc\Style\Options; |
11
|
|
|
|
12
|
|
|
use Seboettg\CiteProc\CiteProc; |
13
|
|
|
use Seboettg\CiteProc\Config\RenderingMode; |
14
|
|
|
use SimpleXMLElement; |
15
|
|
|
|
16
|
|
|
class NameOptions |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
private static $attributes = [ |
20
|
|
|
'and', |
21
|
|
|
'delimiter-precedes-et-al', |
22
|
|
|
'delimiter-precedes-last', |
23
|
|
|
'et-al-min', |
24
|
|
|
'et-al-use-first', |
25
|
|
|
'et-al-use-last', |
26
|
|
|
'et-al-subsequent-min', |
27
|
|
|
'et-al-subsequent-use-first', |
28
|
|
|
'initialize', |
29
|
|
|
'initialize-with', |
30
|
|
|
'name-as-sort-order', |
31
|
|
|
'sort-separator', |
32
|
|
|
'name-form', |
33
|
|
|
'form', |
34
|
|
|
'name-delimiter', |
35
|
|
|
'delimiter' |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Specifies the delimiter between the second to last and last name of the names in a name variable. Allowed values |
40
|
|
|
* are “text” (selects the “and” term, e.g. “Doe, Johnson and Smith”) and “symbol” (selects the ampersand, |
41
|
|
|
* e.g. “Doe, Johnson & Smith”). |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $and; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Determines when the name delimiter or a space is used between a truncated name list and the “et-al” |
49
|
|
|
* (or “and others”) term in case of et-al abbreviation. Allowed values: |
50
|
|
|
* - “contextual” - (default), name delimiter is only used for name lists truncated to two or more names |
51
|
|
|
* - 1 name: “J. Doe et al.” |
52
|
|
|
* - 2 names: “J. Doe, S. Smith, et al.” |
53
|
|
|
* - “after-inverted-name” - name delimiter is only used if the preceding name is inverted as a result of the |
54
|
|
|
* - name-as-sort-order attribute. E.g. with name-as-sort-order set to “first”: |
55
|
|
|
* - “Doe, J., et al.” |
56
|
|
|
* - “Doe, J., S. Smith et al.” |
57
|
|
|
* - “always” - name delimiter is always used |
58
|
|
|
* - 1 name: “J. Doe, et al.” |
59
|
|
|
* - 2 names: “J. Doe, S. Smith, et al.” |
60
|
|
|
* - “never” - name delimiter is never used |
61
|
|
|
* - 1 name: “J. Doe et al.” |
62
|
|
|
* - 2 names: “J. Doe, S. Smith et al.” |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $delimiterPrecedesEtAl; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Determines when the name delimiter is used to separate the second to last and the last name in name lists (if |
70
|
|
|
* and is not set, the name delimiter is always used, regardless of the value of delimiter-precedes-last). Allowed |
71
|
|
|
* values: |
72
|
|
|
* |
73
|
|
|
* - “contextual” - (default), name delimiter is only used for name lists with three or more names |
74
|
|
|
* - 2 names: “J. Doe and T. Williams” |
75
|
|
|
* - 3 names: “J. Doe, S. Smith, and T. Williams” |
76
|
|
|
* - “after-inverted-name” - name delimiter is only used if the preceding name is inverted as a result of the |
77
|
|
|
* name-as-sort-order attribute. E.g. with name-as-sort-order set to “first”: |
78
|
|
|
* - “Doe, J., and T. Williams” |
79
|
|
|
* - “Doe, J., S. Smith and T. Williams” |
80
|
|
|
* - “always” - name delimiter is always used |
81
|
|
|
* - 2 names: “J. Doe, and T. Williams” |
82
|
|
|
* - 3 names: “J. Doe, S. Smith, and T. Williams” |
83
|
|
|
* - “never” - name delimiter is never used |
84
|
|
|
* - 2 names: “J. Doe and T. Williams” |
85
|
|
|
* - 3 names: “J. Doe, S. Smith and T. Williams” |
86
|
|
|
* |
87
|
|
|
* @var string |
88
|
|
|
*/ |
89
|
|
|
private $delimiterPrecedesLast; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Use of etAlMin (et-al-min attribute) and etAlUseFirst (et-al-use-first attribute) enables et-al abbreviation. If |
93
|
|
|
* the number of names in a name variable matches or exceeds the number set on etAlMin, the rendered name list is |
94
|
|
|
* truncated after reaching the number of names set on etAlUseFirst. |
95
|
|
|
* |
96
|
|
|
* @var int |
97
|
|
|
*/ |
98
|
|
|
private $etAlMin; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Use of etAlMin (et-al-min attribute) and etAlUseFirst (et-al-use-first attribute) enables et-al abbreviation. If |
102
|
|
|
* the number of names in a name variable matches or exceeds the number set on etAlMin, the rendered name list is |
103
|
|
|
* truncated after reaching the number of names set on etAlUseFirst. |
104
|
|
|
* |
105
|
|
|
* @var int |
106
|
|
|
*/ |
107
|
|
|
private $etAlUseFirst; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* When set to “true” (the default is “false”), name lists truncated by et-al abbreviation are followed by the name |
111
|
|
|
* delimiter, the ellipsis character, and the last name of the original name list. This is only possible when the |
112
|
|
|
* original name list has at least two more names than the truncated name list (for this the value of |
113
|
|
|
* et-al-use-first/et-al-subsequent-min must be at least 2 less than the value of |
114
|
|
|
* et-al-min/et-al-subsequent-use-first). |
115
|
|
|
* |
116
|
|
|
* @var bool |
117
|
|
|
*/ |
118
|
|
|
private $etAlUseLast = false; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* If used, the values of these attributes (et-al-subsequent-min and et-al-subsequent-use-first) replace those of |
122
|
|
|
* respectively et-al-min and et-al-use-first for subsequent cites (cites referencing earlier cited items). |
123
|
|
|
* |
124
|
|
|
* @var int |
125
|
|
|
*/ |
126
|
|
|
private $etAlSubsequentMin; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* If used, the values of these attributes (et-al-subsequent-min and et-al-subsequent-use-first) replace those of |
130
|
|
|
* respectively et-al-min and et-al-use-first for subsequent cites (cites referencing earlier cited items). |
131
|
|
|
* |
132
|
|
|
* @var int |
133
|
|
|
*/ |
134
|
|
|
private $etAlSubsequentUseFirst; |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* When set to “false” (the default is “true”), given names are no longer initialized when “initialize-with” is set. |
138
|
|
|
* However, the value of “initialize-with” is still added after initials present in the full name (e.g. with |
139
|
|
|
* initialize set to “false”, and initialize-with set to ”.”, “James T Kirk” becomes “James T. Kirk”). |
140
|
|
|
* |
141
|
|
|
* @var bool |
142
|
|
|
*/ |
143
|
|
|
private $initialize = true; |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* When set, given names are converted to initials. The attribute value is added after each initial (”.” results |
147
|
|
|
* in “J.J. Doe”). For compound given names (e.g. “Jean-Luc”), hyphenation of the initials can be controlled with |
148
|
|
|
* the global initialize-with-hyphen option |
149
|
|
|
* |
150
|
|
|
* @var string |
151
|
|
|
*/ |
152
|
|
|
private $initializeWith = false; |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Specifies that names should be displayed with the given name following the family name (e.g. “John Doe” becomes |
156
|
|
|
* “Doe, John”). The attribute has two possible values: |
157
|
|
|
* - “first” - attribute only has an effect on the first name of each name variable |
158
|
|
|
* - “all” - attribute has an effect on all names |
159
|
|
|
* Note that even when name-as-sort-order changes the name-part order, the display order is not necessarily the same |
160
|
|
|
* as the sorting order for names containing particles and suffixes (see Name-part order). Also, name-as-sort-order |
161
|
|
|
* only affects names written in the latin or Cyrillic alphabets. Names written in other alphabets (e.g. Asian |
162
|
|
|
* scripts) are always displayed with the family name preceding the given name. |
163
|
|
|
* |
164
|
|
|
* @var string |
165
|
|
|
*/ |
166
|
|
|
private $nameAsSortOrder = ""; |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Sets the delimiter for name-parts that have switched positions as a result of name-as-sort-order. The default |
170
|
|
|
* value is ”, ” (“Doe, John”). As is the case for name-as-sort-order, this attribute only affects names written in |
171
|
|
|
* the latin or Cyrillic alphabets. |
172
|
|
|
* |
173
|
|
|
* @var string |
174
|
|
|
*/ |
175
|
|
|
private $sortSeparator = ", "; |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Specifies whether all the name-parts of personal names should be displayed (value “long”, the default), or only |
179
|
|
|
* the family name and the non-dropping-particle (value “short”). A third value, “count”, returns the total number |
180
|
|
|
* of names that would otherwise be rendered by the use of the cs:names element (taking into account the effects of |
181
|
|
|
* et-al abbreviation and editor/translator collapsing), which allows for advanced sorting. |
182
|
|
|
* |
183
|
|
|
* @var string |
184
|
|
|
*/ |
185
|
|
|
private $form = "long"; |
186
|
|
|
|
187
|
|
|
private $nameForm = null; |
188
|
|
|
|
189
|
|
|
private $nameDelimiter; |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param SimpleXMLElement $node |
193
|
|
|
* @param RenderingMode|null $mode |
194
|
|
|
* @param NameOptions|null $nameOptions |
195
|
|
|
* @return NameOptions |
196
|
|
|
*/ |
197
|
173 |
|
public static function updateNameOptions( |
198
|
|
|
SimpleXMLElement $node, |
199
|
|
|
?RenderingMode $mode = null, |
200
|
|
|
?NameOptions $nameOptions = null |
201
|
|
|
): NameOptions { |
202
|
173 |
|
if (null === $nameOptions) { |
203
|
173 |
|
$nameOptions = clone CiteProc::getContext()->getNameOptions($mode); |
204
|
|
|
} |
205
|
173 |
|
foreach (self::$attributes as $nameAttribute) { |
206
|
173 |
|
$attribute = $node[$nameAttribute]; |
207
|
173 |
|
if (!empty($attribute)) { |
208
|
|
|
switch ($nameAttribute) { |
209
|
105 |
|
case 'and': |
210
|
62 |
|
$nameOptions->setAnd((string)$attribute); |
211
|
62 |
|
break; |
212
|
102 |
|
case 'delimiter-precedes-et-al': |
213
|
7 |
|
$nameOptions->setDelimiterPrecedesEtAl((string)$attribute); |
214
|
7 |
|
break; |
215
|
102 |
|
case 'delimiter-precedes-last': |
216
|
59 |
|
$nameOptions->setDelimiterPrecedesLast((string)$attribute); |
217
|
59 |
|
break; |
218
|
102 |
|
case 'et-al-min': |
219
|
70 |
|
$nameOptions->setEtAlMin(intval((string)$attribute)); |
220
|
70 |
|
break; |
221
|
102 |
|
case 'et-al-use-first': |
222
|
70 |
|
$nameOptions->setEtAlUseFirst(intval((string)$attribute)); |
223
|
70 |
|
break; |
224
|
99 |
|
case 'et-al-subsequent-min': |
225
|
11 |
|
$nameOptions->setEtAlSubsequentMin(intval((string)$attribute)); |
226
|
11 |
|
break; |
227
|
99 |
|
case 'et-al-subsequent-use-first': |
228
|
11 |
|
$nameOptions->setEtAlSubsequentUseFirst(intval((string)$attribute)); |
229
|
11 |
|
break; |
230
|
98 |
|
case 'et-al-use-last': |
231
|
22 |
|
$nameOptions->setEtAlUseLast(boolval((string)$attribute)); |
232
|
22 |
|
break; |
233
|
95 |
|
case 'initialize': |
234
|
4 |
|
$nameOptions->setInitialize(boolval((string)$attribute)); |
235
|
4 |
|
break; |
236
|
95 |
|
case 'initialize-with': |
237
|
52 |
|
$nameOptions->setInitializeWith((string)$attribute); |
238
|
52 |
|
break; |
239
|
91 |
|
case 'name-as-sort-order': |
240
|
59 |
|
$nameOptions->setNameAsSortOrder((string)$attribute); |
241
|
59 |
|
break; |
242
|
88 |
|
case 'sort-separator': |
243
|
52 |
|
$nameOptions->setSortSeparator((string)$attribute); |
244
|
52 |
|
break; |
245
|
86 |
|
case 'name-form': |
246
|
1 |
|
$nameOptions->setNameForm((string)$attribute); |
247
|
1 |
|
break; |
248
|
86 |
|
case 'form': |
249
|
61 |
|
$nameOptions->setForm((string)$attribute); |
250
|
61 |
|
break; |
251
|
63 |
|
case 'name-delimiter': |
252
|
1 |
|
$nameOptions->setNameDelimiter((string)$attribute); |
253
|
173 |
|
break; |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
173 |
|
return $nameOptions; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* @return string |
262
|
|
|
*/ |
263
|
105 |
|
public function getAnd(): ?string |
264
|
|
|
{ |
265
|
105 |
|
return $this->and; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param string $and |
270
|
|
|
*/ |
271
|
62 |
|
public function setAnd(string $and): void |
272
|
|
|
{ |
273
|
62 |
|
$this->and = $and; |
274
|
62 |
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return string |
278
|
|
|
*/ |
279
|
13 |
|
public function getDelimiterPrecedesEtAl(): ?string |
280
|
|
|
{ |
281
|
13 |
|
return $this->delimiterPrecedesEtAl; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @param string $delimiterPrecedesEtAl |
286
|
|
|
*/ |
287
|
7 |
|
public function setDelimiterPrecedesEtAl(string $delimiterPrecedesEtAl): void |
288
|
|
|
{ |
289
|
7 |
|
$this->delimiterPrecedesEtAl = $delimiterPrecedesEtAl; |
290
|
7 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
51 |
|
public function getDelimiterPrecedesLast(): ?string |
296
|
|
|
{ |
297
|
51 |
|
return $this->delimiterPrecedesLast; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param string $delimiterPrecedesLast |
302
|
|
|
*/ |
303
|
59 |
|
public function setDelimiterPrecedesLast(string $delimiterPrecedesLast): void |
304
|
|
|
{ |
305
|
59 |
|
$this->delimiterPrecedesLast = $delimiterPrecedesLast; |
306
|
59 |
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @return int |
310
|
|
|
*/ |
311
|
105 |
|
public function getEtAlMin(): ?int |
312
|
|
|
{ |
313
|
105 |
|
return $this->etAlMin; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param int $etAlMin |
318
|
|
|
*/ |
319
|
70 |
|
public function setEtAlMin(int $etAlMin): void |
320
|
|
|
{ |
321
|
70 |
|
$this->etAlMin = $etAlMin; |
322
|
70 |
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return int |
326
|
|
|
*/ |
327
|
54 |
|
public function getEtAlUseFirst(): ?int |
328
|
|
|
{ |
329
|
54 |
|
return $this->etAlUseFirst; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param int $etAlUseFirst |
334
|
|
|
*/ |
335
|
70 |
|
public function setEtAlUseFirst(int $etAlUseFirst): void |
336
|
|
|
{ |
337
|
70 |
|
$this->etAlUseFirst = $etAlUseFirst; |
338
|
70 |
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @return bool |
342
|
|
|
*/ |
343
|
86 |
|
public function isEtAlUseLast(): bool |
344
|
|
|
{ |
345
|
86 |
|
return $this->etAlUseLast; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @param bool $etAlUseLast |
350
|
|
|
*/ |
351
|
22 |
|
public function setEtAlUseLast(bool $etAlUseLast): void |
352
|
|
|
{ |
353
|
22 |
|
$this->etAlUseLast = $etAlUseLast; |
354
|
22 |
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @return int |
358
|
|
|
*/ |
359
|
|
|
public function getEtAlSubsequentMin(): ?int |
360
|
|
|
{ |
361
|
|
|
return $this->etAlSubsequentMin; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param int $etAlSubsequentMin |
366
|
|
|
*/ |
367
|
11 |
|
public function setEtAlSubsequentMin(int $etAlSubsequentMin): void |
368
|
|
|
{ |
369
|
11 |
|
$this->etAlSubsequentMin = $etAlSubsequentMin; |
370
|
11 |
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @return int |
374
|
|
|
*/ |
375
|
|
|
public function getEtAlSubsequentUseFirst(): ?int |
376
|
|
|
{ |
377
|
|
|
return $this->etAlSubsequentUseFirst; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @param int $etAlSubsequentUseFirst |
382
|
|
|
*/ |
383
|
11 |
|
public function setEtAlSubsequentUseFirst(int $etAlSubsequentUseFirst): void |
384
|
|
|
{ |
385
|
11 |
|
$this->etAlSubsequentUseFirst = $etAlSubsequentUseFirst; |
386
|
11 |
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @return bool |
390
|
|
|
*/ |
391
|
105 |
|
public function isInitialize(): bool |
392
|
|
|
{ |
393
|
105 |
|
return $this->initialize; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* @param bool $initialize |
398
|
|
|
*/ |
399
|
4 |
|
public function setInitialize(bool $initialize): void |
400
|
|
|
{ |
401
|
4 |
|
$this->initialize = $initialize; |
402
|
4 |
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @return string |
406
|
|
|
*/ |
407
|
105 |
|
public function getInitializeWith() |
408
|
|
|
{ |
409
|
105 |
|
return $this->initializeWith; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* @param string $initializeWith |
414
|
|
|
*/ |
415
|
52 |
|
public function setInitializeWith($initializeWith): void |
416
|
|
|
{ |
417
|
52 |
|
$this->initializeWith = $initializeWith; |
418
|
52 |
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @return string |
422
|
|
|
*/ |
423
|
105 |
|
public function getNameAsSortOrder(): string |
424
|
|
|
{ |
425
|
105 |
|
return $this->nameAsSortOrder; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @param string $nameAsSortOrder |
430
|
|
|
*/ |
431
|
59 |
|
public function setNameAsSortOrder(string $nameAsSortOrder): void |
432
|
|
|
{ |
433
|
59 |
|
$this->nameAsSortOrder = $nameAsSortOrder; |
434
|
59 |
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @return string |
438
|
|
|
*/ |
439
|
43 |
|
public function getSortSeparator(): string |
440
|
|
|
{ |
441
|
43 |
|
return $this->sortSeparator; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @param string $sortSeparator |
446
|
|
|
*/ |
447
|
52 |
|
public function setSortSeparator(string $sortSeparator): void |
448
|
|
|
{ |
449
|
52 |
|
$this->sortSeparator = $sortSeparator; |
450
|
52 |
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @return string |
454
|
|
|
*/ |
455
|
105 |
|
public function getForm(): ?string |
456
|
|
|
{ |
457
|
105 |
|
return $this->nameForm ?? $this->form; |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* @param string $form |
462
|
|
|
*/ |
463
|
61 |
|
public function setForm(string $form): void |
464
|
|
|
{ |
465
|
61 |
|
$this->form = $form; |
466
|
61 |
|
} |
467
|
|
|
|
468
|
|
|
/** |
469
|
|
|
* @return string |
470
|
|
|
*/ |
471
|
|
|
public function getNameForm(): string |
472
|
|
|
{ |
473
|
|
|
return $this->nameForm; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* @param string $nameForm |
478
|
|
|
*/ |
479
|
1 |
|
public function setNameForm(string $nameForm): void |
480
|
|
|
{ |
481
|
1 |
|
$this->nameForm = $nameForm; |
482
|
1 |
|
} |
483
|
|
|
|
484
|
|
|
/** |
485
|
|
|
* @return string|null |
486
|
|
|
*/ |
487
|
105 |
|
public function getNameDelimiter(): ?string |
488
|
|
|
{ |
489
|
105 |
|
return $this->nameDelimiter; |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* @param string $nameDelimiter |
494
|
|
|
*/ |
495
|
1 |
|
public function setNameDelimiter(string $nameDelimiter): void |
496
|
|
|
{ |
497
|
1 |
|
$this->nameDelimiter = $nameDelimiter; |
498
|
1 |
|
} |
499
|
|
|
} |
500
|
|
|
|