Passed
Push — master ( 146f63...0d342d )
by Michael
02:44
created

ResourceIdentifierObject::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 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
use Mikemirten\Component\JsonApi\Document\Behaviour\ResourceBehaviour;
9
10
/**
11
 * Resource Identifier Object
12
 *
13
 * @see http://jsonapi.org/format/#document-resource-identifier-objects
14
 *
15
 * @package Mikemirten\Component\JsonApi\Document
16
 */
17
class ResourceIdentifierObject implements MetadataAwareInterface
18
{
19
    use ResourceBehaviour;
20
    use MetadataContainer;
21
22
    /**
23
     * ResourceObject constructor.
24
     *
25
     * @param string $id
26
     * @param string $type
27
     * @param array  $metadata
28
     */
29 10
    public function __construct(string $id, string $type, array $metadata = [])
30
    {
31 10
        $this->id       = $id;
32 10
        $this->type     = $type;
33 10
        $this->metadata = $metadata;
34 10
    }
35
36
    /**
37
     * Cast to an array
38
     *
39
     * @return array
40
     */
41 2
    public function toArray(): array
42
    {
43 2
        $data = $this->resourceToArray();
44
45 2
        if ($this->hasMetadata()) {
46 1
            $data['meta'] = $this->getMetadata();
47
        }
48
49 2
        return $data;
50
    }
51
52
    /**
53
     * Cast to a string
54
     *
55
     * @return string
56
     */
57 3
    public function __toString(): string
58
    {
59 3
        return sprintf('Identifier of resource with ID: "%s" and type: "%s"', $this->id, $this->type);
60
    }
61
}