Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
HasConfigurableMarkdownFeatures
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
8 / 8
15
100.00% covered (success)
100.00%
1 / 1
 addFeature
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 removeFeature
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 withTableOfContents
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 withPermalinks
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasFeature
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 canEnablePermalinks
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 isDocumentationPage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 canEnableTorchlight
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Hyde\Framework\Concerns\Markdown;
4
5use Hyde\Framework\Helpers\Features;
6use Hyde\Framework\Helpers\Markdown;
7use Hyde\Framework\Models\DocumentationPage;
8
9/**
10 * Allow the Markdown service to have configurable features.
11 *
12 * @see HasMarkdownFeatures for global feature management.
13 */
14trait HasConfigurableMarkdownFeatures
15{
16    protected array $features = [];
17
18    public function addFeature(string $feature): self
19    {
20        if (! in_array($feature, $this->features)) {
21            $this->features[] = $feature;
22        }
23
24        return $this;
25    }
26
27    public function removeFeature(string $feature): self
28    {
29        if (in_array($feature, $this->features)) {
30            $this->features = array_diff($this->features, [$feature]);
31        }
32
33        return $this;
34    }
35
36    public function withTableOfContents(): self
37    {
38        $this->addFeature('table-of-contents');
39
40        return $this;
41    }
42
43    public function withPermalinks(): self
44    {
45        $this->addFeature('permalinks');
46
47        return $this;
48    }
49
50    public function hasFeature(string $feature): bool
51    {
52        return in_array($feature, $this->features);
53    }
54
55    public function canEnablePermalinks(): bool
56    {
57        if ($this->hasFeature('permalinks')) {
58            return true;
59        }
60
61        if ($this->isDocumentationPage() && Markdown::hasTableOfContents()) {
62            return true;
63        }
64
65        return false;
66    }
67
68    public function isDocumentationPage(): bool
69    {
70        return isset($this->sourceModel) && $this->sourceModel === DocumentationPage::class;
71    }
72
73    public function canEnableTorchlight(): bool
74    {
75        return $this->hasFeature('torchlight') ||
76                    Features::hasTorchlight();
77    }
78}