Skip to content

Instantly share code, notes, and snippets.

@lorenzo
Created February 16, 2016 10:03
Show Gist options
  • Save lorenzo/3c9359300ff422d0a14c to your computer and use it in GitHub Desktop.
Save lorenzo/3c9359300ff422d0a14c to your computer and use it in GitHub Desktop.
<?php
namespace App\Form;
use JsonSerializable;
class ErrorBag implements JsonSerializable
{
protected $_errors;
public function __construct($errors)
{
$this->_errors = $errors;
}
public function jsonSerialize()
{
$result = [];
foreach ($this->_errors as $field => $error) {
$result[] = [
'code' => key($error),
'parameter' => $field,
'detail' => current($error)
];
}
return $result;
}
}
<?php
$entityWithErrors = ...;
$errors = new ErrorBag($entityWithErrors->errors());
$this->set('errors', $errors);
$this->set('_serialize', ['errors']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment