@@ -12,21 +12,14 @@ Schematic data mapper is a tool for converting nested data structures
12
12
(any compositions of associative arrays, non-associative arrays and objects)
13
13
according to the given conversion schema.
14
14
15
- ### How to install to your project
15
+ ## How to install to your project
16
16
```
17
17
composer require smoren/schemator
18
18
```
19
19
20
- ### Unit testing
21
- ```
22
- composer install
23
- composer test-init
24
- composer test
25
- ```
20
+ ## Usage
26
21
27
- ### Usage
28
-
29
- #### Simple usage
22
+ ### Simple usage
30
23
31
24
``` php
32
25
use Smoren\Schemator\Factories\SchematorFactory;
@@ -120,7 +113,7 @@ print_r($output);
120
113
*/
121
114
```
122
115
123
- #### Setting errors level
116
+ ### Setting errors level
124
117
125
118
``` php
126
119
use Smoren\Schemator\Factories\SchematorFactory;
@@ -149,7 +142,7 @@ try {
149
142
150
143
```
151
144
152
- #### Using base filters
145
+ ### Using base filters
153
146
154
147
``` php
155
148
use Smoren\Schemator\Factories\SchematorFactory;
@@ -233,7 +226,7 @@ Array
233
226
*/
234
227
```
235
228
236
- #### Using smart filter and replace
229
+ ### Using smart filter and replace
237
230
238
231
``` php
239
232
use Smoren\Schemator\Factories\SchematorFactory;
@@ -331,7 +324,7 @@ Array
331
324
*/
332
325
```
333
326
334
- #### Using custom filters
327
+ ### Using custom filters
335
328
336
329
```php
337
330
use Smoren\Schemator\Factories\SchematorFactory;
@@ -366,7 +359,7 @@ Array
366
359
*/
367
360
```
368
361
369
- #### Mass usage
362
+ ### Mass usage
370
363
371
364
```php
372
365
use Smoren\Schemator\Factories\SchematorFactory;
@@ -460,3 +453,322 @@ Array
460
453
)
461
454
*/
462
455
```
456
+
457
+ ## Filters
458
+
459
+ ### const
460
+ Sets the value from const param.
461
+
462
+ Schema:
463
+ ```php
464
+ ["value" => [["const", "My const value"]]]
465
+ ```
466
+
467
+ Result:
468
+ ```php
469
+ ["value" => "My const value"]
470
+ ```
471
+
472
+ ### sum
473
+ Returns the sum of given array.
474
+
475
+ Given:
476
+ ```php
477
+ ["numbers" => [1, 2, 3, 4, 5]]
478
+ ```
479
+
480
+ Schema:
481
+ ```php
482
+ ["value" => ["numbers", ["sum"]]]
483
+ ```
484
+
485
+ Result:
486
+ ```php
487
+ ["value" => 15]
488
+ ```
489
+
490
+ ### average
491
+ Returns the average of given array.
492
+
493
+ Given:
494
+ ```php
495
+ ["numbers" => [1, 2, 3, 4, 5]]
496
+ ```
497
+
498
+ Schema:
499
+ ```php
500
+ ["value" => ["numbers", ["average"]]]
501
+ ```
502
+
503
+ Result:
504
+ ```php
505
+ ["value" => 3]
506
+ ```
507
+
508
+ ### date
509
+ Returns formatted date from the Unix timestamp given value.
510
+
511
+ Params:
512
+ 1. Date format
513
+ * required
514
+ * data type — string
515
+ * example: `d.m.Y H:i:s`
516
+ * [more about formats](https://www.php.net/manual/ru/datetime.format.php)
517
+ 2. Time zone offset from GMT
518
+ * optional
519
+ * data type — integer
520
+ * default — 0
521
+
522
+ Given:
523
+ ```php
524
+ ["some_date" => 1651481881]
525
+ ```
526
+
527
+ Schema:
528
+ ```php
529
+ ["value" => ["some_date", ["date", "d.m.Y H:i:s", 3]]]
530
+ ```
531
+
532
+ Result:
533
+ ```php
534
+ ["value" => "02.05.2022 11:58:01"]
535
+ ```
536
+
537
+ ### implode
538
+ Returns string of imploded items of given array with separator from args list.
539
+
540
+ params:
541
+ 1. Separator
542
+ * required
543
+ * data type — string
544
+ * example: `; `
545
+
546
+ Given:
547
+ ```php
548
+ ["numbers" => [1, 2, 3, 4, 5]]
549
+ ```
550
+
551
+ Schema:
552
+ ```php
553
+ ["value" => ["numbers", ["implode", "; "]]]
554
+ ```
555
+
556
+ Result:
557
+ ```php
558
+ ["value" => "1; 2; 3; 4; 5"]
559
+ ```
560
+
561
+ ### explode
562
+ Returns array of exploded strings from given string with separator from args list
563
+
564
+ params:
565
+ 1. Separator
566
+ * required
567
+ * data type — string
568
+ * example: `; `
569
+
570
+ Given:
571
+ ```php
572
+ ["numbers" => "1; 2; 3; 4; 5"]
573
+ ```
574
+
575
+ Schema:
576
+ ```php
577
+ ["value" => ["numbers", ["explode", "; "]]]
578
+ ```
579
+
580
+ Result:
581
+ ```php
582
+ ["value" => ["1", "2", "3", "4", "5"]]
583
+ ```
584
+
585
+ ### flatten
586
+ Returns flat array contains all the dead end leaves of tree array.
587
+
588
+ Given:
589
+ ```php
590
+ [
591
+ "numbers" => [
592
+ [
593
+ [1, 2, 3],
594
+ [4, 5, 6]
595
+ ],
596
+ [7, 8, 9]
597
+ ],
598
+ ]
599
+ ```
600
+
601
+ Schema:
602
+ ```php
603
+ ["value" => ["numbers", ["flatten"]]]
604
+ ```
605
+
606
+ Result:
607
+ ```php
608
+ ["value" => [1, 2, 3, 4, 5, 6, 7, 8, 9]]
609
+ ```
610
+
611
+ ### sort
612
+ Sorts and returns given array.
613
+
614
+ Given:
615
+ ```php
616
+ ["numbers" => [3, 5, 4, 1, 2]]
617
+ ```
618
+
619
+ Schema:
620
+ ```php
621
+ ["value" => ["numbers", ["sort"]]]
622
+ ```
623
+
624
+ Result:
625
+ ```php
626
+ ["value" => [1, 2, 3, 4, 5]]
627
+ ```
628
+
629
+ ### rsort
630
+ Sorts reversely and returns given array.
631
+
632
+ Given:
633
+ ```php
634
+ ["numbers" => [3, 5, 4, 1, 2]]
635
+ ```
636
+
637
+ Schema:
638
+ ```php
639
+ ["value" => ["numbers", ["sort"]]]
640
+ ```
641
+
642
+ Result:
643
+ ```php
644
+ ["value" => [5, 4, 3, 2, 1]]
645
+ ```
646
+
647
+ ### filter
648
+ Returns array contains elements from given array, that match the predicates from params list.
649
+
650
+ Rules:
651
+ * Every predicate has such format `["predicate name", ...parans]`.
652
+ * Predicates in one filter apply according the "OR" logic.
653
+ * To apply "AND" logic use [chain of filters](#Chain-of-filters).
654
+ * Available predicates:
655
+ * `["=", 10]` means `value = 10`
656
+ * `[">", 10]` means `value > 10`
657
+ * `[">=", 10]` means `value >= 10`
658
+ * `["<", 10]` means `value < 10`
659
+ * `["<=", 10]` means `value <= 10`
660
+ * `["in", [1, 2]]` means `value = 1 OR value = 2`
661
+ * `["not in", [1, 2]]` means `value != 1 AND value != 2`
662
+ * `["between", 1, 5]` means `1 <= value <= 5`
663
+ * `["between strict", 1, 5]` means `1 < value < 5`
664
+
665
+ Given:
666
+ ```php
667
+ ["numbers" => [-5, -3, -1, 1, 3, 5]]
668
+ ```
669
+
670
+ Schema:
671
+ ```php
672
+ [
673
+ "value" => [
674
+ "numbers",
675
+ [
676
+ "filter",
677
+ [[">", 1], ["<", -1]] // value > 1 OR value < -1
678
+ ],
679
+ ],
680
+ ]
681
+ ```
682
+
683
+ Result:
684
+ ```php
685
+ ["value" => [-5, -3, 3, 5]]
686
+ ```
687
+
688
+ ### replace
689
+ Returns array of elements from given array with replaces by rules from params list.
690
+
691
+ Rules:
692
+ * Every rule has such format `["value to replace", "rule name", ...params]`.
693
+ * Rules in one filter apply according the "OR" logic.
694
+ * To apply "AND" logic use [chain of filters](#Chain-of-filters).
695
+ * Available rules:
696
+ * `["=", 10]` means `value = 10`
697
+ * `[">", 10]` means `value > 10`
698
+ * `[">=", 10]` means `value >= 10`
699
+ * `["<", 10]` means `value < 10`
700
+ * `["<=", 10]` means `value <= 10`
701
+ * `["in", [1, 2]]` means `value = 1 или value = 2`
702
+ * `["not in", [1, 2]]` means `value != 1 и value != 2`
703
+ * `["between", 1, 5]` means `1 <= value <= 5`
704
+ * `["between strict", 1, 5]` means `1 < value < 5`
705
+ * `["else"]` — no rules matched for value
706
+ _(If rule `else` did not use, by default such values are replaced with `null`)_
707
+
708
+ Given:
709
+ ```php
710
+ ["numbers" => [-5, -3, -1, 1, 3, 5]]
711
+ ```
712
+
713
+ Schema:
714
+ ```php
715
+ [
716
+ "value" => [
717
+ "numbers",
718
+ [
719
+ "replace",
720
+ [
721
+ ["positive", ">", 0],
722
+ ["negative", "<", 0],
723
+ ["zero", "else"]
724
+ ],
725
+ ],
726
+ ],
727
+ ]
728
+ ```
729
+
730
+ Result:
731
+ ```php
732
+ ["value" => ["negative", "negative", "negative", "zero", "positive", "positive", "positive"]]
733
+ ```
734
+
735
+ ### Chain of filters
736
+
737
+ Given:
738
+ ```php
739
+ ["numbers" => [-5, -3, -1, 1, 3, 5]]
740
+ ```
741
+
742
+ Schema:
743
+ ```php
744
+ [
745
+ "value" => [
746
+ "numbers",
747
+ [
748
+ "filter",
749
+ [[">", 1], ["<", -1]] // (value > 1 OR value < -1)
750
+ ],
751
+ // AND
752
+ [
753
+ "filter",
754
+ [[">=", -3]] // value >= -3
755
+ ],
756
+ ],
757
+ ]
758
+ ```
759
+
760
+ Result:
761
+ ```php
762
+ ["value" => [-3, 3, 5]]
763
+ ```
764
+
765
+ ## Unit testing
766
+ ```
767
+ composer install
768
+ composer test-init
769
+ composer test
770
+ ```
771
+
772
+ ## License
773
+
774
+ Schemator Data Mapper is licensed under the MIT License.
0 commit comments