-
Notifications
You must be signed in to change notification settings - Fork 0
/
Listing.php
284 lines (223 loc) · 8.52 KB
/
Listing.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?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;
// listing
// class to deal with associative strings and arrays
class Listing extends Assoc
{
// config
protected static array $config = [
'option'=>[ // tableau d'options
'implode'=>0, // index du séparateur à utiliser lors du implode
'explode'=>0, // index du séparateur à utiliser lors du explode
'case'=>null, // les clés sont ramenés dans cette case lors du explode
'caseImplode'=>null, // les clés sont ramenés dans cette case lors du implode
'limit'=>null, // limit par défaut lors du explode
'trim'=>true, // chaque partie de listing est trim
'clean'=>true, // une partie listing vide est retiré
'start'=>false, // ajoute le premier séparateur au début lors du implode
'end'=>false, // ajoute le premier séparateur à la fin lors du implode
'sort'=>null], // les clés sont sort
'separator'=>[ // les séparateurs de listing, le deuxième index est la version avec espace
["\n","\n"],
[':',': ']]
];
// isSeparatorStart
// retourne vrai si le listing a un separator au début
final public static function isSeparatorStart(string $listing):bool
{
return Str::isStart(static::getSeparator(),$listing,static::getSensitive());
}
// isSeparatorEnd
// retourne vrai si le listing a un separator à la fin et n'est pas seulement le séparateur
final public static function isSeparatorEnd(string $listing):bool
{
return $listing !== ($separator = static::getSeparator()) && Str::isEnd($separator,$listing,static::getSensitive());
}
// hasSeparatorDouble
// retourne vrai si le listing contient un double séparateur
final public static function hasSeparatorDouble(string $listing):bool
{
return !empty($separator = static::getSeparator()) && Str::posIpos($separator.$separator,$listing,static::getSensitive()) !== null;
}
// getSeparator
// retourne un des séparateurs via index
// possiblité de retourner la version avec espace via index2
final public static function getSeparator(int $index=0,int $index2=0):?string
{
$return = null;
if(array_key_exists($index,static::$config['separator']) && is_array(static::$config['separator'][$index]))
{
if(array_key_exists($index2,static::$config['separator'][$index]) && is_string(static::$config['separator'][$index][$index2]))
$return = static::$config['separator'][$index][$index2];
}
return $return;
}
// str
// explose et implose une valeur
// retourne une string correctement formattée
final public static function str($value,?array $option=null):string
{
return static::implode(static::arr($value,$option),$option);
}
// list
// les options de list peuvent être null
// explose et implose une valeur
// retourne un tableau unidimensionnel avec clé numérique
public static function list($array,?array $option=null):array
{
$return = [];
$option = static::option($option);
$separator = static::getSeparator(1,$option['implode']);
$array = static::arr($array,$option);
if(is_array($array) && !empty($array))
{
foreach ($array as $key => $value)
{
if(is_string($key) && (is_scalar($value) || is_array($value)))
{
$value = (array) $value;
foreach ($value as $v)
{
if(is_scalar($v))
{
$v = Str::cast($v);
$return[] = implode($separator,[$key,$v]);
}
}
}
}
}
return $return;
}
// parse
// parse un tableau arr
// pas utilisé dans listing
public static function parse(array $return,array $option):array
{
return $return;
}
// arr
// explose une string listing
// de même si listing est déjà un array, retourne le après parse
final public static function arr($value,?array $option=null):array
{
$return = [];
$option = static::option($option);
$value = Obj::cast($value);
if(is_scalar($value))
{
$value = Str::cast($value);
$value = static::prepareStr($value,$option);
}
if(is_array($value))
$value = static::prepareArr($value,$option);
if(is_array($value) && !empty($value))
{
$value = Arr::trimClean($value,$option['trim'],$option['trim'],$option['clean']);
if($option['case'] !== null)
$value = Arr::keysChangeCase($option['case'],$value);
$return = static::parse($value,$option);
if($option['sort'] !== null)
$return = Arr::keysSort($return,$option['sort']);
}
return $return;
}
// prepareStr
// prépare une string dans la méthode arr
public static function prepareStr(string $value,array $option):array
{
$return = [];
$separator = static::getSeparator(0,$option['explode']);
$return = Str::explode($separator,$value,$option['limit']);
return $return;
}
// prepareArr
// prépare un array dans la méthode arr
public static function prepareArr(array $value,array $option):array
{
$return = [];
if(Arr::isIndexed($value))
{
$separator = static::getSeparator(1,$option['explode']);
$return = Arr::explodeKeyValue($separator,$value);
}
else
$return = $value;
return $return;
}
// keyValue
// retourne la version unidimensionnel du tableau explode
// traite aussi les demandes de caseImplode
public static function keyValue(array $array,array $option):array
{
$return = [];
foreach ($array as $key => $value)
{
if(is_scalar($value))
$return[$key] = Str::cast($value);
}
if($option['caseImplode'] !== null)
$return = Arr::keysChangeCase($option['caseImplode'],$return);
return $return;
}
// implode
// implose un tableau qui a été passé dans arr
// fonctionne aussi avec les tableaux list
public static function implode(array $value,?array $option=null):string
{
$return = '';
$option = static::option($option);
$separator = static::getSeparator(0,$option['implode']);
if(Arr::isIndexed($value))
$return = implode($separator,$value);
else
{
$value = static::keyValue($value,$option);
if(Arr::isIndexed($value))
$return = implode($separator,$value);
else
$return = Arr::implodeKey($separator,static::getSeparator(1,$option['implode']),$value);
}
$return = static::stripWrap($return,$option['start'],$option['end']);
return $return;
}
// stripWrap
// ajoute ou enlève le séparateur en début ou fin de chaîne
final public static function stripWrap(string $listing,?bool $start=null,?bool $end=null):string
{
return Str::stripWrap(static::getSeparator(),$listing,$start,$end,static::getSensitive());
}
// stripStart
// retourne le listing sans le séparateur du début
final public static function stripStart(string $listing):string
{
return Str::stripStart(static::getSeparator(),$listing,static::getSensitive());
}
// stripEnd
// retourne le listing sans le séparateur de la fin
final public static function stripEnd(string $listing):string
{
return Str::stripEnd(static::getSeparator(),$listing,static::getSensitive());
}
// wrapStart
// wrap un listing au début s'il ne l'est pas déjà
final public static function wrapStart(string $listing):string
{
return Str::wrapStart(static::getSeparator(),$listing,static::getSensitive());
}
// wrapEnd
// wrap un listing à la fin s'il ne l'est pas déjà
final public static function wrapEnd(string $listing):string
{
return Str::wrapEnd(static::getSeparator(),$listing,static::getSensitive());
}
}
// init
Listing::__init();
?>