-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStyle.php
175 lines (144 loc) · 4.99 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
170
171
172
173
174
175
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// style
// class with static methods to generate an HTML style attribute
final class Style extends Listing
{
// config
protected static array $config = [
'option'=>[ // tableau d'options
'implode'=>1, // index du séparateur à utiliser lors du implode
'end'=>true, // ajoute le premier séparateur à la fin lors du implode
'extension'=>'jpg', // extension par défaut pour uri si non fourni
'uri'=>null], // option pour uri,
'separator'=>[ // les séparateurs de style
[';','; '],
[':',': ']],
'default'=>'background-image', // clé par défaut si style est string
'shortcut'=>[ // shortcut pour clé style
'bgimg'=>'background-image'],
'unit'=>[ // unit par défaut si la valeur est int ou float
'border-radius'=>'%',
'padding'=>'px',
'margin'=>'px',
'border'=>'px',
'outline'=>'px',
'font-size'=>'%',
'width'=>'%',
'min-'=>'%',
'max-'=>'%',
'height'=>'%',
'background'=>'%',
'top'=>'%',
'left'=>'%',
'right'=>'%',
'botton'=>'%']
];
// parse
// passe à travers un tableau style explosé
// ajoute un unit pour certaines clés, tel que défini dans config unit
// ajoute url pour background-image
// enlève les clés->valeurs dans un mauvais format
// les clés du tableau sont insensible à la case
final public static function parse(array $array,array $option):array
{
$return = [];
foreach ($array as $key => $value)
{
if(is_string($key) && (is_string($value) || is_numeric($value)))
{
if(array_key_exists($key,self::$config['shortcut']))
$key = self::$config['shortcut'][$key];
if((is_int($value) || is_float($value)))
{
foreach (self::$config['unit'] as $k => $unit)
{
if(strpos($key,$k) !== false)
{
$return[$key] = $value.$unit;
break;
}
}
}
if(!array_key_exists($key,$return))
{
$value = Str::cast($value);
if($key === 'background-image' && strpos($value,'url(') === false)
{
$value = self::parseUri($value,$option);
if(!empty($value))
$return[$key] = "url($value)";
}
else
$return[$key] = $value;
}
}
}
return $return;
}
// parseUri
// parse un champ uri (background-image)
final public static function parseUri($value,array $option):?string
{
$return = null;
if(is_string($value))
{
if(is_string($option['extension']))
{
$extension = Path::extension($value);
if(empty($extension))
$value = Path::changeExtension($option['extension'],$value);
}
$return = Uri::output($value,$option['uri'] ?? null);
}
return $return;
}
// prepareStr
// prépare une string dans la méthode arr
final public static function prepareStr(string $value,array $option):array
{
$return = [];
$separator = self::getSeparator(1,$option['explode']);
if(!empty($separator) && strpos($value,$separator))
$return = self::explodeStr($value,$option);
else
$return = [self::$config['default']=>$value];
return $return;
}
// explodeStr
// explode une string style
final public static function explodeStr(string $value,array $option):array
{
$return = [];
$separator = self::getSeparator(0,$option['explode']);
$separator2 = self::getSeparator(1,$option['explode']);
if(!empty($separator) && !empty($separator2))
{
$value = Str::explode($separator,$value,$option['limit']);
if(!empty($value))
$return = Arr::explodeKeyValue($separator2,$value);
}
return $return;
}
// getUriOption
// retourne les options uri pour style
final public static function getUriOption():array
{
return self::$config['option']['uri'];
}
// setUriOption
// change les options uri pour style
final public static function setUriOption(array $option):void
{
self::$config['option']['uri'] = Uri::option($option);
}
}
// init
Style::__init();
?>