-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathRuleset.php
More file actions
145 lines (119 loc) · 4.38 KB
/
Ruleset.php
File metadata and controls
145 lines (119 loc) · 4.38 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace Cloudflare\API\Endpoints;
use Cloudflare\API\Adapter\Adapter;
class Ruleset implements API
{
private $adapter;
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
/**
* Get all rulesets for a zone.
*
* @param string $zoneID The ID of the zone.
* @return array The list of rulesets.
*/
public function listZoneRulesets(string $zoneID): array
{
$response = $this->adapter->get("zones/{$zoneID}/rulesets");
$body = $response->getBody()->getContents();
$data = json_decode($body, true);
return $data["result"] ?? [];
}
/**
* Get rulesets for a specific phase within a zone.
*
* @param string $zoneID The ID of the zone.
* @param string $phase The phase of the ruleset (e.g., http_request_dynamic_redirect).
* @return array The filtered list of rulesets.
*/
public function getRulesetsByPhase(string $zoneID, string $phase): array
{
$rulesets = $this->listZoneRulesets($zoneID);
return array_filter($rulesets, function ($ruleset) use ($phase) {
return isset($ruleset['phase']) && $ruleset['phase'] === $phase;
});
}
/**
* Get a specific ruleset by ID.
*
* @param string $zoneID The ID of the zone.
* @param string $rulesetID The ID of the ruleset.
* @return array The ruleset details.
*/
public function getRuleset(string $zoneID, string $rulesetID): array
{
$response = $this->adapter->get("zones/{$zoneID}/rulesets/{$rulesetID}");
$body = $response->getBody()->getContents();
$data = json_decode($body, true);
return $data["result"] ?? [];
}
/**
* Create a new ruleset for a zone.
*
* @param string $zoneID The ID of the zone.
* @param array $payload The payload for the new ruleset.
* @return array The created ruleset details.
*/
public function createRuleset(string $zoneID, array $payload): array
{
$response = $this->adapter->post("zones/{$zoneID}/rulesets", $payload);
$body = $response->getBody()->getContents();
$data = json_decode($body, true);
return $data["result"] ?? [];
}
/**
* Update an existing ruleset.
*
* @param string $zoneID The ID of the zone.
* @param string $rulesetID The ID of the ruleset.
* @param array $payload The payload with updated ruleset details.
* @return array The updated ruleset details.
*/
public function updateRuleset(string $zoneID, string $rulesetID, array $payload): array
{
$response = $this->adapter->put("zones/{$zoneID}/rulesets/{$rulesetID}", $payload);
$body = $response->getBody()->getContents();
$data = json_decode($body, true);
return $data["result"] ?? [];
}
/**
* Delete a specific rule by name from a ruleset.
*
* @param string $zoneID The ID of the zone.
* @param string $rulesetID The ID of the ruleset.
* @param string $ruleName The name of the rule to delete.
* @return bool True if deletion was successful, false otherwise.
*/
public function deleteRuleByName(string $zoneID, string $rulesetID, string $ruleName): bool
{
$rulesetDetails = $this->getRuleset($zoneID, $rulesetID);
$rules = $rulesetDetails['rules'] ?? [];
// Filter out the rule with the specified name
$updatedRules = array_filter($rules, function ($rule) use ($ruleName) {
return $rule['description'] !== $ruleName;
});
if (count($updatedRules) === count($rules)) {
// No rule was removed
return false;
}
$payload = ['rules' => array_values($updatedRules)];
$updatedRuleset = $this->updateRuleset($zoneID, $rulesetID, $payload);
return !empty($updatedRuleset);
}
/**
* Delete a ruleset from a zone.
*
* @param string $zoneID The ID of the zone.
* @param string $rulesetID The ID of the ruleset.
* @return bool True if deletion was successful, false otherwise.
*/
public function deleteRuleset(string $zoneID, string $rulesetID): bool
{
$response = $this->adapter->delete("zones/{$zoneID}/rulesets/{$rulesetID}");
$body = $response->getBody()->getContents();
$data = json_decode($body, true);
return $data["success"] ?? false;
}
}