-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use Hal and vnd.error repsonse objects
- Loading branch information
Showing
22 changed files
with
595 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal\Exception; | ||
|
||
use Nocarrier\Hal; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
|
||
class FormErrorException extends BadRequestHttpException implements HalException | ||
{ | ||
private $form; | ||
private $logref; | ||
|
||
public function __construct(FormInterface $form, | ||
$message = null, | ||
$logref = null, | ||
\Exception $previous = null, | ||
$code = 0) | ||
{ | ||
parent::__construct($message, $previous, $code); | ||
|
||
$this->form = $form; | ||
$this->logref = $logref; | ||
} | ||
|
||
public function getHal() | ||
{ | ||
$data = array( | ||
'message' => $this->getMessage() | ||
); | ||
|
||
if ($this->logref) { | ||
$data['@logref'] = $this->logref; | ||
} | ||
|
||
$hal = new Hal(null, $data); | ||
|
||
$this->appendErrors($hal, $this->form, ''); | ||
|
||
return $hal; | ||
} | ||
|
||
private function appendErrors(Hal $hal, FormInterface $form, $path) | ||
{ | ||
foreach ($form->getErrors() as $key => $error) { | ||
$data = array( | ||
'message' => $error->getMessage() | ||
); | ||
|
||
$path = rtrim($path, '/'); | ||
|
||
if ($path) { | ||
$data['path'] = $path; | ||
} | ||
|
||
$hal->addResource('errors', new Hal(null, $data)); | ||
} | ||
|
||
foreach ($form->all() as $child) { | ||
$this->appendErrors($hal, $child, $path . '/' . $child->getName()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal\Exception; | ||
|
||
use Nocarrier\Hal; | ||
|
||
interface HalException | ||
{ | ||
/** | ||
* @return Hal | ||
*/ | ||
public function getHal(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal\Exception; | ||
|
||
use Nocarrier\Hal; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
use Symfony\Component\Validator\ConstraintViolationList; | ||
|
||
class ValidationErrorException extends BadRequestHttpException implements HalException | ||
{ | ||
private $violationList; | ||
private $logref; | ||
|
||
public function __construct(ConstraintViolationList $violationList, | ||
$message = null, | ||
$logref = null, | ||
\Exception $previous = null, | ||
$code = 0) | ||
{ | ||
parent::__construct($message, $previous, $code); | ||
|
||
$this->violationList = $violationList; | ||
$this->logref = $logref; | ||
} | ||
|
||
public function getHal() | ||
{ | ||
$data = array( | ||
'message' => $this->getMessage() | ||
); | ||
|
||
if ($this->logref) { | ||
$data['@logref'] = $this->logref; | ||
} | ||
|
||
$hal = new Hal(null, $data); | ||
|
||
foreach ($this->violationList as $violation){ | ||
$path = str_replace('][', '/', $violation->getPropertyPath()); | ||
$path = '/' . trim($path, '[]'); | ||
|
||
$data = array( | ||
'message' => $violation->getMessage(), | ||
'path' => $path | ||
); | ||
|
||
$hal->addResource('errors', new Hal(null, $data)); | ||
} | ||
|
||
return $hal; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Jsor\Stack\Hal\Response; | ||
|
||
use Nocarrier\Hal; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class HalResponse extends Response | ||
{ | ||
protected $hal; | ||
protected $requestFormat; | ||
protected $prettyPrint; | ||
|
||
public function __construct(Hal $hal, $status = 200, $headers = array(), $prettyPrint = false) | ||
{ | ||
parent::__construct(null, $status, $headers); | ||
|
||
$this->hal = $hal; | ||
$this->prettyPrint = (bool) $prettyPrint; | ||
|
||
$this->requestFormat = 'json'; | ||
$this->headers->set('Content-Type', 'application/hal+json'); | ||
} | ||
|
||
public static function create($hal = null, $status = 200, $headers = array(), $prettyPrint = false) | ||
{ | ||
return new static($hal, $status, $headers, $prettyPrint); | ||
} | ||
|
||
public function prepare(Request $request) | ||
{ | ||
if ('xml' === $request->getRequestFormat()) { | ||
$this->requestFormat = 'xml'; | ||
$this->headers->set('Content-Type', 'application/hal+xml'); | ||
} | ||
|
||
return parent::prepare($request); | ||
} | ||
|
||
public function sendContent() | ||
{ | ||
echo $this->getContent(); | ||
|
||
return $this; | ||
} | ||
|
||
public function setContent($content) | ||
{ | ||
if (null !== $content && !$content instanceof Hal) { | ||
throw new \LogicException('The content must be a Hal instance.'); | ||
} | ||
|
||
$this->hal = $content; | ||
} | ||
|
||
public function getContent() | ||
{ | ||
if (null === $this->hal) { | ||
return null; | ||
} | ||
|
||
switch ($this->requestFormat) { | ||
case 'xml': | ||
return $this->hal->asXml($this->prettyPrint); | ||
default: | ||
return $this->hal->asJson($this->prettyPrint); | ||
} | ||
} | ||
} |
Oops, something went wrong.