-
Notifications
You must be signed in to change notification settings - Fork 5
/
Path.php
99 lines (86 loc) · 2.35 KB
/
Path.php
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
<?php
namespace WPOpenAPI\Spec;
class Path {
/**
* @var string
*/
private string $path;
/**
* @var Operation[]
*/
private array $operations = array();
private string $originalPath;
private array $pathVariables = array();
private ?string $schemaRef;
public function __construct(
string $path,
string $schemaRef = null
) {
$this->originalPath = $path;
$this->path = $this->replacePathVariable( $path );
if ( $schemaRef ) {
$this->schemaRef = '#/components/schemas/' . $schemaRef;
}
}
public function replacePathVariable( string $path ): string {
if ( str_contains( $path, '(?P<' ) ) {
$path = trim( $path, '?' );
$path = preg_replace_callback(
'/\(.*?<([^<>]*)>.*?\)(?=\/|$|\+)/',
function ( $match ) {
$this->pathVariables[] = $match[1];
return '{' . $match[1] . '}';
},
$path
);
// it's possible that the path still have unwanted chars left
// after the preg replacement. Clean them up.
// @todo -- find a better regex that works for all
$path = str_replace( array( '(', ')', '?' ), '', $path );
}
return $path;
}
public function getOperations(): array {
return $this->operations;
}
public function generateOperationsFromRouteArgs( $args ): void {
foreach ( $args as $arg ) {
$responses = array();
if ( ! empty( $this->schemaRef ) ) {
$content = new ResponseContent(
'application/json',
array(
'$ref' => $this->schemaRef,
)
);
$response = new Response( 200, 'OK' );
$response->addContent( $content );
$responses[] = $response;
} else {
$responses[] = new Response( 200, 'OK' );
}
foreach ( $arg['methods'] as $method => $value ) {
$description = $arg['description'] ?? '';
$method = strtolower( $method );
if($method == 'options') continue;
$op = new Operation( $method, $responses );
$op->setDescription( $description );
$op->generateParametersFromRouteArgs( $method, $arg['args'], $this->pathVariables, $this->path );
$this->operations[] = $op;
}
}
}
public function getOriginalPath(): string {
return $this->originalPath;
}
public function getPath(): string {
return $this->path;
}
public function toArray(): array {
$data = array();
foreach ( $this->operations as $operation ) {
$data[ $operation->getMethod() ] = $operation->toArray();
}
return $data;
}
}