Completed
Push — master ( 71b299...4ab862 )
by Fabien
55:03
created

StandardFacet::getLanguageService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Fab\Vidi\Facet;
3
4
/**
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use Fab\Vidi\Persistence\Matcher;
18
use Fab\Vidi\Tca\Tca;
19
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
20
21
/**
22
 * Class for configuring a custom Facet item.
23
 */
24
class StandardFacet implements FacetInterface
25
{
26
27
    /**
28
     * @var string
29
     */
30
    protected $name;
31
32
    /**
33
     * @var string
34
     */
35
    protected $label;
36
37
    /**
38
     * @var array
39
     */
40
    protected $suggestions = array();
41
42
    /**
43
     * @var string
44
     */
45
    protected $dataType;
46
47
    /**
48
     * @var bool
49
     */
50
    protected $canModifyMatcher = false;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $forceKeyValueObject = false;
56
57
    /**
58
     * Constructor of a Generic Facet in Vidi.
59
     *
60
     * @param string $name
61
     * @param string $label
62
     * @param array $suggestions
63
     * @param bool $forceKeyValueObject
64
     */
65
    public function __construct($name, $label = '', array $suggestions = array(), $forceKeyValueObject = false)
66
    {
67
        $this->name = $name;
68
        if (empty($label)) {
69
            $label = $this->name;
70
        }
71
        $this->label = $label;
72
        $this->suggestions = $suggestions;
73
        $this->forceKeyValueObject = $forceKeyValueObject;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getName()
80
    {
81
        return $this->name;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getLabel()
88
    {
89
        if ($this->label === $this->name) {
90
            $label = Tca::table($this->dataType)->field($this->getName())->getLabel();
91
        } else {
92
            $label = LocalizationUtility::translate($this->label, '');
93
            if (empty($label)) {
94
                $label = $this->label;
95
            }
96
        }
97
98
        return $label;
99
    }
100
101
    /**
102
     * @return array
103
     */
104
    public function getSuggestions()
105
    {
106
107
        $values = array();
108
        foreach ($this->suggestions as $key => $label) {
109
            $localizedLabel = $this->getLanguageService()->sL($label);
110
            if (!empty($localizedLabel)) {
111
                $label = $localizedLabel;
112
            }
113
114
            if ($this->forceKeyValueObject) {
115
                $values[] = [$key => $label];
116
            } else {
117
118
                // Hack to have object correctly json encoded.
119
                $values[$key] = $label;
120
            }
121
        }
122
123
        return $values;
124
    }
125
126
    /**
127
     * @return \TYPO3\CMS\Lang\LanguageService
128
     */
129
    protected function getLanguageService()
0 ignored issues
show
Coding Style introduced by
getLanguageService uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
130
    {
131
        return $GLOBALS['LANG'];
132
    }
133
134
    /**
135
     * @return bool
136
     */
137
    public function hasSuggestions()
138
    {
139
        return !empty($this->suggestions);
140
    }
141
142
    /**
143
     * @param string $dataType
144
     * @return $this
145
     */
146
    public function setDataType($dataType)
147
    {
148
        $this->dataType = $dataType;
149
        return $this;
150
    }
151
152
    /**
153
     * @return bool
154
     */
155
    public function canModifyMatcher()
156
    {
157
        return $this->canModifyMatcher;
158
    }
159
160
    /**
161
     * @param Matcher $matcher
162
     * @param $value
163
     * @return Matcher
164
     */
165
    public function modifyMatcher(Matcher $matcher, $value)
166
    {
167
        return $matcher;
168
    }
169
170
    /**
171
     * Magic method implementation for retrieving state.
172
     *
173
     * @param array $states
174
     * @return StandardFacet
175
     */
176
    static public function __set_state($states)
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
177
    {
178
        return new StandardFacet($states['name'], $states['label'], $states['suggestions']);
179
    }
180
181
}
182