Skip to content

Commit fe9b05c

Browse files
committed
Add withResourceType() and strict checks for the Format
1 parent 98b8cdb commit fe9b05c

File tree

5 files changed

+87
-83
lines changed

5 files changed

+87
-83
lines changed

Classes/Documentation/Handler/DummyRequest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function getResourceType()
3939
return $this->resourceType;
4040
}
4141

42+
public function withResourceType(ResourceType $resourceType)
43+
{
44+
return clone $this;
45+
}
46+
4247
public function getSentData()
4348
{
4449
return null;
@@ -69,7 +74,7 @@ public function getRootObjectKey()
6974
return '';
7075
}
7176

72-
public function withFormat($format)
77+
public function withFormat(Format $format)
7378
{
7479
return clone $this;
7580
}

Classes/Domain/Model/Format.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function __construct($format)
5151
}
5252

5353
/**
54-
* Returns an instance of the default format
54+
* Return an instance of the default format
5555
*
5656
* @return Format
5757
*/
@@ -61,18 +61,32 @@ public static function defaultFormat()
6161
}
6262

6363
/**
64-
* The __toString method allows a class to decide how it will react when it is converted to a string.
64+
* Return a HTML Format instance
6565
*
66-
* @return string
67-
* @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring
66+
* @return Format
6867
*/
68+
public static function formatHtml()
69+
{
70+
return new static('html');
71+
}
72+
73+
/**
74+
* Return a JSON Format instance
75+
*
76+
* @return Format
77+
*/
78+
public static function formatJson()
79+
{
80+
return new static('json');
81+
}
82+
6983
public function __toString()
7084
{
7185
return $this->format;
7286
}
7387

7488
/**
75-
* Returns if the given format is valid
89+
* Return if the given format is valid
7690
*
7791
* @param $format
7892
* @return boolean

Classes/Http/RestRequestInterface.php

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313
interface RestRequestInterface extends ServerRequestInterface
1414
{
1515
/**
16-
* Returns the original request
16+
* Return the original request
1717
*
1818
* @return ServerRequestInterface
1919
*/
2020
public function getOriginalRequest();
2121

2222
/**
23-
* Returns the request path (eventually aliases have been mapped)
23+
* Return the request path (eventually aliases have been mapped)
2424
*
2525
* @return string
2626
*/
2727
public function getPath();
2828

2929
/**
30-
* Returns the requested resource type
30+
* Return the requested resource type
3131
*
3232
* The resource type is the first part of the request path, after mapping aliases
3333
*
@@ -36,51 +36,66 @@ public function getPath();
3636
public function getResourceType();
3737

3838
/**
39-
* Returns the sent data
39+
* Return the sent data
4040
*
4141
* @return mixed
4242
*/
4343
public function getSentData();
4444

4545
/**
46-
* Returns the requested format
46+
* Return the requested format
4747
*
4848
* @return Format
4949
*/
5050
public function getFormat();
5151

5252
/**
53-
* Returns if the request is a preflight request
53+
* Return if the request is a preflight request
5454
*
5555
* @return bool
5656
*/
5757
public function isPreflight();
5858

5959
/**
60-
* Returns if the request wants to write data
60+
* Return if the request wants to write data
6161
*
6262
* @return bool
6363
*/
6464
public function isWrite();
6565

6666
/**
67-
* Returns if the request wants to read data
67+
* Return if the request wants to read data
6868
*
6969
* @return bool
7070
*/
7171
public function isRead();
7272

7373
/**
74-
* Returns the key to use for the root object if addRootObjectForCollection
75-
* is enabled
74+
* Return the key to use for the root object if addRootObjectForCollection is enabled
7675
*
7776
* @return string
7877
*/
7978
public function getRootObjectKey();
8079

8180
/**
82-
* @param $format
81+
* Return an instance with the given format
82+
*
83+
* This method MUST be implemented in such a way as to retain the
84+
* immutability of the message.
85+
*
86+
* @param Format $format
87+
* @return static
88+
*/
89+
public function withFormat(Format $format);
90+
91+
/**
92+
* Return an instance with the given Resource Type
93+
*
94+
* This method MUST be implemented in such a way as to retain the
95+
* immutability of the message.
96+
*
97+
* @param ResourceType $resourceType
8398
* @return static
8499
*/
85-
public function withFormat($format);
100+
public function withResourceType(ResourceType $resourceType);
86101
}

Classes/Request.php

Lines changed: 16 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -84,43 +84,24 @@ public function getOriginalRequest()
8484
return $this->originalRequest;
8585
}
8686

87-
/**
88-
* Returns the request path (eventually aliases have been mapped)
89-
*
90-
* @return string
91-
*/
9287
public function getPath()
9388
{
9489
return $this->internalUri->getPath();
9590
}
9691

97-
/**
98-
* Returns the requested resource type
99-
*
100-
* The resource type is the first part of the request path, after mapping aliases
101-
*
102-
* @return ResourceType
103-
*/
10492
public function getResourceType()
10593
{
10694
return $this->resourceType;
10795
}
10896

109-
/**
110-
* Returns the request path before mapping aliases
111-
*
112-
* @return string
113-
*/
114-
public function getOriginalResourceType()
97+
public function withResourceType(ResourceType $resourceType)
11598
{
116-
return (string)strtok(strtok($this->originalPath, '?'), '/');
99+
$clone = clone $this;
100+
$clone->resourceType = $resourceType;
101+
102+
return $clone;
117103
}
118104

119-
/**
120-
* Returns the sent data
121-
*
122-
* @return mixed
123-
*/
124105
public function getSentData()
125106
{
126107
if ($this->sentData) {
@@ -148,62 +129,32 @@ function ($isFormEncoded, $contentType) {
148129
return $this->sentData;
149130
}
150131

151-
/**
152-
* Returns the requested format
153-
*
154-
* @return Format
155-
*/
156132
public function getFormat()
157133
{
158134
return $this->format;
159135
}
160136

161-
/**
162-
* Returns if the request is a preflight request
163-
*
164-
* @return bool
165-
*/
166137
public function isPreflight()
167138
{
168139
return strtoupper($this->getMethod()) === 'OPTIONS';
169140
}
170141

171-
/**
172-
* Returns if the request wants to write data
173-
*
174-
* @return bool
175-
*/
176142
public function isWrite()
177143
{
178144
return !$this->isRead() && !$this->isPreflight();
179145
}
180146

181-
/**
182-
* Returns if the request wants to read data
183-
*
184-
* @return bool
185-
*/
186147
public function isRead()
187148
{
188149
return in_array(strtoupper($this->getMethod()), ['GET', 'HEAD']);
189150
}
190151

191-
/**
192-
* Returns the key to use for the root object if addRootObjectForCollection
193-
* is enabled
194-
*
195-
* @return string
196-
*/
197152
public function getRootObjectKey()
198153
{
199154
return $this->getOriginalResourceType();
200155
}
201156

202-
/**
203-
* @param $format
204-
* @return static
205-
*/
206-
public function withFormat($format)
157+
public function withFormat(Format $format)
207158
{
208159
return new static(
209160
$this->originalRequest,
@@ -214,6 +165,16 @@ public function withFormat($format)
214165
);
215166
}
216167

168+
/**
169+
* Returns the request path before mapping aliases
170+
*
171+
* @return string
172+
*/
173+
public function getOriginalResourceType()
174+
{
175+
return (string)strtok(strtok($this->originalPath, '?'), '/');
176+
}
177+
217178
/**
218179
* @param ServerRequestInterface $request
219180
* @return $this

0 commit comments

Comments
 (0)