Passed
Push — new-api ( 34a0a9...30b18d )
by Sebastian
04:06
created

Context::isModeBibliography()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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