|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Trucker\Url; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Inflector\Inflector; |
|
6
|
|
|
use Illuminate\Container\Container; |
|
7
|
|
|
use Trucker\Resource\Model; |
|
8
|
|
|
|
|
9
|
|
|
class UrlGenerator |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* The IoC Container. |
|
13
|
|
|
* |
|
14
|
|
|
* @var Container |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $app; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Build a new UrlGenerator. |
|
20
|
|
|
* |
|
21
|
|
|
* @param Container $app |
|
22
|
|
|
*/ |
|
23
|
2 |
|
public function __construct(Container $app) |
|
24
|
|
|
{ |
|
25
|
2 |
|
$this->app = $app; |
|
26
|
2 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Getter to access the IoC Container. |
|
30
|
|
|
* |
|
31
|
|
|
* @return Container |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function getApp() |
|
34
|
|
|
{ |
|
35
|
1 |
|
return $this->app; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Function to get the URI with placeholders for data |
|
40
|
|
|
* that a POST request should be made to in order to create |
|
41
|
|
|
* a new entity. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Model $model |
|
44
|
|
|
* @param array $options Array of options to replace placeholders with |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
5 |
|
public function getCreateUri($model, array $options = []) |
|
49
|
|
|
{ |
|
50
|
5 |
|
return $this->getCollectionUri($model, $options); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Function to get the URI with placeholders for data |
|
55
|
|
|
* that a PUT / PATCH request should be made to in order to |
|
56
|
|
|
* update an existing entity. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Model $model |
|
59
|
|
|
* @param array $options Array of options to replace placeholders with |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function getUpdateUri($model, array $options = []) |
|
64
|
|
|
{ |
|
65
|
1 |
|
return $this->getInstanceUri($model, $options); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Function to get the URI with placeholders for data |
|
70
|
|
|
* that a DELETE request should be made to in order to delete |
|
71
|
|
|
* an existing entity. |
|
72
|
|
|
* |
|
73
|
|
|
* @param Model $model |
|
74
|
|
|
* @param array $options Array of options to replace placeholders with |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
9 |
|
public function getDeleteUri($model, array $options = []) |
|
79
|
|
|
{ |
|
80
|
9 |
|
return $this->getInstanceUri($model, $options); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Function to get the URI with placeholders for data |
|
85
|
|
|
* that a GET request should be made to in order to retreive |
|
86
|
|
|
* a collection of Entities. |
|
87
|
|
|
* |
|
88
|
|
|
* @param Model $model |
|
89
|
|
|
* @param array $options Array of options to replace placeholders with |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
12 |
|
public function getCollectionUri($model, array $options = []) |
|
94
|
|
|
{ |
|
95
|
12 |
|
$uri = $this->getURI($model); |
|
96
|
12 |
|
foreach ($options as $key => $value) { |
|
97
|
1 |
|
$uri = str_replace($key, $value, $uri); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
12 |
|
return $uri; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Function to get the URI with placeholders for data |
|
105
|
|
|
* that a GET request should be made to in order to retreive |
|
106
|
|
|
* an instance of an Entity. |
|
107
|
|
|
* |
|
108
|
|
|
* @param Model $model |
|
109
|
|
|
* @param array $options Array of options to replace placeholders with |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
11 |
|
public function getInstanceUri($model, array $options = []) |
|
114
|
|
|
{ |
|
115
|
11 |
|
$uri = implode('/', [$this->getURI($model), ':id']); |
|
116
|
11 |
|
foreach ($options as $key => $value) { |
|
117
|
11 |
|
$uri = str_replace($key, $value, $uri); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
11 |
|
return $uri; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Function to return the name of the URI to hit based on |
|
125
|
|
|
* the interpreted name of the class in question. For example |
|
126
|
|
|
* a Person class would resolve to /people. |
|
127
|
|
|
* |
|
128
|
|
|
* @param Model $model |
|
129
|
|
|
* |
|
130
|
|
|
* @return string The URI to hit |
|
131
|
|
|
*/ |
|
132
|
24 |
|
public function getURI($model) |
|
133
|
|
|
{ |
|
134
|
24 |
|
if ($uri = $model->getURI()) { |
|
135
|
2 |
|
return $uri; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
24 |
|
$uri = Inflector::pluralize( |
|
139
|
24 |
|
Inflector::tableize( |
|
140
|
24 |
|
$model->getResourceName() |
|
141
|
|
|
) |
|
142
|
|
|
); |
|
143
|
|
|
|
|
144
|
24 |
|
$uriResult = []; |
|
145
|
24 |
|
if (!empty($model->nestedUnder)) { |
|
146
|
2 |
|
$nesting = array_map( |
|
147
|
2 |
|
function ($item) { |
|
148
|
2 |
|
return explode(':', trim($item)); |
|
149
|
2 |
|
}, |
|
150
|
2 |
|
explode(',', $model->nestedUnder) |
|
151
|
|
|
); |
|
152
|
2 |
|
foreach ($nesting as $nest) { |
|
153
|
2 |
|
list($klass, $entityIdSegment) = $nest; |
|
154
|
2 |
|
if (!is_numeric($entityIdSegment)) { |
|
155
|
1 |
|
$entityIdSegment = ":$entityIdSegment"; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
2 |
|
$entityTypeSegment = Inflector::pluralize(Inflector::tableize($klass)); |
|
159
|
2 |
|
$uriResult[] = $entityTypeSegment; |
|
160
|
2 |
|
$uriResult[] = $entityIdSegment; |
|
161
|
|
|
} |
|
162
|
2 |
|
$uri = implode('/', $uriResult) . "/$uri"; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
24 |
|
return "/$uri"; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|