Completed
Push — master ( 3efc81...a16fdb )
by Fabien
03:51
created

StandardFacet::__set_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Fab\Vidi\Facet;
3
4
/*
5
 * This file is part of the Fab/Vidi project under GPLv2 or later.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.md file that was distributed with this source code.
9
 */
10
11
use Fab\Vidi\Persistence\Matcher;
12
use Fab\Vidi\Tca\Tca;
13
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
14
use TYPO3\CMS\Core\Utility\GeneralUtility;
15
use TYPO3\CMS\Lang\LanguageService;
16
17
/**
18
 * Class for configuring a custom Facet item.
19
 */
20
class StandardFacet implements FacetInterface
21
{
22
23
    /**
24
     * @var string
25
     */
26
    protected $name;
27
28
    /**
29
     * @var string
30
     */
31
    protected $label;
32
33
    /**
34
     * @var array
35
     */
36
    protected $suggestions = [];
37
38
    /**
39
     * @var string
40
     */
41
    protected $dataType;
42
43
    /**
44
     * @var bool
45
     */
46
    protected $canModifyMatcher = false;
47
48
    /**
49
     * Constructor of a Generic Facet in Vidi.
50
     *
51
     * @param string $name
52
     * @param string $label
53
     * @param array $suggestions
54
     */
55
    public function __construct($name, $label = '', array $suggestions = [])
56
    {
57
        $this->name = $name;
58
        if (empty($label)) {
59
            $label = $this->name;
60
        }
61
        $this->label = $label;
62
        $this->suggestions = $suggestions;
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    public function getName(): string
69
    {
70
        return $this->name;
71
    }
72
73
    /**
74
     * @return string
75
     */
76 View Code Duplication
    public function getLabel(): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        if ($this->label === $this->name) {
79
            $label = Tca::table($this->dataType)->field($this->getName())->getLabel();
80
        } else {
81
            try {
82
                $label = LocalizationUtility::translate($this->label, '');
83
            } catch (\InvalidArgumentException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
84
            }
85
            if (empty($label)) {
86
                $label = $this->label;
87
            }
88
        }
89
90
        return $label;
91
    }
92
93
    /**
94
     * @return array
95
     */
96 View Code Duplication
    public function getSuggestions(): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
99
        $values = [];
100
        foreach ($this->suggestions as $key => $label) {
101
            $localizedLabel = $this->getLanguageService()->sL($label);
102
            if (!empty($localizedLabel)) {
103
                $label = $localizedLabel;
104
            }
105
106
            $values[] = [$key => $label];
107
        }
108
109
        return $values;
110
    }
111
112
    /**
113
     * @return LanguageService
114
     */
115 View Code Duplication
    protected function getLanguageService(): LanguageService
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        /** @var LanguageService $langService */
118
        $langService = $GLOBALS['LANG'];
119
        if (!$langService) {
120
            $langService = GeneralUtility::makeInstance(LanguageService::class);
121
            $langService->init('en');
122
        }
123
124
        return $langService;
125
    }
126
127
    /**
128
     * @return bool
129
     */
130
    public function hasSuggestions(): bool
131
    {
132
        return !empty($this->suggestions);
133
    }
134
135
    /**
136
     * @param string $dataType
137
     * @return $this
138
     */
139
    public function setDataType($dataType): self
140
    {
141
        $this->dataType = $dataType;
142
        return $this;
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function canModifyMatcher(): bool
149
    {
150
        return $this->canModifyMatcher;
151
    }
152
153
    /**
154
     * @param Matcher $matcher
155
     * @param $value
156
     * @return Matcher
157
     */
158
    public function modifyMatcher(Matcher $matcher, $value): Matcher
159
    {
160
        return $matcher;
161
    }
162
163
}
164