SingleResourceDocument::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 5
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 5
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Document;
5
6
/**
7
 * Single resource document
8
 *
9
 * @see http://jsonapi.org/format/#document-structure
10
 *
11
 * @package Mikemirten\Component\JsonApi
12
 */
13 View Code Duplication
class SingleResourceDocument extends AbstractDocument
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
14
{
15
    /**
16
     * Resource
17
     *
18
     * @var ResourceObject
19
     */
20
    protected $resource;
21
22
    /**
23
     * SingleIdentifierDocument constructor.
24
     *
25
     * @param ResourceObject $resource
26
     * @param array          $metadata
27
     */
28 20
    public function __construct(ResourceObject $resource, array $metadata = [])
29
    {
30 20
        $this->resource = $resource;
31 20
        $this->metadata = $metadata;
32 20
    }
33
34
    /**
35
     * Get resource
36
     *
37
     * @return ResourceObject
38
     */
39 8
    public function getResource(): ResourceObject
40
    {
41 8
        return $this->resource;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 6
    public function toArray(): array
48
    {
49 6
        $data = parent::toArray();
50
51 6
        $data['data'] = $this->getResource()->toArray();
52
53 6
        return $data;
54
    }
55
56
    /**
57
     * Cast to a string
58
     *
59
     * @return string
60
     */
61 5
    public function __toString(): string
62
    {
63 5
        return sprintf('Document contains [%s]', $this->resource);
64
    }
65
}