Passed
Push — new-api ( f151f9...5a646f )
by Sebastian
04:44
created

Context::isModeBibliography()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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;
11
12
use Seboettg\CiteProc\Config\RenderingMode;
13
use Seboettg\CiteProc\Config\RenderingState;
14
use Seboettg\CiteProc\Data\DataList;
15
use Seboettg\CiteProc\Locale\Locale;
16
use Seboettg\CiteProc\Rendering\Observer\CitationDataChangedEvent;
17
use Seboettg\CiteProc\Rendering\Observer\CitationItemsChangedEvent;
18
use Seboettg\CiteProc\Rendering\Observer\CitedItemsChangedEvent;
19
use Seboettg\CiteProc\Rendering\Observer\ModeChangedEvent;
20
use Seboettg\CiteProc\Rendering\Observer\RenderingEvent;
21
use Seboettg\CiteProc\Rendering\Observer\RenderingObservable;
22
use Seboettg\CiteProc\Rendering\Observer\RenderingObserver;
23
use Seboettg\CiteProc\Rendering\Observer\StateChangedEvent;
24
use Seboettg\CiteProc\Root\Info;
25
use Seboettg\CiteProc\Style\Bibliography;
26
use Seboettg\CiteProc\Style\Citation;
27
use Seboettg\CiteProc\Style\Macro;
28
use Seboettg\CiteProc\Style\Options\BibliographyOptions;
29
use Seboettg\CiteProc\Style\Options\CitationOptions;
30
use Seboettg\CiteProc\Style\Options\GlobalOptions;
31
use Seboettg\CiteProc\Style\Options\NameOptions;
32
use Seboettg\CiteProc\Style\Sort\Sort;
33
use Seboettg\CiteProc\Root\Root;
34
use Seboettg\CiteProc\Styles\Css\CssStyle;
35
use Seboettg\Collection\ArrayList;
36
use Seboettg\Collection\ArrayList\ArrayListInterface;
37
38
/**
39
 * Class Context
40
 * @package Seboettg\CiteProc
41
 *
42
 * @author Sebastian Böttger <[email protected]>
43
 */
44
class Context implements RenderingObservable
45
{
46
    /** @var ArrayListInterface */
47
    private $macros;
48
49
    /** @var Locale */
50
    private $locale;
51
52
    /** @var Bibliography */
53
    private $bibliography;
54
55
    /** @var Citation */
56
    private $citation;
57
58
    /** @var Sort */
59
    private $sorting;
60
61
    /** @var RenderingMode */
62
    private $mode;
63
64
    /** @var DataList */
65
    private $citationData;
66
67
    /** @var ArrayListInterface */
68
    private $citationItems;
69
70
    /** @var ArrayListInterface */
71
    private $results;
72
73
    /** @var Root */
74
    private $root;
75
76
    /** @var GlobalOptions */
77
    private $globalOptions;
78
79
    /** @var BibliographyOptions */
80
    private $bibliographySpecificOptions;
81
82
    /** @var CitationOptions */
83
    private $citationSpecificOptions;
84
85
    /** @var RenderingState */
86
    private $renderingState;
87
88
    /** @var CssStyle */
89
    private $cssStyle;
90
91
    /** @var Info */
92
    private $info;
93
94
    /** @var array */
95
    protected $markupExtension = [];
96
97
    /** @var bool */
98
    private $citationsAsArray = false;
99
100
    /** @var ArrayListInterface */
101
    private $citedItems;
102
103
    /** @var ArrayListInterface */
104
    private $observers;
105
106
    /** @var NameOptions */
107
    private $nameOptions;
108
109 183
    public function __construct($locale = null)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$locale" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$locale"; expected 0 but found 1
Loading history...
110
    {
111 183
        if (!empty($locale)) {
112
            $this->locale = $locale;
113
        }
114
115 183
        $this->macros = new ArrayList();
116 183
        $this->citationData = new DataList();
117 183
        $this->results = new ArrayList();
118 183
        $this->renderingState = RenderingState::RENDERING();
119 183
        $this->mode = RenderingMode::BIBLIOGRAPHY();
120 183
        $this->citedItems = new ArrayList();
121 183
        $this->citationItems = new ArrayList();
122 183
        $this->observers = new ArrayList();
123 183
        $this->nameOptions[Root::class] = new NameOptions();
124 183
    }
125
126 177
    public function addObserver(RenderingObserver $observer): void
127
    {
128 177
        $this->observers->append($observer);
129 177
        $observer->setContext($this);
130 177
    }
131
132 173
    public function notifyObservers(RenderingEvent $event)
133
    {
134
        /** @var RenderingObserver $observer */
135 173
        foreach ($this->observers as $observer) {
136 169
            $observer->notify($event);
137
        }
138 173
    }
139
140 173
    public function getNameOptions(?RenderingMode $mode = null): NameOptions
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$mode" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$mode"; expected 0 but found 1
Loading history...
141
    {
142 173
        if (null === $mode) {
143 173
            return $this->nameOptions[Root::class];
144
        } else {
145 118
            return $this->nameOptions[(string) $mode] ?? $this->nameOptions[Root::class];
146
        }
147
    }
148
149 173
    public function setNameOptions(NameOptions $nameOptions, ?RenderingMode $mode = null): void
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$mode" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$mode"; expected 0 but found 1
Loading history...
150
    {
151 173
        if (null === $mode) {
152 173
            $this->nameOptions[Root::class] = $nameOptions;
153
        } else {
154 173
            $this->nameOptions[(string) $mode] = $nameOptions;
155
        }
156 173
    }
157
158 71
    public function addMacro($key, $macro)
159
    {
160 71
        $this->macros->add($key, $macro);
161 71
    }
162
163
    /**
164
     * @param $key
165
     * @return Macro
166
     */
167 14
    public function getMacro($key)
168
    {
169 14
        return $this->macros->get($key);
170
    }
171
172
    /**
173
     * @param Locale $locale
174
     */
175 183
    public function setLocale(Locale $locale)
176
    {
177 183
        $this->locale = $locale;
178 183
    }
179
180
    /**
181
     * @return Locale
182
     */
183 183
    public function getLocale()
184
    {
185 183
        return $this->locale;
186
    }
187
188
    /**
189
     * @return Bibliography
190
     */
191 173
    public function getBibliography()
192
    {
193 173
        return $this->bibliography;
194
    }
195
196
    /**
197
     * @param Bibliography $bibliography
198
     */
199 87
    public function setBibliography(Bibliography $bibliography)
200
    {
201 87
        $this->bibliography = $bibliography;
202 87
    }
203
204
    /**
205
     * @return Citation
206
     */
207 173
    public function getCitation()
208
    {
209 173
        return $this->citation;
210
    }
211
212
    /**
213
     * @param Citation $citation
214
     */
215 160
    public function setCitation(Citation $citation)
216
    {
217 160
        $this->citation = $citation;
218 160
    }
219
220
    /**
221
     * @param $citationsAsArray
222
     */
223 173
    public function setCitationsAsArray($citationsAsArray = true)
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$citationsAsArray" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$citationsAsArray"; expected 0 but found 1
Loading history...
224
    {
225 173
        $this->citationsAsArray = $citationsAsArray;
226 173
    }
227
228 1
    public function isCitationsAsArray()
229
    {
230 1
        return $this->citationsAsArray;
231
    }
232
233 173
    public function setSorting(RenderingMode $mode, $sorting)
234
    {
235 173
        $this->sorting[(string)$mode] = $sorting;
236 173
    }
237
238
    public function getSorting()
239
    {
240
        return $this->sorting[(string)$this->mode] ?? null;
241
    }
242
243
    /**
244
     * return the render mode (citation|bibliography)
245
     * @return string
246
     */
247 173
    public function getMode()
248
    {
249 173
        return $this->mode;
250
    }
251
252
    /**
253
     * @param RenderingMode $mode
254
     */
255 173
    public function setMode(RenderingMode $mode)
256
    {
257 173
        $this->mode = $mode;
258 173
        $this->notifyObservers(new ModeChangedEvent($mode));
259 173
    }
260
261
    /**
262
     * returns true if the render mode is set to citation
263
     * @return bool
264
     */
265
    public function isModeCitation(): bool
266
    {
267
        return $this->mode->equals(RenderingMode::CITATION());
268
    }
269
270
    /**
271
     * returns true if the render mode is set to bibliography
272
     * @return bool
273
     */
274
    public function isModeBibliography(): bool
275
    {
276
        return $this->mode->equals(RenderingMode::BIBLIOGRAPHY());
277
    }
278
279
    /**
280
     * @return DataList
281
     */
282
    public function getCitationData(): DataList
283
    {
284
        return $this->citationData;
285
    }
286
287
    /**
288
     * @param ArrayListInterface|DataList $citationData
289
     */
290 1
    public function setCitationData($citationData)
291
    {
292 1
        $this->citationData = $citationData;
293 1
        $this->notifyObservers(new CitationDataChangedEvent($citationData));
294 1
    }
295
296
    /**
297
     * @return ArrayListInterface
298
     */
299
    public function getCitationItems(): ?ArrayListInterface
300
    {
301
        return $this->citationItems;
302
    }
303
304
    /**
305
     * @param ArrayListInterface $citationItems
306
     */
307
    public function setCitationItems(ArrayListInterface $citationItems): void
308
    {
309
        $this->citationItems = $citationItems;
310
        $this->notifyObservers(new CitationItemsChangedEvent($citationItems));
311
    }
312
313
    public function hasCitationItems(): bool
314
    {
315
        return ($this->citationData->count() > 0);
316
    }
317
318
    /**
319
     * @return ArrayList
320
     */
321 120
    public function getMacros()
322
    {
323 120
        return $this->macros;
324
    }
325
326
    /**
327
     * @return ArrayListInterface
328
     */
329 169
    public function getResults(): ArrayListInterface
330
    {
331 169
        return $this->results;
332
    }
333
334
    /**
335
     * @return Root
336
     */
337
    public function getRoot()
338
    {
339
        return $this->root;
340
    }
341
342
    /**
343
     * @param Root $root
344
     */
345 173
    public function setRoot(Root $root)
346
    {
347 173
        $this->root = $root;
348 173
    }
349
350
    /**
351
     * @return GlobalOptions
352
     */
353 159
    public function getGlobalOptions(): ?GlobalOptions
354
    {
355 159
        return $this->globalOptions;
356
    }
357
358
    /**
359
     * @param GlobalOptions $globalOptions
360
     */
361 173
    public function setGlobalOptions(GlobalOptions $globalOptions): void
362
    {
363 173
        $this->globalOptions = $globalOptions;
364 173
    }
365
366
    /**
367
     * @return RenderingState
368
     */
369
    public function getRenderingState()
370
    {
371
        return $this->renderingState;
372
    }
373
374
    /**
375
     * @param RenderingState|string $renderingState
376
     */
377
    public function setRenderingState(RenderingState $renderingState)
378
    {
379
        $this->renderingState = $renderingState;
380
        $this->notifyObservers(new StateChangedEvent($renderingState));
381
    }
382
383
    /**
384
     * @return BibliographyOptions
385
     */
386 173
    public function getBibliographySpecificOptions(): ?BibliographyOptions
387
    {
388 173
        return $this->bibliographySpecificOptions;
389
    }
390
391
    /**
392
     * @param BibliographyOptions $bibliographySpecificOptions
393
     */
394 87
    public function setBibliographySpecificOptions(BibliographyOptions $bibliographySpecificOptions)
395
    {
396 87
        $this->bibliographySpecificOptions = $bibliographySpecificOptions;
397 87
    }
398
399
    /**
400
     * @return CitationOptions
401
     */
402
    public function getCitationSpecificOptions(): ?CitationOptions
403
    {
404
        return $this->citationSpecificOptions;
405
    }
406
407
    /**
408
     * @param CitationOptions $citationSpecificOptions
409
     */
410 160
    public function setCitationSpecificOptions(CitationOptions $citationSpecificOptions)
411
    {
412 160
        $this->citationSpecificOptions = $citationSpecificOptions;
413 160
    }
414
415
    /**
416
     * @param CssStyle $cssStyle
417
     */
418
    public function setCssStyle(CssStyle $cssStyle)
419
    {
420
        $this->cssStyle = $cssStyle;
421
    }
422
423
    /**
424
     * @return CssStyle
425
     */
426
    public function getCssStyle()
427
    {
428
        return $this->cssStyle;
429
    }
430
431 163
    public function setInfo(Info $info)
432
    {
433 163
        $this->info = $info;
434 163
    }
435
436 1
    public function getInfo()
437
    {
438 1
        return $this->info;
439
    }
440
441
    /**
442
     * @return array
443
     */
444 173
    public function getMarkupExtension()
445
    {
446 173
        return $this->markupExtension;
447
    }
448
449
    /**
450
     * @param callable[] $markupExtension
451
     */
452 173
    public function setMarkupExtension(array $markupExtension)
453
    {
454 173
        $this->markupExtension = $markupExtension;
455 173
    }
456
457
    public function getCitationItemById($id)
458
    {
459
        return $this->citationItems->filter(function ($item) use ($id) {
460
            return $item->id === $id;
461
        })->current();
462
    }
463
464
    /**
465
     * @return ArrayListInterface
466
     */
467
    public function getCitedItems(): ArrayListInterface
468
    {
469
        return $this->citedItems;
470
    }
471
472
    /**
473
     * @param ArrayListInterface $citedItems
474
     */
475
    public function setCitedItems(ArrayListInterface $citedItems): void
476
    {
477
        $this->citedItems = $citedItems;
478
        $this->notifyObservers(new CitedItemsChangedEvent($citedItems));
479
    }
480
481
    public function appendCitedItem($citedItem)
482
    {
483
        $this->citedItems->append($citedItem);
484
        $this->notifyObservers(new CitedItemsChangedEvent($this->citedItems));
485
    }
486
}
487