1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace luya\web\jsonld; |
4
|
|
|
|
5
|
|
|
use yii\base\Arrayable; |
6
|
|
|
use yii\base\ArrayableTrait; |
7
|
|
|
use luya\helpers\StringHelper; |
8
|
|
|
use yii\base\BaseObject; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base Thing. |
12
|
|
|
* |
13
|
|
|
* Every JsonLD object must implement BaseThing. Therfore BaseThing auto resolves the fields, in |
14
|
|
|
* order to provide the Arrayable::fields() methods. |
15
|
|
|
* |
16
|
|
|
* @author Basil Suter <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
abstract class BaseThing extends BaseObject implements Arrayable, ThingInterface |
19
|
|
|
{ |
20
|
|
|
use ThingTrait; |
21
|
|
|
|
22
|
|
|
use ArrayableTrait { toArray as protected internalToArray; } |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Contains the jsonLd definton @type value if not null or false. |
26
|
|
|
* |
27
|
|
|
* @return boolean|string Generates the @type field. |
28
|
|
|
*/ |
29
|
|
|
public function typeDefintion() |
30
|
|
|
{ |
31
|
|
|
return false; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Find all getter methods. |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
public function resolveGetterMethods() |
40
|
|
|
{ |
41
|
|
|
$resolved = []; |
42
|
|
|
$methods = get_class_methods($this); |
43
|
|
|
|
44
|
|
|
if (!$methods) { |
|
|
|
|
45
|
|
|
return []; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
foreach ($methods as $method) { |
49
|
|
|
if (StringHelper::startsWith($method, 'get', true)) { |
50
|
|
|
$resolved[] = lcfirst(StringHelper::replaceFirst('get', '', $method)); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
asort($resolved); |
55
|
|
|
|
56
|
|
|
return $resolved; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
|
|
public function toArray(array $fields = [], array $expand = [], $recursive = true) |
63
|
|
|
{ |
64
|
|
|
$array = $this->removeEmptyValues($this->internalToArray($fields, $expand, $recursive)); |
65
|
|
|
|
66
|
|
|
if ($this->typeDefintion()) { |
67
|
|
|
$array['@type'] = $this->typeDefintion(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $array; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritdoc |
75
|
|
|
*/ |
76
|
|
|
public function fields() |
77
|
|
|
{ |
78
|
|
|
return $this->resolveGetterMethods(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Cleanup array from null values. |
83
|
|
|
* |
84
|
|
|
* @param array $haystack |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
private function removeEmptyValues(array $haystack) |
88
|
|
|
{ |
89
|
|
|
foreach ($haystack as $key => $value) { |
90
|
|
|
if (is_array($value)) { |
91
|
|
|
$haystack[$key] = $this->removeEmptyValues($value); |
92
|
|
|
} |
93
|
|
|
// remove empty values as there is no property which allows empty values. |
94
|
|
|
if (empty($value)) { |
95
|
|
|
unset($haystack[$key]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $haystack; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.