-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathProperties.php
More file actions
94 lines (81 loc) · 2.57 KB
/
Properties.php
File metadata and controls
94 lines (81 loc) · 2.57 KB
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
<?php
namespace modmore\VersionX\Fields;
class Properties extends Field
{
public function parse()
{
if (is_array($this->value)) {
$this->value = self::splitPropertyValues($this->value, $this->fieldName);
}
}
/**
*
* @param array $arrayField
* @param string $name
* @param array $fields
* @return array
*/
public static function splitPropertyValues(array $arrayField, string $name = '', array &$fields = []): array
{
$arrays = [];
foreach ($arrayField as $field => $value) {
if (empty($name)) {
$fields[$field] = $value;
continue;
}
if (is_numeric($field)) {
$fields[$name][$field] = $value;
continue;
}
if (!is_array($value)) {
$fields["{$name}.{$field}"] = $value;
continue;
}
$arrays[$field] = $value;
}
foreach ($arrays as $field => $value) {
self::splitPropertyValues($value, "{$name}.{$field}", $fields);
}
return $fields;
}
/**
* @param \vxDeltaField $field
* @param mixed $data
* @return mixed
*/
public static function revertPropertyValue(\vxDeltaField $field, &$data)
{
if (!is_array($data)) {
return $data;
}
$pieces = explode('.', $field->get('field'));
$last = end($pieces);
foreach ($pieces as $piece) {
if (!array_key_exists($piece, $data)) {
continue;
}
// The last 'piece' will be the key we're after.
if ($piece === $last) {
$beforeValue = $field->get('before');
$beforeType = $field->get('before_type');
if (in_array($beforeType, ['array', 'object'])) {
// Decode if it's meant to be an array/object
$decoded = json_decode($beforeValue, true);
$data[$piece] = $decoded ?: $field->get('before');
}
else if (in_array(strtolower($beforeType), self::ACCEPTED_VALUE_TYPES)) {
// Cast as the set type
$data[$piece] = settype($beforeValue, $beforeType);
}
else {
// If we're here treat as a string
$data[$piece] = $field->get('before');
}
}
else {
$data = &$data[$piece];
}
}
return $data;
}
}