1 | <?php |
||||
2 | /** |
||||
3 | * Recipe plugin for Craft CMS 3.x |
||||
4 | * |
||||
5 | * A comprehensive recipe FieldType for Craft CMS that includes metric/imperial |
||||
6 | * conversion, portion calculation, and JSON-LD microdata support |
||||
7 | * |
||||
8 | * @link https://nystudio107.com |
||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||
9 | * @copyright Copyright (c) 2017 nystudio107 |
||||
0 ignored issues
–
show
|
|||||
10 | */ |
||||
0 ignored issues
–
show
|
|||||
11 | |||||
12 | namespace nystudio107\recipe\helpers; |
||||
13 | |||||
14 | use Craft; |
||||
15 | |||||
16 | /** |
||||
0 ignored issues
–
show
|
|||||
17 | * @author nystudio107 |
||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||
18 | * @package Recipe |
||||
0 ignored issues
–
show
|
|||||
19 | * @since 1.0.0 |
||||
0 ignored issues
–
show
|
|||||
20 | */ |
||||
0 ignored issues
–
show
|
|||||
21 | class Json extends \craft\helpers\Json |
||||
22 | { |
||||
23 | // Static Properties |
||||
24 | // ========================================================================= |
||||
25 | |||||
26 | protected static int $recursionLevel; |
||||
27 | |||||
28 | // Static Methods |
||||
29 | // ========================================================================= |
||||
30 | |||||
31 | /** |
||||
0 ignored issues
–
show
|
|||||
32 | * @inheritdoc |
||||
33 | */ |
||||
0 ignored issues
–
show
|
|||||
34 | public static function encode( |
||||
35 | $value, |
||||
36 | $options = |
||||
0 ignored issues
–
show
|
|||||
37 | JSON_UNESCAPED_UNICODE |
||||
38 | | JSON_UNESCAPED_SLASHES, |
||||
39 | ): string { |
||||
40 | // If `devMode` is enabled, make the JSON-LD human-readable |
||||
41 | if (Craft::$app->getConfig()->getGeneral()->devMode) { |
||||
42 | $options |= JSON_PRETTY_PRINT; |
||||
43 | } |
||||
44 | |||||
45 | self::$recursionLevel = 0; |
||||
46 | |||||
47 | return parent::encode($value, $options); |
||||
48 | } |
||||
49 | |||||
50 | /** |
||||
0 ignored issues
–
show
|
|||||
51 | * @inheritdoc |
||||
52 | */ |
||||
0 ignored issues
–
show
|
|||||
53 | protected static function processData($data, &$expressions, $expPrefix) |
||||
54 | { |
||||
55 | ++self::$recursionLevel; |
||||
56 | $result = parent::processData($data, $expressions, $expPrefix); |
||||
57 | --self::$recursionLevel; |
||||
58 | static::normalizeJsonLdArray($result, self::$recursionLevel); |
||||
59 | |||||
60 | return $result; |
||||
61 | } |
||||
62 | |||||
63 | |||||
64 | // Private Methods |
||||
65 | // ========================================================================= |
||||
66 | |||||
67 | /** |
||||
68 | * Normalize the JSON-LD array recursively to remove empty values, change |
||||
69 | * 'type' to '@type' and have it be the first item in the array |
||||
70 | * |
||||
71 | * @param $array |
||||
0 ignored issues
–
show
|
|||||
72 | * @param $depth |
||||
0 ignored issues
–
show
|
|||||
73 | */ |
||||
0 ignored issues
–
show
|
|||||
74 | protected static function normalizeJsonLdArray(&$array, $depth): void |
||||
0 ignored issues
–
show
The parameter
$depth is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
75 | { |
||||
76 | $array = array_filter($array); |
||||
77 | $array = self::changeKey($array, 'context', '@context'); |
||||
78 | $array = self::changeKey($array, 'type', '@type'); |
||||
79 | ksort($array); |
||||
80 | } |
||||
81 | |||||
82 | /** |
||||
0 ignored issues
–
show
|
|||||
83 | * Replace key values without reordering the array or converting numeric |
||||
84 | * keys to associative keys (which unset() does) |
||||
85 | */ |
||||
0 ignored issues
–
show
|
|||||
86 | protected static function changeKey(array $array, string $oldKey, string $newKey): array |
||||
87 | { |
||||
88 | if (!array_key_exists($oldKey, $array)) { |
||||
89 | return $array; |
||||
90 | } |
||||
91 | |||||
92 | $keys = array_keys($array); |
||||
93 | $keys[array_search($oldKey, $keys)] = $newKey; |
||||
94 | |||||
95 | return array_combine($keys, $array); |
||||
96 | } |
||||
97 | } |
||||
98 |