ListResponse::serializeObject()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 22
rs 9.4888
cc 5
nc 8
nop 0
1
<?php
2
3
/*
4
 * This file is part of the tmilos/scim-schema package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Tmilos\ScimSchema\Model;
13
14
abstract class ListResponse extends SchemaBase implements SerializableInterface
15
{
16
    /** @var int */
17
    protected $totalResults;
18
19
    /** @var int */
20
    protected $startIndex;
21
22
    /** @var int */
23
    protected $itemsPerPage;
24
25
    /** @var array|SerializableInterface[] */
26
    protected $resources;
27
28
    /**
29
     * @param array|SerializableInterface[] $resources
30
     * @param int                           $totalResults
31
     * @param int                           $startIndex
32
     * @param int                           $itemsPerPage
33
     */
34
    public function __construct(array $resources, $totalResults, $startIndex, $itemsPerPage)
35
    {
36
        $this->resources = $resources;
37
        $this->totalResults = $totalResults;
38
        $this->startIndex = $startIndex;
39
        $this->itemsPerPage = $itemsPerPage;
40
    }
41
42
    /**
43
     * @return int
44
     */
45
    public function getTotalResults()
46
    {
47
        return $this->totalResults;
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getStartIndex()
54
    {
55
        return $this->startIndex;
56
    }
57
58
    /**
59
     * @return int
60
     */
61
    public function getItemsPerPage()
62
    {
63
        return $this->itemsPerPage;
64
    }
65
66
    /**
67
     * @return array|SerializableInterface[]
68
     */
69
    public function getResources()
70
    {
71
        return $this->resources;
72
    }
73
74
    public function serializeObject()
75
    {
76
        $result = [
77
            'schemas' => $this->getSchemas(),
78
            'totalResults' => $this->totalResults,
79
            'Resources' => [],
80
        ];
81
        if ($this->startIndex) {
82
            $result['startIndex'] = $this->startIndex;
83
            $result['itemsPerPage'] = $this->itemsPerPage;
84
        }
85
86
        foreach ($this->resources as $resource) {
87
            if ($resource instanceof SerializableInterface) {
88
                $result['Resources'][] = $resource->serializeObject();
89
            } elseif (!is_array($resource)) {
90
                throw new \InvalidArgumentException('Resource must implement SerializableInterface or already be serialized to array');
91
            }
92
            $result['Resources'][] = $resource;
93
        }
94
95
        return $result;
96
    }
97
}
98