-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
319 additions
and
36 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
src/Provider/Rest/Exception/ErrorCollectionException.php
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,122 @@ | ||
<?php | ||
/* | ||
* This file is part of the RestServiceProvider. | ||
* | ||
* (c) Axel Etcheverry <axel@etcheverry.biz> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Euskadi31\Silex\Provider\Rest\Exception; | ||
|
||
use Symfony\Component\HttpKernel\Exception\HttpException; | ||
use Iterator; | ||
use ArrayAccess; | ||
use Countable; | ||
|
||
/** | ||
* ErrorCollectionException | ||
* | ||
* @author Axel Etcheverry <axel@etcheverry.biz> | ||
*/ | ||
class ErrorCollectionException extends HttpException implements Iterator, ArrayAccess, Countable | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* | ||
* @param array $errors | ||
* @param integer $statusCode | ||
* @param array $headers | ||
*/ | ||
public function __construct(array $errors, $statusCode = 400, array $headers = []) | ||
{ | ||
parent::__construct($statusCode, null, null, $headers); | ||
|
||
$this->container = $errors; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function count() | ||
{ | ||
return count($this->container); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function offsetSet($offset, $value) | ||
{ | ||
$this->container[] = $value; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function offsetExists($offset) | ||
{ | ||
return isset($this->container[$offset]); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function offsetUnset($offset) | ||
{ | ||
unset($this->container[$offset]); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function offsetGet($offset) | ||
{ | ||
return isset($this->container[$offset]) ? $this->container[$offset] : null; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function rewind() | ||
{ | ||
return reset($this->container); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function current() | ||
{ | ||
return current($this->container); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function key() | ||
{ | ||
return key($this->container); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function next() | ||
{ | ||
return next($this->container); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function valid() | ||
{ | ||
return key($this->container) !== null; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Provider/Rest/Exception/InvalidParameterExceptionInterface.php
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,21 @@ | ||
<?php | ||
/* | ||
* This file is part of the RestServiceProvider. | ||
* | ||
* (c) Axel Etcheverry <axel@etcheverry.biz> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Euskadi31\Silex\Provider\Rest\Exception; | ||
|
||
/** | ||
* InvalidParameterExceptionInterface | ||
* | ||
* @author Axel Etcheverry <axel@etcheverry.biz> | ||
*/ | ||
interface InvalidParameterExceptionInterface | ||
{ | ||
public function getParameter(); | ||
} |
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
57 changes: 57 additions & 0 deletions
57
tests/Provider/Rest/Exception/ErrorCollectionExceptionTest.php
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,57 @@ | ||
<?php | ||
/* | ||
* This file is part of the RestServiceProvider. | ||
* | ||
* (c) Axel Etcheverry <axel@etcheverry.biz> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Euskadi31\Silex\Provider\Rest\Exception; | ||
|
||
use Euskadi31\Silex\Provider\Rest\Exception\ErrorCollectionException; | ||
|
||
class ErrorCollectionExceptionTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testErrorCollectionException() | ||
{ | ||
$errors = []; | ||
$errors[] = $ex1 = $this->getMock('\Exception'); | ||
$errors[] = $ex2 = $this->getMock('\Exception'); | ||
|
||
$collection = new ErrorCollectionException($errors); | ||
|
||
$this->assertEquals(400, $collection->getStatusCode()); | ||
|
||
$this->assertEquals(2, count($collection)); | ||
|
||
$collection[] = $ex3 = $this->getMock('\Exception'); | ||
|
||
$this->assertEquals(3, count($collection)); | ||
|
||
$this->assertEquals($ex3, $collection[2]); | ||
|
||
$this->assertTrue(isset($collection[2])); | ||
|
||
$this->assertEquals($ex1, $collection->current()); | ||
$this->assertEquals(0, $collection->key()); | ||
$this->assertTrue($collection->valid()); | ||
|
||
$collection->next(); | ||
|
||
$this->assertEquals($ex2, $collection->current()); | ||
$this->assertEquals(1, $collection->key()); | ||
$this->assertTrue($collection->valid()); | ||
|
||
$collection->rewind(); | ||
|
||
$this->assertEquals($ex1, $collection->current()); | ||
$this->assertEquals(0, $collection->key()); | ||
$this->assertTrue($collection->valid()); | ||
|
||
unset($collection[0]); | ||
|
||
$this->assertFalse(isset($collection[0])); | ||
} | ||
} |
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
Oops, something went wrong.