Skip to content

Commit a548ab9

Browse files
Merge pull request plesk#15 from vbaranovskiy-plesk/master
Add get and getAll operations for subdomains
2 parents 1b9a669 + a624dcc commit a548ab9

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

src/PleskX/Api/Operator/Subdomain.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,42 @@ public function delete($field, $value)
3232
{
3333
return $this->_delete($field, $value);
3434
}
35+
36+
/**
37+
* @param string $field
38+
* @param integer|string $value
39+
* @return Struct\Info
40+
*/
41+
public function get($field, $value)
42+
{
43+
$items = $this->getAll($field, $value);
44+
return reset($items);
45+
}
46+
47+
/**
48+
* @param string $field
49+
* @param integer|string $value
50+
* @return Struct\Info[]
51+
*/
52+
public function getAll($field = null, $value = null)
53+
{
54+
$packet = $this->_client->getPacket();
55+
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
56+
57+
$filterTag = $getTag->addChild('filter');
58+
if (!is_null($field)) {
59+
$filterTag->addChild($field, $value);
60+
}
61+
62+
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
63+
64+
$items = [];
65+
foreach ($response->xpath('//result') as $xmlResult) {
66+
$item = new Struct\Info($xmlResult->data);
67+
$item->id = (int)$xmlResult->id;
68+
$items[] = $item;
69+
}
70+
71+
return $items;
72+
}
3573
}

src/PleskX/Api/Struct/Subdomain/Info.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,25 @@ class Info extends \PleskX\Api\Struct
88
/** @var integer */
99
public $id;
1010

11+
/** @var string */
12+
public $parent;
13+
14+
/** @var string */
15+
public $name;
16+
17+
/** @var array */
18+
public $properties;
19+
1120
public function __construct($apiResponse)
1221
{
22+
$this->properties = [];
1323
$this->_initScalarProperties($apiResponse, [
1424
'id',
25+
'parent',
26+
'name'
1527
]);
28+
foreach ($apiResponse->property as $propertyInfo) {
29+
$this->properties[(string)$propertyInfo->name] = (string)$propertyInfo->value;
30+
}
1631
}
1732
}

tests/SubdomainTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,34 @@ public function testDelete()
5050
$result = static::$_client->subdomain()->delete('id', $subdomain->id);
5151
$this->assertTrue($result);
5252
}
53+
54+
public function testGet()
55+
{
56+
$subdomain = $this->_createSubdomain('sub');
57+
58+
$subdomainInfo = static::$_client->subdomain()->get('id', $subdomain->id);
59+
$name = explode('.', $subdomainInfo->name);
60+
$parent = explode('.', $subdomainInfo->parent);
61+
$this->assertEquals('sub', reset(array_diff($name, $parent)));
62+
63+
static::$_client->subdomain()->delete('id', $subdomain->id);
64+
}
65+
66+
public function testGetAll()
67+
{
68+
$subdomain = $this->_createSubdomain('sub');
69+
$subdomain2 = $this->_createSubdomain('sub2');
70+
71+
$subdomainInfo = static::$_client->subdomain()->getAll();
72+
$this->assertCount(2, $subdomainInfo);
73+
$name = explode('.', $subdomainInfo[0]->name);
74+
$parent = explode('.', $subdomainInfo[0]->parent);
75+
$name2 = explode('.', $subdomainInfo[1]->name);
76+
$parent2 = explode('.', $subdomainInfo[1]->parent);
77+
$this->assertEquals('sub', reset(array_diff($name, $parent)));
78+
$this->assertEquals('sub2', reset(array_diff($name2, $parent2)));
79+
80+
static::$_client->subdomain()->delete('id', $subdomain->id);
81+
static::$_client->subdomain()->delete('id', $subdomain2->id);
82+
}
5383
}

0 commit comments

Comments
 (0)