-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractProtocol.php
More file actions
executable file
·153 lines (140 loc) · 4.22 KB
/
AbstractProtocol.php
File metadata and controls
executable file
·153 lines (140 loc) · 4.22 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
146
147
148
149
150
151
152
153
<?php
namespace MaplePHP\Nest;
abstract class AbstractProtocol
{
protected $isStart = false;
protected $protocol = array();
protected $protocolID = array();
protected $statusCode = 200;
protected $path = array();
/**
* Propagate the active returnable data
* @return callable
*/
abstract protected function propagateActiveData(): callable;
/**
* Find and protocol item with key
* @param string|int|float|null $key
* @return array|false
*/
public function exists(string|int|float|null $key): array|false
{
$key = (string)$key;
return (isset($this->protocol[$key]) ? $this->protocol[$key] : false);
}
/**
* Find and the URI before the key
* @param string $slug
* @return array|false
*/
public function before(string $slug): array|false
{
return $this->beforeAfter($slug, -1);
}
/**
* Find and the URI after the key
* @param string $slug
* @return array|false
*/
public function after(string $slug): array|false
{
return $this->beforeAfter($slug, 1);
}
/**
* Get Data By slug
* @param string $slug (string becouse of addes duplicate preflix)
* @return array|null
*/
public function getPart(?string $slug): ?array
{
if ($row = $this->exists($slug)) {
return $row;
}
return null;
}
/**
* Get multiple parts
* @param array $arr array with multiple slugs
* @param callable|null $call
* @return array
*/
public function getMultipleParts(array $arr, ?callable $call = null): array
{
$new = array();
foreach ($arr as $slug) {
if ($data = $this->getPart($slug)) {
$new[] = $data;
if (!is_null($call)) {
$call($data);
}
}
}
return $new;
}
/**
* Propagate protocol data
* @param array $vars Slugs in array
* @param string|int $identifier ID
* @param mixed $data Data to be passed on to protocol (will be returned in validation)
*/
public function add(array $vars, string|int $identifier, mixed $data): void
{
$key = end($vars);
$this->protocol[$key] = array("uri" => $vars, "id" => $identifier, "data" => $data);
$this->protocolID[$identifier] = $key;
}
/**
* Check if a 301 redirect result
* @param array $vars
* @return bool
*/
final protected function is301(array $vars): bool
{
if (count($vars) !== count($this->path)) {
return true;
} else {
foreach ($vars as $paramKey => $paramVal) {
if ((empty($this->path[$paramKey]) || (string)$this->path[$paramKey] !== (string)$paramVal) || !in_array($paramVal, $this->path)) {
return true;
}
}
}
return true;
}
/**
* Validate and Propagate start data as active returnable data if is valid
* @return void
*/
final protected function validateStartObject(array $vars): void
{
if ($this->isStart) {
if ($home = reset($this->protocol)) {
$end = is_array($home['uri'] ?? []) ? end($home['uri']) : "";
if (is_array($home) && isset($home['id']) && (!$vars || ($vars === $end))) {
$this->getMultipleParts($home, $this->propagateActiveData());
} else {
$this->statusCode = 404;
}
} else {
$this->statusCode = 404;
}
}
}
/**
* Find and the URI after the key
* @param string $slug
* @param int $increment
* @return array|false
*/
final public function beforeAfter(string $slug, int $increment = 0): array|false
{
$keys = array_keys($this->protocol);
$searchResult = array_search($slug, $keys);
$key = (int)$searchResult + $increment;
if ($searchResult !== false && ($key >= 0) && $key < count($keys)) {
$find = ($keys[$key] ?? null);
return $this->exists($find);
}
return false;
}
}