JsonApiObject   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 71
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setVersion() 0 4 1
A getVersion() 0 4 1
A toArray() 0 12 2
A __toString() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Document;
5
6
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataAwareInterface;
7
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataContainer;
8
9
/**
10
 * Json API info container
11
 *
12
 * @see http://jsonapi.org/format/#document-jsonapi-object
13
 *
14
 * @package Mikemirten\Component\JsonApi\Document
15
 */
16
class JsonApiObject implements MetadataAwareInterface
17
{
18
    use MetadataContainer;
19
20
    /**
21
     * Version of standard
22
     *
23
     * @var string
24
     */
25
    protected $version;
26
27
    /**
28
     * JsonApiObject constructor.
29
     *
30
     * @param string $version
31
     * @param array  $metadata
32
     */
33 18
    public function __construct(string $version = '1.0', array $metadata = [])
34
    {
35 18
        $this->version  = $version;
36 18
        $this->metadata = $metadata;
37 18
    }
38
39
    /**
40
     * Set version of standard
41
     *
42
     * @param string $version
43
     */
44 1
    public function setVersion(string $version)
45
    {
46 1
        $this->version = $version;
47 1
    }
48
49
    /**
50
     * Get version of standard
51
     *
52
     * @return string
53
     */
54 8
    public function getVersion(): string
55
    {
56 8
        return $this->version;
57
    }
58
59
    /**
60
     * Cast to an array
61
     *
62
     * @return array
63
     */
64 5
    public function toArray(): array
65
    {
66
        $data = [
67 5
            'version' => $this->getVersion()
68
        ];
69
70 5
        if ($this->hasMetadata()) {
71
            $data['meta'] = $this->getMetadata();
72
        }
73
74 5
        return $data;
75
    }
76
77
    /**
78
     * Cast to a string
79
     *
80
     * @return string
81
     */
82 3
    public function __toString(): string
83
    {
84 3
        return sprintf('JsonAPI-object of version "%s"', $this->version);
85
    }
86
}