1
|
|
|
<?php |
2
|
|
|
// Copyright 1999-2021. Plesk International GmbH. |
3
|
|
|
|
4
|
|
|
namespace PleskX\Api; |
5
|
|
|
|
6
|
|
|
class Operator |
7
|
|
|
{ |
8
|
|
|
/** @var string|null */ |
9
|
|
|
protected $_wrapperTag = null; |
10
|
|
|
|
11
|
|
|
/** @var \PleskX\Api\Client */ |
12
|
|
|
protected $_client; |
13
|
|
|
|
14
|
|
|
public function __construct($client) |
15
|
|
|
{ |
16
|
|
|
$this->_client = $client; |
17
|
|
|
|
18
|
|
|
if (is_null($this->_wrapperTag)) { |
19
|
|
|
$classNameParts = explode('\\', get_class($this)); |
20
|
|
|
$this->_wrapperTag = end($classNameParts); |
21
|
|
|
$this->_wrapperTag = strtolower(preg_replace('/([a-z])([A-Z])/', '\1-\2', $this->_wrapperTag)); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Perform plain API request. |
27
|
|
|
* |
28
|
|
|
* @param string|array $request |
29
|
|
|
* @param int $mode |
30
|
|
|
* |
31
|
|
|
* @return XmlResponse |
32
|
|
|
*/ |
33
|
|
|
public function request($request, $mode = Client::RESPONSE_SHORT) |
34
|
|
|
{ |
35
|
|
|
$wrapperTag = $this->_wrapperTag; |
36
|
|
|
|
37
|
|
|
if (is_array($request)) { |
38
|
|
|
$request = [$wrapperTag => $request]; |
39
|
|
|
} elseif (preg_match('/^[a-z]/', $request)) { |
40
|
|
|
$request = "$wrapperTag.$request"; |
41
|
|
|
} else { |
42
|
|
|
$request = "<$wrapperTag>$request</$wrapperTag>"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->_client->request($request, $mode); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string $field |
50
|
|
|
* @param int|string $value |
51
|
|
|
* @param string $deleteMethodName |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
protected function _delete($field, $value, $deleteMethodName = 'del') |
56
|
|
|
{ |
57
|
|
|
$response = $this->request([ |
58
|
|
|
$deleteMethodName => [ |
59
|
|
|
'filter' => [ |
60
|
|
|
$field => $value, |
61
|
|
|
], |
62
|
|
|
], |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
return 'ok' === (string) $response->status; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $structClass |
70
|
|
|
* @param string $infoTag |
71
|
|
|
* @param string|null $field |
72
|
|
|
* @param int|string|null $value |
73
|
|
|
* @param callable|null $filter |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
protected function _getItems($structClass, $infoTag, $field = null, $value = null, callable $filter = null) |
78
|
|
|
{ |
79
|
|
|
$packet = $this->_client->getPacket(); |
80
|
|
|
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get'); |
81
|
|
|
|
82
|
|
|
$filterTag = $getTag->addChild('filter'); |
83
|
|
|
if (!is_null($field)) { |
84
|
|
|
$filterTag->addChild($field, $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$getTag->addChild('dataset')->addChild($infoTag); |
88
|
|
|
|
89
|
|
|
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL); |
90
|
|
|
|
91
|
|
|
$items = []; |
92
|
|
|
foreach ($response->xpath('//result') as $xmlResult) { |
93
|
|
|
if (!is_null($filter) && !$filter($xmlResult->data->$infoTag)) { |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
if (!isset($xmlResult->data) || !isset($xmlResult->data->$infoTag)) { |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
$items[] = new $structClass($xmlResult->data->$infoTag); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $items; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|