Issues (15)

src/ResponseBag.php (1 issue)

Checks if the types of the passed arguments in a function/method call are compatible.

Bug Minor
1
<?php
2
3
namespace ByJG\RestServer;
4
5
use ByJG\Serializer\SerializerObject;
6
use InvalidArgumentException;
7
8
class ResponseBag
9
{
10
11
    const AUTOMATIC = 0;
12
    const SINGLE_OBJECT = 1;
13
    const OBJECT_LIST = 2;
14
    const RAW = 3;
15
16
    protected $collection = [];
17
    protected $serializationRule = ResponseBag::AUTOMATIC;
18
19
    /**
20
     * @param string|mixed $object
21
     */
22 21
    public function add($object)
23
    {
24 21
        if (!is_string($object) && !is_numeric($object) && $this->serializationRule === ResponseBag::RAW) {
25 1
            throw new InvalidArgumentException("Raw data can be only string or numbers");
26
        }
27
28 20
        if (!is_object($object) && !is_array($object)) {
29 8
            $object = [ $object ];
30
        }
31
32 20
        if ($this->serializationRule !== ResponseBag::SINGLE_OBJECT && $this->serializationRule !== ResponseBag::RAW) {
33 15
            $this->collection[] = $object;
34 15
            return;
35
        }
36
37 5
        if (is_object($object)) {
38 1
            $object = [$object];
39
        }
40 5
        $this->collection = array_merge($this->collection, $object);
0 ignored issues
show
It seems like $object can also be of type object; however, parameter $arrays of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        $this->collection = array_merge($this->collection, /** @scrutinizer ignore-type */ $object);
Loading history...
41
    }
42
43
    /**
44
     * @param bool $buildNull
45
     * @param bool $onlyString
46
     * @return array
47
     */
48 25
    public function process($buildNull = true, $onlyString = false)
49
    {
50 25
        $collection = (array)$this->collection;
51 25
        if ($this->serializationRule === ResponseBag::RAW) {
52 3
            return implode("", $collection);
53
        }
54
55 22
        if (count($collection) === 1
56 22
            && $this->serializationRule !== ResponseBag::OBJECT_LIST && isset($collection[0])
57
        ) {
58 14
            $collection = $collection[0];
59
        }
60
        
61 22
        $object = SerializerObject::instance($collection)
62 22
            ->withOnlyString($onlyString);
63
64 22
        if (!$buildNull) {
65 1
            $object->withDoNotSerializeNull();
66
        }
67 22
        return $object->serialize();
68
    }
69
70
    public function getCollection()
71
    {
72
        return $this->collection;
73
    }
74
75 10
    public function setSerializationRule($value)
76
    {
77 10
        $this->serializationRule = $value;
78
    }
79
80 15
    public function getSerializationRule()
81
    {
82 15
        return $this->serializationRule;
83
    }
84
}
85