/
Style.php
169 lines (150 loc) · 4.46 KB
/
Style.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
namespace Concrete\Core\StyleCustomizer\Style;
use Concrete\Core\Filesystem\FileLocator;
use Concrete\Core\Support\Facade\Application;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* @method static \Concrete\Core\StyleCustomizer\Style\Value\Value[] getValuesFromVariables($rules = [])
*/
abstract class Style
{
/**
* The name of this style.
*
* @var string
*/
protected $name = '';
/**
* The name of the associated CSS variable.
*
* @var string
*/
protected $variable = '';
/**
* Render the control of this style.
*
* @param \Concrete\Core\StyleCustomizer\Style\Value\Value|null|false $value the current style value
*/
abstract public function render($value = false);
/*
* This is commented out only because PHP raises a "strict standards" warning for PHP prior to version 7.0,
* but child classes MUST implement it (see also https://bugs.php.net/bug.php?id=72993 )
*/
// abstract public static function getValuesFromVariables($rules = []);
/**
* Get the value of this style as received from a request.
*
* @param \Symfony\Component\HttpFoundation\ParameterBag $request the received data
*
* @return \Concrete\Core\StyleCustomizer\Style\Value\Value|null
*/
abstract public function getValueFromRequest(ParameterBag $request);
/**
* Get the value of this style extracted from a list of values.
*
* @param \Concrete\Core\StyleCustomizer\Style\ValueList $list
*
* @return \Concrete\Core\StyleCustomizer\Style\Value\Value|null
*/
public function getValueFromList(ValueList $list)
{
$type = static::getTypeFromClass($this);
foreach ($list->getValues() as $value) {
if ($value->getVariable() == $this->getVariable() && $type == static::getTypeFromClass($value, 'Value')) {
return $value;
}
}
}
/**
* Get the type handle of a given Style instance.
*
* @param \Concrete\Core\StyleCustomizer\Style\Style|object $class
* @param string $suffix
*
* @return string
*/
protected static function getTypeFromClass($class, $suffix = 'Style')
{
$class = get_class($class);
$class = substr($class, strrpos($class, '\\') + 1);
$type = uncamelcase(substr($class, 0, strrpos($class, $suffix)));
return $type;
}
/**
* Set the name of this style.
*
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = (string) $name;
return $this;
}
/**
* Get the name of this style.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get the display name for this style (localized and escaped accordingly to $format).
*
* @param string $format = 'html'
* Escape the result in html format (if $format is 'html').
* If $format is 'text' or any other value, the display name won't be escaped.
*
* @return string
*/
public function getDisplayName($format = 'html')
{
$value = tc('StyleName', $this->getName());
switch ($format) {
case 'html':
return h($value);
case 'text':
default:
return $value;
}
}
/**
* Set the name of the associated CSS variable.
*
* @param string $variable
*
* @return $this
*/
public function setVariable($variable)
{
$this->variable = (string) $variable;
return $this;
}
/**
* Get the name of the associated CSS variable.
*
* @return string
*/
public function getVariable()
{
return $this->variable;
}
/**
* Get a path to an elements directory for this Style. Might not be used by all styles.
*
* @return string
*/
public function getFormElementPath()
{
$app = Application::getFacadeApplication();
$className = implode('', array_slice(explode('\\', get_called_class()), -1));
$segment = substr($className, 0, strpos($className, 'Style'));
$element = uncamelcase($segment);
$locator = $app->make(FileLocator::class);
$record = $locator->getRecord(DIRNAME_ELEMENTS . '/' . DIRNAME_STYLE_CUSTOMIZER . '/' . DIRNAME_STYLE_CUSTOMIZER_TYPES . '/' . $element . '.php');
return $record->getFile();
}
}