|
1
|
|
|
<?php |
|
2
|
|
|
// Copyright 1999-2021. Plesk International GmbH. |
|
3
|
|
|
|
|
4
|
|
|
namespace PleskX\Api; |
|
5
|
|
|
|
|
6
|
|
|
abstract class Struct |
|
7
|
|
|
{ |
|
8
|
|
|
public function __set($property, $value) |
|
9
|
|
|
{ |
|
10
|
|
|
throw new \Exception("Try to set an undeclared property '$property'."); |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Initialize list of scalar properties by response. |
|
15
|
|
|
* |
|
16
|
|
|
* @param \SimpleXMLElement $apiResponse |
|
17
|
|
|
* @param array $properties |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \Exception |
|
20
|
|
|
*/ |
|
21
|
|
|
protected function _initScalarProperties($apiResponse, array $properties) |
|
22
|
|
|
{ |
|
23
|
|
|
foreach ($properties as $property) { |
|
24
|
|
|
if (is_array($property)) { |
|
25
|
|
|
$classPropertyName = current($property); |
|
26
|
|
|
$value = $apiResponse->{key($property)}; |
|
27
|
|
|
} else { |
|
28
|
|
|
$classPropertyName = $this->_underToCamel(str_replace('-', '_', $property)); |
|
29
|
|
|
$value = $apiResponse->$property; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$reflectionProperty = new \ReflectionProperty($this, $classPropertyName); |
|
33
|
|
|
$docBlock = $reflectionProperty->getDocComment(); |
|
34
|
|
|
$propertyType = preg_replace('/^.+ @var ([a-z]+) .+$/', '\1', $docBlock); |
|
35
|
|
|
|
|
36
|
|
|
if ('string' == $propertyType) { |
|
37
|
|
|
$value = (string) $value; |
|
38
|
|
|
} elseif ('int' == $propertyType) { |
|
39
|
|
|
$value = (int) $value; |
|
40
|
|
|
} elseif ('bool' == $propertyType) { |
|
41
|
|
|
$value = in_array((string) $value, ['true', 'on', 'enabled']); |
|
42
|
|
|
} else { |
|
43
|
|
|
throw new \Exception("Unknown property type '$propertyType'."); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->$classPropertyName = $value; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Convert underscore separated words into camel case. |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $under |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
private function _underToCamel($under) |
|
58
|
|
|
{ |
|
59
|
|
|
$under = '_'.str_replace('_', ' ', strtolower($under)); |
|
60
|
|
|
|
|
61
|
|
|
return ltrim(str_replace(' ', '', ucwords($under)), '_'); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|