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

Context::setNameOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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