ResponseBag   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 16 5
A process() 0 15 4
A getCollection() 0 4 1
A serializationRule() 0 4 1
1
<?php
2
3
namespace ByJG\RestServer;
4
5
use ByJG\Serializer\SerializerObject;
6
7
class ResponseBag
8
{
9
10
    const AUTOMATIC = 0;
11
    const SINGLE_OBJECT = 1;
12
    const OBJECT_LIST = 2;
13
14
    protected $collection = [];
15
    protected $serializationRule = ResponseBag::AUTOMATIC;
16
17
    /**
18
     * @param string|mixed $object
19
     */
20
    public function add($object)
21
    {
22
        if (!is_object($object) && !is_array($object)) {
23
            $object = [ $object ];
24
        }
25
26
        if ($this->serializationRule !== ResponseBag::SINGLE_OBJECT) {
27
            $this->collection[] = $object;
28
            return;
29
        }
30
31
        if (is_object($object)) {
32
            $object = [$object];
33
        }
34
        $this->collection = array_merge($this->collection, $object);
35
    }
36
37
    /**
38
     * @param bool $buildNull
39
     * @param bool $onlyString
40
     * @return array
41
     */
42
    public function process($buildNull = true, $onlyString = false)
43
    {
44
        $collection = (array)$this->collection;
45
        if (count($collection) === 1
46
            && $this->serializationRule !== ResponseBag::OBJECT_LIST && isset($collection[0])
47
        ) {
48
            $collection = $collection[0];
49
        }
50
        
51
        $object = new SerializerObject($collection);
52
        return $object
53
            ->setOnlyString($onlyString)
54
            ->setBuildNull($buildNull)
55
            ->build();
56
    }
57
58
    public function getCollection()
59
    {
60
        return $this->collection;
61
    }
62
63
    public function serializationRule($value)
64
    {
65
        $this->serializationRule = $value;
66
    }
67
}
68