forked from cloudflare/cloudflare-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkersKV.php
More file actions
97 lines (80 loc) · 3.1 KB
/
WorkersKV.php
File metadata and controls
97 lines (80 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* User: junade
* Date: 01/02/2017
* Time: 12:30
*/
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
use Cloudflare\API\Traits\BodyAccessorTrait;
class WorkersKV implements API
{
use BodyAccessorTrait;
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
public function createNamespace(string $accountID, string $namespace): \stdClass
{
$response = $this->adapter->post(
'accounts/' . $accountID . '/storage/kv/namespaces',
['title' => $namespace]
);
$this->body = json_decode($response->getBody());
return $this->body->result;
}
public function getNameSpaces(string $accountID): array
{
$response = $this->adapter->get('accounts/' . $accountID . '/storage/kv/namespaces');
$this->body = json_decode($response->getBody());
return $this->body->result;
}
public function listNamespaceKeys(string $accountID, $namespaceIdentifier): array
{
$response = $this->adapter->get('accounts/' . $accountID . '/storage/kv/namespaces/' . $namespaceIdentifier . '/keys');
$this->body = json_decode($response->getBody());
return $this->body->result;
}
public function getReadKeyValuePair(string $accountID, $namespaceIdentifier, $keyName): string
{
$response = $this->adapter->get('accounts/' . $accountID . '/storage/kv/namespaces/' . $namespaceIdentifier . '/values/' . $keyName);
$this->body = (string) $response->getBody();
return $this->body;
}
public function getAllKeysAndValuesForNamespace(string $accountID, string $namespaceIdentifier)
{
$keys = $this->listNamespaceKeys($accountID, $namespaceIdentifier);
foreach ($keys as $index => $values) {
$value = $this->getReadKeyValuePair($accountID, $namespaceIdentifier, $values->name);
$keys[$index]->value = $value;
}
return $keys;
}
public function getListOfNamespaces(string $accountID, int $page = 1, int $perPage = 50, string $order = '', string $direction = '')
{
$query = [
'page' => $page,
'per_page' => $perPage
];
if (!empty($order)) {
$query['order'] = $order;
}
if (!empty($direction)) {
$query['direction'] = $direction;
}
$namespace = $this->adapter->get('accounts/' . $accountID . '/storage/kv/namespaces', $query);
$this->body = json_decode($namespace->getBody());
return $this->body->result;
}
public function writeMultipleKeyValuePairs(string $accountID, $namespaceIdentifier, $keys = []): bool
{
$this->adapter->put('accounts/' . $accountID . '/storage/kv/namespaces/' . $namespaceIdentifier . '/bulk', $keys);
return true;
}
public function deleteKeyValuePair(string $accountID, $namespaceIdentifier, string $key): bool
{
$this->adapter->delete('accounts/' . $accountID . '/storage/kv/namespaces/' . $namespaceIdentifier . '/values', [$key]);
return true;
}
}