Passed
Push — master ( e9bd9f...92cbc5 )
by Michael
03:05
created

AbstractDocument::toArray()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4.0218

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 9
cp 0.8889
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 8
nop 0
crap 4.0218
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Document;
5
6
use Mikemirten\Component\JsonApi\Document\Behaviour\ErrorsContainer;
7
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksAwareInterface;
8
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksContainer;
9
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataAwareInterface;
10
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataContainer;
11
12
/**
13
 * Abstract Document
14
 *
15
 * @see http://jsonapi.org/format/#document-structure
16
 *
17
 * Represents base JsonAPI-document structure
18
 * Supposed to be extended by case-based documents
19
 *
20
 * @package Mikemirten\Component\JsonApi\Document
21
 */
22
abstract class AbstractDocument implements MetadataAwareInterface, LinksAwareInterface
23
{
24
    use MetadataContainer;
25
    use LinksContainer;
26
    use ErrorsContainer;
27
28
    /**
29
     * Cast to an array
30
     *
31
     * @return array
32
     */
33 11
    public function toArray(): array
34
    {
35 11
        $data = [];
36
37 11
        if ($this->hasMetadata()) {
38 4
            $data['meta'] = $this->getMetadata();
39
        }
40
41 11
        if ($this->hasLinks()) {
42 4
            $data['links'] = $this->linksToArray();
43
        }
44
45 11
        if ($this->hasErrors()) {
46
            $data['errors'] = $this->errorsToArray();
47
        }
48
49 11
        return $data;
50
    }
51
}