1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Service for converting Addon to a nested array suitable for json-encoding in an API |
5
|
|
|
*/ |
6
|
|
|
class AddonToArray |
7
|
|
|
{ |
8
|
|
|
|
9
|
|
|
public function convert(Addon $package) |
10
|
|
|
{ |
11
|
|
|
$data = $package->toMap(); |
12
|
|
|
unset($data['LastEdited']); |
13
|
|
|
unset($data['Created']); |
14
|
|
|
unset($data['ClassName']); |
15
|
|
|
unset($data['RecordClassName']); |
16
|
|
|
|
17
|
|
|
unset($data['VendorID']); |
18
|
|
|
|
19
|
|
|
// Ensure consistent typing |
20
|
|
|
$data['Rating'] = (int)$data['Rating']; |
21
|
|
|
|
22
|
|
|
if ($package->RatingDetails) { |
|
|
|
|
23
|
|
|
$data['RatingDetails'] = []; |
24
|
|
|
foreach (json_decode($package->RatingDetails, true) as $k => $v) { |
|
|
|
|
25
|
|
|
$data['RatingDetails'][] = [ |
26
|
|
|
'Name' => $k, |
27
|
|
|
'Value' => $v, |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$data['Versions'] = []; |
33
|
|
|
foreach ($package->Versions() as $version) { |
|
|
|
|
34
|
|
|
$versionData = $version->toMap(); |
35
|
|
|
unset($versionData['LastEdited']); |
36
|
|
|
unset($versionData['Created']); |
37
|
|
|
unset($versionData['ClassName']); |
38
|
|
|
unset($versionData['RecordClassName']); |
39
|
|
|
unset($versionData['AddonID']); |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
unset($versionData['Name']); |
43
|
|
|
unset($versionData['Type']); |
44
|
|
|
unset($versionData['Homepage']); |
45
|
|
|
|
46
|
|
|
if (!empty($versionData['ExtraValue'])) { |
47
|
|
|
$versionData['ExtraValue'] = unserialize($versionData['ExtraValue']); |
48
|
|
|
} |
49
|
|
|
if (!empty($versionData['LicenseValue'])) { |
50
|
|
|
$versionData['LicenseValue'] = unserialize($versionData['LicenseValue']); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
foreach ($version->CompatibleVersions() as $compatible) { |
54
|
|
|
$versionData['CompatibleVersions'][] = [ |
55
|
|
|
"Version" => $compatible->Name, |
56
|
|
|
"Major" => $compatible->Major, |
57
|
|
|
"Minor" => $compatible->Minor, |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($version->Authors() as $author) { |
62
|
|
|
$authorData = $author->toMap(); |
63
|
|
|
unset($authorData['ID']); |
64
|
|
|
unset($authorData['ClassName']); |
65
|
|
|
unset($authorData['RecordClassName']); |
66
|
|
|
unset($authorData['LastEdited']); |
67
|
|
|
unset($authorData['Created']); |
68
|
|
|
$versionData['Authors'][] = $authorData; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$data['Versions'][] = $versionData; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $data; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.