-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAttr.php
1231 lines (963 loc) · 37.4 KB
/
Attr.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <[email protected]>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// attr
// class with static methods to generate HTML attributes
final class Attr extends Listing
{
// config
protected static array $config = [
'option'=>[ // tableau d'options
'explode'=>1, // index du séparateur à utiliser lors du explode
'trim'=>false, // chaque partie de attr est trim
'clean'=>false, // une partie attr vide est retiré
'alt'=>null, // option pour slug de alt
'style'=>null, // option pour style
'json'=>null, // option pour json
'uri'=>null, // option pour uri, html passe un tableau différent pour chaque tag
'encode'=>true, // si les attributs sont passés dans htmlspecialchars ou htmlentities lors du implode, true est specialchars
'quote'=>false, // null est pas de quote, false est single quote, true est double quote
'href'=>[
'active'=>true, // permet d'activer ou désactiver les options href selected, targetExternal et lang
'selected'=>'selected', // classe pour les selected href
'targetExternal'=>'_blank', // target si le href est externe
'lang'=>true, // détecte la lang pour href
'mailto'=>true, // détecte les courriels
'tel'=>true, // détecte les téléphones
'extension'=>null], // extension par défaut pour href si non fourni et uri relative
'src'=>[
'extension'=>'jpg'], // extension par défaut pour src si non fourni et uri relative
'action'=>[
'extension'=>null]], // extension par défaut pour action si non fourni et uri relative
'separator'=>[ // les séparateurs de attr, le troisième index est pour explode
[' ',' '],
['=','=']],
'id'=>'#', // ce caractère sert à identifier un id
'randomId'=>[10], // argument par défaut pour randomId
'class'=>'.', // ce caractère sert à identifier une classe
'oddEven'=>['odd','even'], // les classes pour oddEven
'mirror'=>['multiple','selected','checked','required','disabled'], // attributs mirror
'boolean'=>['async'], // attributs boolean
'pattern'=>['pattern','data-pattern'], // noms d'attributs qui contiennent pattern et doivent être passés dans validate/pattern
'method'=>'post', // si method est true
'target'=>'_blank', // si target est true
'group'=>[ // group d'attributs
'ajax'=>['ajax','rel'=>'nofollow']],
'style'=>[ // si ces clés se retrouvent au premier niveau de attr, ils sont envoyés dans un tableau style
'color',
'padding',
'margin',
'border',
'outline',
'background',
'background-image',
'bgimg',
'background-position']
];
// selectedUri
protected static array $selectedUri = []; // tableau qui contient les uri sélectionnés
// isDataKey
// retourne vrai si l'attribut est data
final public static function isDataKey($value):bool
{
return is_string($value) && strpos($value,'data-') === 0 && strlen($value) > 5;
}
// isDataUri
// retourne vrai si l'attribut est une data-uri -> string encodé en base 64
final public static function isDataUri($value):bool
{
return is_string($value) && strpos($value,'data:') === 0 && strpos($value,';base64,') !== false;
}
// isSelectedUri
// retourne vrai si l'uri fourni est sélectionné
final public static function isSelectedUri($value):bool
{
return is_string($value) && array_key_exists($value,self::$selectedUri);
}
// isTel
// retourne vrai si la valeur est un téléphone
final public static function isTel($value):bool
{
$return = false;
if(is_scalar($value))
{
$value = (string) $value;
if(strlen($value) < 20 && Validate::isPhone($value))
$return = true;
}
return $return;
}
// hasId
// retourne vrai si les attributs ont le id spécifiés
final public static function hasId(string $value,$attr):bool
{
return self::getId($attr) === $value;
}
// hasClass
// retourne vrai si les attributs ont la ou les classes spécifiés
final public static function hasClass($value,$attr):bool
{
$value = (array) self::parseClass($value);
$class = (array) self::getClass($attr);
return !empty($value) && !empty($class) && Arr::ins($value,$class);
}
// styleExists
// retourne vrai si les attributs ont la clé style spécifié
final public static function styleExists(string $key,$attr,?array $option=null):bool
{
return ($style = self::style($attr,$option)) && Arr::keyExists($key,$style,self::getSensitive());
}
// stylesExists
// retourne vrai si les attributs ont les clés style spécifiés
final public static function stylesExists(array $keys,$attr,?array $option=null):bool
{
return ($style = self::style($attr,$option)) && Arr::keysExists($keys,$style,self::getSensitive());
}
// dataExists
// retourne vrai si les attributs ont la clé data spécifié
final public static function dataExists(string $key,$attr,?array $option=null):bool
{
return ($data = self::data($attr,$option)) && Arr::keyExists(self::parseDataKey($key),$data,self::getSensitive());
}
// datasExists
// retourne vrai si les attributs ont les clés data spécifié
final public static function datasExists(array $keys,$attr,?array $option=null):bool
{
$return = false;
$data = self::data($attr,$option);
$dataKeys = self::parseDataKeys($keys);
if(!empty($data) && count($keys) === count($dataKeys))
$return = Arr::keysExists($dataKeys,$data,self::getSensitive());
return $return;
}
// compare
// compare différents chaînes attributs
// doivent avoir même clé, même count, même valeur, même classe et même clés de style
// l'ordre peut être différent entre les tableaux
final public static function compare(...$values):bool
{
$return = false;
if(count($values) > 1)
{
foreach ($values as $key => $value)
{
$values[$key] = self::arr($value);
if(!empty($values[$key]['class']))
$values[$key]['class'] = array_fill_keys($values[$key]['class'],true);
$values[$key] = Arrs::keysSort($values[$key]);
}
$return = Arr::sameKeyValue(...$values);
}
return $return;
}
// prepareParse
// prépare le tableau en provenant de arr avant parse
final protected static function prepareParse(array $return,array $option):array
{
// prepareStyle
if(array_key_exists('style',$return) && !is_array($return['style']))
$return['style'] = self::parseStyle($return['style'],$option['style'] ?? null);
// parseClass
if(array_key_exists('class',$return) && !is_array($return['class']))
$return['class'] = (array) self::parseClass($return['class']);
// clé numérique
foreach (array_keys($return) as $key)
{
if(is_numeric($key))
{
$basic = self::parseBasic($return[$key]);
if(!empty($basic['id']))
$return['id'] = $basic['id'];
if(!empty($basic['class']))
$return = self::parseMerge(['class'=>$basic['class']],$return);
unset($return[$key]);
}
}
foreach ($return as $key => $value)
{
if(is_string($key))
{
// style key
if(in_array($key,self::$config['style'],true))
{
$return['style'][$key] = $value;
unset($return[$key]);
}
// oddEven
elseif($key === 'oddEven' && is_numeric($value))
{
$oddEven = self::getOddEvenClass($value);
if(is_string($oddEven))
$return['class'][] = $oddEven;
unset($return[$key]);
}
// parse Uri + hrefTargetExternal, hrefSelected et hrefLang et hrefMailto
elseif($key === 'href')
$return = self::prepareParseHref($value,$return,$option);
}
}
return $return;
}
// prepareParseHref
// méthode protégé pour parseHref
final protected static function prepareParseHref($value,array $return,array $option):array
{
$key = 'href';
$href = $option[$key];
if($href['active'] === true && !empty($href['mailto']) && Email::is($value))
$value = 'mailto:'.$value;
elseif($href['active'] === true && !empty($href['tel']) && self::isTel($value))
$value = 'tel:'.static::prepareTel($value);
else
{
$value = self::parseUri($value,$option[$key]['extension'] ?? null);
if(is_string($value))
{
if($href['active'] === true)
{
$isExternal = Uri::isExternal($value);
if(is_string($href['targetExternal']) && !array_key_exists('target',$return) && $isExternal === true)
$return['target'] = $href['targetExternal'];
if(!empty($href['selected']) && self::isSelectedUri($value))
{
$selected = self::getSelectedUri($value,$href['selected']);
$return['class'][] = $selected;
}
if(!empty($href['lang']) && $isExternal === false)
{
$lang = Uri::lang($value);
if(!empty($lang))
$return['hreflang'] = $lang;
}
}
$value = self::outputUri($value,$option['uri']);
}
}
if(is_string($value))
$return[$key] = $value;
return $return;
}
// prepareGroup
// prépare et append les groups à l'intérieur d'un tableau d'attributs
// retourne null si le tableau d'attribut ne contient pas de group
final protected static function prepareGroup(array $array,array $option):?array
{
$return = null;
if(array_key_exists('group',$array))
{
$groups = (array) $array['group'];
unset($array['group']);
if(!empty($groups))
{
$append = [];
foreach ($groups as $group)
{
if(array_key_exists($group,self::$config['group']))
$append[] = self::$config['group'][$group];
}
if(!empty($append))
$return = self::prepend($array,...$append);
}
}
return $return;
}
// prepareTel
// prépare le numéro de téléphone
final protected static function prepareTel($value):string
{
return Str::keepNumber((string) $value,"\+");
}
// parse
// passe à travers un tableau attr explosé
// si le tableau contient un ou des groupes, il n'est pas parse à nouveau
final public static function parse(array $array,array $option):array
{
$return = [];
$group = self::prepareGroup($array,$option);
if(is_array($group))
$return = $group;
else
{
foreach (self::prepareParse($array,$option) as $key => $value)
{
if(is_string($key) && !empty($key))
{
if($key === 'alt')
$value = self::parseAlt($value,$option['alt']);
elseif($key === 'id')
$value = self::parseId($value);
elseif($key === 'class')
$value = self::parseClass($value);
elseif(in_array($key,['src','action'],true) && self::isDataUri($value) === false)
{
$value = self::parseUri($value,$option[$key]['extension'] ?? null);
if(is_string($value))
$value = self::outputUri($value,$option['uri']);
}
elseif($key === 'style')
$value = self::parseStyle($value,$option['style'] ?? null);
elseif($key === 'data' && is_array($value))
$value = self::parseData($value,$option['case']);
elseif($key === 'method' && is_bool($value))
$value = ($value === true)? (self::$config['method'] ?? null):null;
elseif($key === 'target' && is_bool($value))
$value = ($value === true)? (self::$config['target'] ?? null):null;
elseif(in_array($key,self::$config['mirror'],true) && is_bool($value))
$value = ($value === true)? $key:null;
elseif(in_array($key,self::$config['boolean'],true) && is_bool($value))
$value = ($value === true)? true:null;
if(is_string($key) && !empty($key) && $value !== null)
$return = self::parseMerge([$key=>$value],$return);
}
}
}
return $return;
}
// implode
// hack pour pouvoir générer un champ boolean
public static function implode(array $value,?array $option=null):string
{
$return = parent::implode($value,$option);
foreach(self::$config['boolean'] as $booleanField)
{
if(!empty($value[$booleanField]))
{
$target = $booleanField."='1'";
$return = Str::replace([$target=>$booleanField],$return);
}
}
return $return;
}
// parseBasic
// parse une entrée basic du tableau attr
// ceci signifie une valeur d'une clé numérique du tableau
// peut seulement contenir id ou classe
final public static function parseBasic($value):?array
{
$return = null;
$id = self::$config['id'];
$class = self::$config['class'];
$classes = [];
if(is_array($value))
{
if(Arrs::is($value))
$value = Arrs::implode(' ',$value);
else
$value = implode(' ',$value);
}
if(is_string($value) && !empty($value))
{
if(!empty($id) && strpos($value,$id) !== false)
$value = str_replace($id," $id",$value);
if(!empty($class) && strpos($value,$class) !== false)
$value = str_replace($class," $class",$value);
foreach (Str::wordExplode($value) as $key => $value)
{
if(strlen($value) > 1)
{
if(!empty($id) && strpos($value,$id) === 0)
{
$id = substr($value,strlen($id));
$parse = self::parseId($id);
if(!empty($parse))
$return = ['id'=>$parse];
}
else
{
if(!empty($class) && strpos($value,$class) === 0)
$value = substr($value,strlen($class));
$classes[] = $value;
}
}
}
if(!empty($classes))
{
$classes = self::parseClass($classes);
if(!empty($classes))
$return['class'] = $classes;
}
}
return $return;
}
// parseId
// parse une valeur qui pourrait être un id
// si value est true, envoie à randomId
final public static function parseId($value):?string
{
$return = null;
if($value === true)
$return = self::randomId();
elseif(is_string($value) && Validate::isAlphanumericDash($value))
$return = $value;
return $return;
}
// parseAlt
// parse un champ alt
// le retour est une slug
final public static function parseAlt($value,?array $option=null):?string
{
return (is_string($value) && !empty($value))? Slug::str($value,$option):null;
}
// parseClass
// parse une valeur classe, peut être string ou array
// si return est fourni, les classes uniques sont ajoutés à return
final public static function parseClass($value,?array $return=null):?array
{
if(!empty($value))
{
if(is_string($value) && !is_numeric($value))
$value = [$value];
if(is_array($value))
{
foreach ($value as $v)
{
if(is_string($v))
$return = self::explodeClass($v,$return);
}
}
}
return $return;
}
// parseUri
// parse un champ uri (href, action ou src)
// extension par défaut seulement ajouté si uri relative
final public static function parseUri($value,?string $extension=null):?string
{
$return = null;
if(is_scalar($value))
{
$return = Str::cast($value);
if(is_string($extension) && !Uri::isAbsolute($return))
{
$ext = Path::extension($return);
if(empty($ext))
$return = Path::changeExtension($extension,$return);
}
}
return $return;
}
// outputUri
// méthode utilisé pour faire un output d'uri
// option append est enlevé si l'uri est absolute
final public static function outputUri(string $value,?array $option=null):string
{
$option = Arr::plus(['append'=>null],$option);
if(!empty($option['append']) && Uri::isAbsolute($value))
{
$host = Uri::host($value);
if(!empty($host) && empty(Finder::getHostPaths($host)))
$option['append'] = null;
}
return Uri::output($value,$option);
}
// parseStyle
// parse un champ style
final public static function parseStyle($value,array $option=null):?array
{
return (!empty($value) && (is_string($value) || is_array($value)))? Style::arr($value,$option):null;
}
// parseData
// parse un champ data qui est un tableau avec multiples clés datas
final public static function parseData(array $value,$case=null):?array
{
$return = null;
if(!empty($value))
{
$return = [];
if($case !== null)
$value = Arr::keysChangeCase($case,$value);
foreach ($value as $k => $v)
{
$k = self::parseDataKey($k);
if(is_string($k))
$return[$k] = $v;
}
}
return $return;
}
// parseDataKey
// parse une clé data, ajoute data- au besoin
// traite aussi les entrées data camelCase
final public static function parseDataKey(string $return):string
{
if(!self::isDataKey($return))
$return = 'data-'.$return;
if(strtolower($return) !== $return)
$return = Str::fromCamelCase('-',$return);
return $return;
}
// parseDataKeys
// parse un tableau de clés data
final public static function parseDataKeys(array $keys):array
{
$return = [];
foreach ($keys as $i => $key)
{
if(is_string($key))
$return[$i] = self::parseDataKey($key);
}
return $return;
}
// parseMerge
// merge un tableau parse sur un tableau existant
// seules les clés data peuvent accepter une valeur bool, pas les attributs normaux
final public static function parseMerge(array $array,array $return=[]):array
{
foreach ($array as $key => $value)
{
if(is_string($key) && !empty($key) && $value !== null)
{
if(($key === 'class' || $key === 'style') && array_key_exists($key,$return) && is_array($value) && is_array($return[$key]))
{
if($key === 'class')
$return[$key] = Arr::mergeUnique($return[$key],$value);
elseif($key === 'style')
$return[$key] = Arr::replace($return[$key],$value);
}
elseif($key === 'data' && is_array($value))
{
foreach ($value as $k => $v)
{
if(is_string($k))
{
if(in_array($k,self::$config['pattern'],true))
$v = Validate::pattern($v);
$return[$k] = $v;
}
}
}
elseif(in_array($key,self::$config['pattern'],true))
$return[$key] = Validate::pattern($value);
else
$return[$key] = $value;
}
}
return $return;
}
// append
// append plusieurs valeurs attributes et retourne un grand tableau parsed
final public static function append(...$values):array
{
$return = [];
foreach ($values as $value)
{
if(!empty($value))
{
$value = self::arr($value);
if(!empty($value))
$return = self::parseMerge($value,$return);
}
}
return self::arr($return);
}
// list
// les options de list peuvent être null
// explose et implose une valeur
// retourne un tableau unidimensionnel avec clé numérique
final public static function list($array,?array $option=null):array
{
$return = [];
$option = self::option($option);
$separator = self::getSeparator(1,$option['implode']);
$array = self::arr($array,$option);
$array = self::keyValue($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;
}
// 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(strpos($value,$separator) === false)
$return = [$value];
else
$return = self::explodeStr($value,$option);
return $return;
}
// prepareArr
// prépare un array dans la méthode arr
final public static function prepareArr(array $value,?array $option=null):array
{
return $value;
}
// explodeStr
// explode une string attr avec preg_match_all
// ne peut pas fonctionner si un attribut contient un single ou double quote à l'intérieur des quotes
final public static function explodeStr(string $value):array
{
$return = [];
$match = [];
$value = Str::doubleToSingleQuote($value);
preg_match_all("%(.*)=\'(.*)\'%Uis",$value,$match,PREG_SET_ORDER);
if(!empty($match))
{
foreach ($match as $key => $value)
{
if(is_array($value) && count($value) === 3)
{
$value = Arr::trim($value);
$k = Str::unquote($value[1],true,false);
$v = Str::unquote($value[2],true,false);
$return[$k] = $v;
}
}
}
return $return;
}
// explodeClass
// explose une string de classe
// sensible à la case
final public static function explodeClass(string $value,?array $return=null):array
{
$return ??= [];
if(strpos($value,' ') !== false)
{
$value = explode(' ',$value);
if(!empty($value))
{
foreach ($value as $v)
{
if(!is_numeric($v) && !empty($v) && Validate::isAlphanumericDash($v) && !in_array($v,$return,true))
$return[] = $v;
}
}
}
elseif(!is_numeric($value) && !empty($value) && Validate::isAlphanumericDash($value) && !in_array($value,$return,true))
$return[] = $value;
return $return;
}
// classImplode
// retourne une string class à partir d'un array
final public static function classImplode(array $value):?string
{
$value = Str::wordImplode($value);
return $value ?: null;
}
// prepareClass
// retourne une string class correctement formatté
final public static function prepareClass($value):?string
{
$value = self::parseClass($value);
return (is_array($value))? self::classImplode($value):null;
}
// keyValue
// ramène le tableau arr à un tableau unidimensionnel
// toutes les clés et valeurs du tableau sont des string
// les valeurs sont passés dans quote et entities ou specialchars par défaut
final public static function keyValue(array $array,array $option):array
{
$return = [];
$option['encode'] = (is_string($option['encode']) || $option['encode'] === true)? [$option['encode']]:$option['encode'];
if($option['caseImplode'] !== null)
$array = Arr::keysChangeCase($option['caseImplode'],$array);
foreach ($array as $key => $value)
{
if(is_string($key) && !empty($key))
{
if(is_scalar($value))
$value = Str::cast($value);
elseif(is_array($value) && !empty($value))
{
if($key === 'class')
$value = self::classImplode($value);
elseif($key === 'style')
$value = Style::implode($value,$option['style']);
else
$value = Json::encode($value,$option['json']);
}
elseif(is_object($value))
$value = Json::encode($value,$option['json']);
if(is_string($value))
{
if(!empty($option['encode']) && is_array($option['encode']))
$value = Html::encode($value,...$option['encode']);
if(is_bool($option['quote']))
$value = Str::quote($value,$option['quote']);
$return[$key] = $value;
}
}
}
return $return;
}
// getId
// retourne le id à partir d'une valeur attribut
final public static function getId($assoc,?array $option=null):?string
{
return self::get('id',$assoc,$option);
}
// setId
// change le id d'une valeur attribut
final public static function setId(string $value,$assoc,?array $option=null):?array
{
$value = self::parseId($value);
return (!empty($value))? self::set('id',$value,$assoc,$option):null;
}
// randomId
// génère un id random
final public static function randomId(?string $name=null,?array $option=null):string
{
return Str::randomPrefix((is_string($name))? $name:'',...array_values(Arr::plus(self::$config['randomId'],$option)));
}
// getClass
// retourne le tableau des classes uniques à partir d'une valeur attribut
final public static function getClass($assoc,?array $option=null):?array
{
return self::get('class',$assoc,$option);
}
// getOddEvenClass
// retourne le nom de la classe dépendamment si la valeur est numérique odd ou even
final public static function getOddEvenClass($value):?string
{
$return = null;
if(is_numeric($value))
{
if(Num::isOdd($value))
$return = self::getOddClass();
elseif(Num::isEven($value))
$return = self::getEvenClass();
}
return $return;
}
// getOddClass
// retourne le nom de la classe pour odd
final public static function getOddClass():?string
{
return (!empty(self::$config['oddEven'][0]))? self::$config['oddEven'][0]:null;
}
// getEvenClass
// retourne le nom de la classe pour even
final public static function getEvenClass():?string
{
return (!empty(self::$config['oddEven'][1]))? self::$config['oddEven'][1]:null;
}
// setClass
// change les classes d'une valeur attribut
final public static function setClass($value,$assoc,?array $option=null):?array
{
return self::set('class',self::parseClass($value),$assoc,$option);
}
// addClass
// ajoute une ou plusieurs classes à une valeur attribut
final public static function addClass($value,$assoc,?array $option=null):?array
{
return self::set('class',self::parseClass($value,self::getClass($assoc,$option)),$assoc,$option);
}
// removeClass
// enlève une ou plusieurs classes à une valeur attribut
final public static function removeClass($value,$assoc,?array $option=null):?array
{
$value = (array) self::parseClass($value);
$class = (array) self::getClass($assoc,$option);
return (!empty($class))? self::set('class',Arr::valuesStrip($value,$class,self::getSensitive()),$assoc,$option):null;
}
// toggleClass
// ajoute une ou plusieurs classes non existante dans la valeur attribut
// enlève une ou plusieurs classes existante dans la valeur attribut
final public static function toggleClass($value,$assoc,?array $option=null):?array
{
$return = false;
$value = (array) self::parseClass($value);
$class = (array) self::getClass($assoc,$option);
if(is_array($value))
{