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 |
|
|
|
|
9
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\recipe\helpers; |
13
|
|
|
|
14
|
|
|
use Craft; |
15
|
|
|
|
16
|
|
|
/** |
|
|
|
|
17
|
|
|
* @author nystudio107 |
|
|
|
|
18
|
|
|
* @package Recipe |
|
|
|
|
19
|
|
|
* @since 1.0.0 |
|
|
|
|
20
|
|
|
*/ |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
|
|
|
|
34
|
|
|
public static function encode( |
35
|
|
|
$value, |
36
|
|
|
$options = |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
72
|
|
|
* @param $depth |
|
|
|
|
73
|
|
|
*/ |
|
|
|
|
74
|
|
|
protected static function normalizeJsonLdArray(&$array, $depth): void |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
83
|
|
|
* Replace key values without reordering the array or converting numeric |
84
|
|
|
* keys to associative keys (which unset() does) |
85
|
|
|
*/ |
|
|
|
|
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
|
|
|
|