for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace ByJG\RestServer;
use ByJG\RestServer\Exception\HttpResponseException;
use ByJG\Serializer\SerializerObject;
class ResponseBag
{
const AUTOMATIC = 0;
const SINGLE_OBJECT = 1;
const ARRAY = 2;
protected $collection = [];
protected $serializationRule = ResponseBag::AUTOMATIC;
public function add($object)
if (is_string($object)) {
$object = [ $object ];
}
if (!is_object($object) && !is_array($object)) {
throw new HttpResponseException('You can add only object');
if ($this->serializationRule !== ResponseBag::SINGLE_OBJECT) {
$this->collection[] = $object;
return;
if (is_object($object)) {
$object = [$object];
$this->collection = array_merge($this->collection, $object);
/**
* @return array
*/
public function process()
$collection = (array)$this->collection;
if (count($collection) === 1 && $this->serializationRule !== ResponseBag::ARRAY && isset($collection[0])) {
$collection = $collection[0];
$object = new SerializerObject($collection);
return $object->build();
public function getCollection()
return $this->collection;
public function serializationRule($value)
$this->serializationRule = $value;