ArrayFilter::json()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Platine Template
5
 *
6
 * Platine Template is a template engine that has taken a lot of inspiration from Django.
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Template
11
 * Copyright (c) 2014 Guz Alexander, http://guzalexander.com
12
 * Copyright (c) 2011, 2012 Harald Hanek, http://www.delacap.com
13
 * Copyright (c) 2006 Mateo Murphy
14
 *
15
 * Permission is hereby granted, free of charge, to any person obtaining a copy
16
 * of this software and associated documentation files (the "Software"), to deal
17
 * in the Software without restriction, including without limitation the rights
18
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
 * copies of the Software, and to permit persons to whom the Software is
20
 * furnished to do so, subject to the following conditions:
21
 *
22
 * The above copyright notice and this permission notice shall be included in all
23
 * copies or substantial portions of the Software.
24
 *
25
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
 * SOFTWARE.
32
 */
33
34
/**
35
 *  @file ArrayFilter.php
36
 *
37
 *  The Array Filter class
38
 *
39
 *  @package    Platine\Template\Filter
40
 *  @author Platine Developers Team
41
 *  @copyright  Copyright (c) 2020
42
 *  @license    http://opensource.org/licenses/MIT  MIT License
43
 *  @link   https://www.platine-php.com
44
 *  @version 1.0.0
45
 *  @filesource
46
 */
47
48
declare(strict_types=1);
49
50
namespace Platine\Template\Filter;
51
52
use Iterator;
53
use Platine\Stdlib\Helper\Json;
54
use Platine\Template\Parser\AbstractFilter;
55
use Traversable;
56
57
/**
58
 * @class ArrayFilter
59
 * @package Platine\Template\Filter
60
 */
61
class ArrayFilter extends AbstractFilter
62
{
63
    /**
64
     * Returns the first element of an array
65
     * @param array<mixed>|Iterator<mixed>|mixed $value
66
     * @return mixed
67
     */
68
    public static function first(mixed $value): mixed
69
    {
70
        if (is_array($value)) {
71
            return reset($value);
72
        }
73
74
        if ($value instanceof Iterator) {
75
            $value->rewind();
76
77
            return $value->current();
78
        }
79
80
        return $value;
81
    }
82
83
    /**
84
     * Returns the last element of an array
85
     * @param array<mixed>|Traversable<mixed>|mixed $value
86
     * @return mixed
87
     */
88
    public static function last(mixed $value): mixed
89
    {
90
        if (is_array($value)) {
91
            return end($value);
92
        }
93
94
        if ($value instanceof Traversable) {
95
            $last = null;
96
            foreach ($value as $elem) {
97
                $last = $elem;
98
            }
99
100
            return $last;
101
        }
102
103
        return $value;
104
    }
105
106
    /**
107
     * Sort an array.
108
     * @param array<int|string, mixed>|mixed $variable
109
     * @param int|string|null $property
110
     * @return mixed
111
     */
112
    public static function sort(mixed $variable, int|string|null $property = null): mixed
113
    {
114
        if ($variable instanceof Traversable) {
115
            $variable = iterator_to_array($variable);
116
        }
117
118
        if ($property === null) {
119
            asort($variable);
120
        } else {
121
            $first = reset($variable);
122
            if ($first !== false && is_array($first) && array_key_exists($property, $first)) {
123
                uasort($variable, function ($a, $b) use ($property) {
124
                    return $a[$property] <=> $b[$property];
125
                });
126
            }
127
        }
128
129
        return $variable;
130
    }
131
132
    /**
133
     * Sort an array by key.
134
     * @param array<int|string, mixed>|mixed $variable
135
     * @return mixed
136
     */
137
    public static function sortKey(mixed $variable): mixed
138
    {
139
        if (is_array($variable)) {
140
            ksort($variable);
141
            return $variable;
142
        }
143
144
        return $variable;
145
    }
146
147
    /**
148
     * Remove duplicate elements from an array
149
     * @param array<int|string, mixed> $variable
150
     * @return array<mixed>
151
     */
152
    public static function unique(iterable $variable): array
153
    {
154
        if ($variable instanceof Traversable) {
155
            $variable = iterator_to_array($variable);
156
        }
157
158
        return array_unique($variable);
0 ignored issues
show
Bug introduced by
It seems like $variable can also be of type iterable; however, parameter $array of array_unique() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
        return array_unique(/** @scrutinizer ignore-type */ $variable);
Loading history...
159
    }
160
161
    /**
162
     * Map/collect on a given property
163
     * @param array<mixed>|Traversable|mixed $variable
164
     * @param int|string $property
165
     * @return mixed
166
     */
167
    public static function map(mixed $variable, int|string $property): mixed
168
    {
169
        if ($variable instanceof Traversable) {
170
            $variable = iterator_to_array($variable);
171
        }
172
173
        if (!is_array($variable)) {
174
            return $variable;
175
        }
176
177
        return array_map(
178
            function ($element) use ($property) {
179
                if (is_callable($element)) {
180
                    return $element();
181
                } elseif (is_array($element) && array_key_exists($property, $element)) {
182
                    return $element[$property];
183
                }
184
185
                return null;
186
            },
187
            $variable
188
        );
189
    }
190
191
    /**
192
     * Reverse the elements of an array
193
     * @param array<mixed>|Traversable|mixed $variable
194
     * @return mixed
195
     */
196
    public static function reverse(mixed $variable): mixed
197
    {
198
        if ($variable instanceof Traversable) {
199
            $variable = iterator_to_array($variable);
200
        }
201
202
        if (!is_array($variable)) {
203
            return $variable;
204
        }
205
206
        return array_reverse($variable);
207
    }
208
209
    /**
210
     * Return the JSON representation
211
     * @param mixed $variable
212
     * @param mixed $pretty whether use pretty print
213
     * @return string
214
     */
215
    public static function json(mixed $variable, mixed $pretty = false): string
216
    {
217
        $prettyPrint = boolval($pretty);
218
        $options = 0;
219
        if ($prettyPrint) {
220
            $options = JSON_PRETTY_PRINT;
221
        }
222
223
        return Json::encode($variable, $options);
224
    }
225
}
226