Skip to content

Commit ecd6353

Browse files
committed
Route information
1 parent 1d481e2 commit ecd6353

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/RouteInformation.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
class RouteInformation
6+
{
7+
8+
public function __construct(
9+
private string $route,
10+
private string $method,
11+
private $callback,
12+
) {
13+
}
14+
15+
public function get_method(): string
16+
{
17+
return $this->method;
18+
}
19+
20+
public function is_post(): bool
21+
{
22+
return $this->method === 'POST';
23+
}
24+
25+
public function is_put(): bool
26+
{
27+
return $this->method === 'PUT';
28+
}
29+
30+
public function is_delete(): bool
31+
{
32+
return $this->method === 'DELETE';
33+
}
34+
35+
public function is_put(): bool
36+
{
37+
return $this->method === 'PUT';
38+
}
39+
40+
public function is_singular(): bool
41+
{
42+
// Always true
43+
if (str_ends_with($this->route, '(?P<id>[\d]+)')) {
44+
return true;
45+
}
46+
47+
// Never true
48+
if ( ! str_contains($this->route, '?P<id>')) {
49+
return false;
50+
}
51+
52+
return false;
53+
}
54+
55+
public function is_list(): bool
56+
{
57+
return ! $this->is_singular();
58+
}
59+
60+
public function is_wp_rest_controller()
61+
{
62+
$allowed = [
63+
WP_REST_Posts_Controller::class,
64+
WP_REST_Users_Controller::class,
65+
WP_REST_Taxonomies_Controller::class,
66+
];
67+
68+
foreach ($allowed as $controller) {
69+
if ($this->callback[0] instanceof $controller) {
70+
return true;
71+
}
72+
}
73+
74+
return false;
75+
}
76+
77+
}

0 commit comments

Comments
 (0)