Skip to content

Commit

Permalink
Add withResourceType() and strict checks for the Format
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Nov 14, 2018
1 parent 98b8cdb commit fe9b05c
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 83 deletions.
7 changes: 6 additions & 1 deletion Classes/Documentation/Handler/DummyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function getResourceType()
return $this->resourceType;
}

public function withResourceType(ResourceType $resourceType)
{
return clone $this;
}

public function getSentData()
{
return null;
Expand Down Expand Up @@ -69,7 +74,7 @@ public function getRootObjectKey()
return '';
}

public function withFormat($format)
public function withFormat(Format $format)
{
return clone $this;
}
Expand Down
24 changes: 19 additions & 5 deletions Classes/Domain/Model/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __construct($format)
}

/**
* Returns an instance of the default format
* Return an instance of the default format
*
* @return Format
*/
Expand All @@ -61,18 +61,32 @@ public static function defaultFormat()
}

/**
* The __toString method allows a class to decide how it will react when it is converted to a string.
* Return a HTML Format instance
*
* @return string
* @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring
* @return Format
*/
public static function formatHtml()
{
return new static('html');
}

/**
* Return a JSON Format instance
*
* @return Format
*/
public static function formatJson()
{
return new static('json');
}

public function __toString()
{
return $this->format;
}

/**
* Returns if the given format is valid
* Return if the given format is valid
*
* @param $format
* @return boolean
Expand Down
39 changes: 27 additions & 12 deletions Classes/Http/RestRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
interface RestRequestInterface extends ServerRequestInterface
{
/**
* Returns the original request
* Return the original request
*
* @return ServerRequestInterface
*/
public function getOriginalRequest();

/**
* Returns the request path (eventually aliases have been mapped)
* Return the request path (eventually aliases have been mapped)
*
* @return string
*/
public function getPath();

/**
* Returns the requested resource type
* Return the requested resource type
*
* The resource type is the first part of the request path, after mapping aliases
*
Expand All @@ -36,51 +36,66 @@ public function getPath();
public function getResourceType();

/**
* Returns the sent data
* Return the sent data
*
* @return mixed
*/
public function getSentData();

/**
* Returns the requested format
* Return the requested format
*
* @return Format
*/
public function getFormat();

/**
* Returns if the request is a preflight request
* Return if the request is a preflight request
*
* @return bool
*/
public function isPreflight();

/**
* Returns if the request wants to write data
* Return if the request wants to write data
*
* @return bool
*/
public function isWrite();

/**
* Returns if the request wants to read data
* Return if the request wants to read data
*
* @return bool
*/
public function isRead();

/**
* Returns the key to use for the root object if addRootObjectForCollection
* is enabled
* Return the key to use for the root object if addRootObjectForCollection is enabled
*
* @return string
*/
public function getRootObjectKey();

/**
* @param $format
* Return an instance with the given format
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message.
*
* @param Format $format
* @return static
*/
public function withFormat(Format $format);

/**
* Return an instance with the given Resource Type
*
* This method MUST be implemented in such a way as to retain the
* immutability of the message.
*
* @param ResourceType $resourceType
* @return static
*/
public function withFormat($format);
public function withResourceType(ResourceType $resourceType);
}
71 changes: 16 additions & 55 deletions Classes/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,43 +84,24 @@ public function getOriginalRequest()
return $this->originalRequest;
}

/**
* Returns the request path (eventually aliases have been mapped)
*
* @return string
*/
public function getPath()
{
return $this->internalUri->getPath();
}

/**
* Returns the requested resource type
*
* The resource type is the first part of the request path, after mapping aliases
*
* @return ResourceType
*/
public function getResourceType()
{
return $this->resourceType;
}

/**
* Returns the request path before mapping aliases
*
* @return string
*/
public function getOriginalResourceType()
public function withResourceType(ResourceType $resourceType)
{
return (string)strtok(strtok($this->originalPath, '?'), '/');
$clone = clone $this;
$clone->resourceType = $resourceType;

return $clone;
}

/**
* Returns the sent data
*
* @return mixed
*/
public function getSentData()
{
if ($this->sentData) {
Expand Down Expand Up @@ -148,62 +129,32 @@ function ($isFormEncoded, $contentType) {
return $this->sentData;
}

/**
* Returns the requested format
*
* @return Format
*/
public function getFormat()
{
return $this->format;
}

/**
* Returns if the request is a preflight request
*
* @return bool
*/
public function isPreflight()
{
return strtoupper($this->getMethod()) === 'OPTIONS';
}

/**
* Returns if the request wants to write data
*
* @return bool
*/
public function isWrite()
{
return !$this->isRead() && !$this->isPreflight();
}

/**
* Returns if the request wants to read data
*
* @return bool
*/
public function isRead()
{
return in_array(strtoupper($this->getMethod()), ['GET', 'HEAD']);
}

/**
* Returns the key to use for the root object if addRootObjectForCollection
* is enabled
*
* @return string
*/
public function getRootObjectKey()
{
return $this->getOriginalResourceType();
}

/**
* @param $format
* @return static
*/
public function withFormat($format)
public function withFormat(Format $format)
{
return new static(
$this->originalRequest,
Expand All @@ -214,6 +165,16 @@ public function withFormat($format)
);
}

/**
* Returns the request path before mapping aliases
*
* @return string
*/
public function getOriginalResourceType()
{
return (string)strtok(strtok($this->originalPath, '?'), '/');
}

/**
* @param ServerRequestInterface $request
* @return $this
Expand Down
Loading

0 comments on commit fe9b05c

Please sign in to comment.