Skip to content

Commit dc2da61

Browse files
author
Wazabii
committed
First commit
1 parent e39e3f4 commit dc2da61

File tree

6 files changed

+1004
-1
lines changed

6 files changed

+1004
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
._*
2+
.DS_Store
3+
.sass-cache

AbstractProtocol.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
namespace MaplePHP\Nest;
4+
5+
abstract class AbstractProtocol
6+
{
7+
8+
protected $isStart = false;
9+
protected $protocol = array();
10+
protected $protocolID = array();
11+
protected $statusCode = 200;
12+
protected $path = array();
13+
14+
/**
15+
* Propagate the active returnable data
16+
* @return callable
17+
*/
18+
abstract protected function propagateActiveData(): callable;
19+
20+
/**
21+
* Find and protocol item with key
22+
* @param string|int|float|null $key
23+
* @return array|false
24+
*/
25+
public function exists(string|int|float|null $key): array|false
26+
{
27+
$key = (string)$key;
28+
return (isset($this->protocol[$key]) ? $this->protocol[$key] : false);
29+
}
30+
31+
/**
32+
* Find and the URI before the key
33+
* @param string $slug
34+
* @return array|false
35+
*/
36+
public function before(string $slug): array|false
37+
{
38+
return $this->beforeAfter($slug, -1);
39+
}
40+
41+
/**
42+
* Find and the URI after the key
43+
* @param string $slug
44+
* @return array|false
45+
*/
46+
public function after(string $slug): array|false
47+
{
48+
return $this->beforeAfter($slug, 1);
49+
}
50+
51+
/**
52+
* Get Data By slug
53+
* @param string $slug (string becouse of addes duplicate preflix)
54+
* @return array|null
55+
*/
56+
public function getPart(?string $slug): ?array
57+
{
58+
if ($row = $this->exists($slug)) {
59+
return $row;
60+
}
61+
return null;
62+
}
63+
64+
/**
65+
* Get multiple parts
66+
* @param array $arr array with multiple slugs
67+
* @param callable|null $call
68+
* @return array
69+
*/
70+
public function getMultipleParts(array $arr, ?callable $call = null): array
71+
{
72+
$new = array();
73+
foreach ($arr as $slug) {
74+
if ($data = $this->getPart($slug)) {
75+
$new[] = $data;
76+
if (!is_null($call)) {
77+
$call($data);
78+
}
79+
}
80+
}
81+
return $new;
82+
}
83+
84+
/**
85+
* Propagate protocol data
86+
* @param array $vars Slugs in array
87+
* @param string|int $identifier ID
88+
* @param mixed $data Data to be passed on to protocol (will be returned in validation)
89+
*/
90+
public function add(array $vars, string|int $identifier, mixed $data): void
91+
{
92+
$key = end($vars);
93+
$this->protocol[$key] = array("uri" => $vars, "id" => $identifier, "data" => $data);
94+
$this->protocolID[$identifier] = $key;
95+
}
96+
97+
/**
98+
* Check if a 301 redirect result
99+
* @param array $vars
100+
* @return bool
101+
*/
102+
final protected function is301(array $vars): bool
103+
{
104+
if (count($vars) !== count($this->path)) {
105+
return true;
106+
} else {
107+
foreach ($vars as $paramKey => $paramVal) {
108+
if ((empty($this->path[$paramKey]) || (string)$this->path[$paramKey] !== (string)$paramVal) || !in_array($paramVal, $this->path)) {
109+
return true;
110+
}
111+
}
112+
}
113+
return true;
114+
}
115+
116+
/**
117+
* Validate and Propagate start data as active returnable data if is valid
118+
* @return void
119+
*/
120+
final protected function validateStartObject(array $vars): void
121+
{
122+
if ($this->isStart) {
123+
if ($home = reset($this->protocol)) {
124+
$end = is_array($home['uri'] ?? []) ? end($home['uri']) : "";
125+
if (is_array($home) && isset($home['id']) && (!$vars || ($vars === $end))) {
126+
$this->getMultipleParts($home, $this->propagateActiveData());
127+
} else {
128+
$this->statusCode = 404;
129+
}
130+
} else {
131+
$this->statusCode = 404;
132+
}
133+
}
134+
}
135+
136+
/**
137+
* Find and the URI after the key
138+
* @param string $slug
139+
* @param int $increment
140+
* @return array|false
141+
*/
142+
final public function beforeAfter(string $slug, int $increment = 0): array|false
143+
{
144+
$keys = array_keys($this->protocol);
145+
$searchResult = array_search($slug, $keys);
146+
$key = (int)$searchResult + $increment;
147+
if ($searchResult !== false && ($key >= 0) && $key < count($keys)) {
148+
$find = ($keys[$key] ?? null);
149+
return $this->exists($find);
150+
}
151+
return false;
152+
}
153+
}

0 commit comments

Comments
 (0)