Passed
Pull Request — master (#7)
by Pieter
04:15
created

PrimaryKeyReference::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
4
namespace W2w\Lib\Apie\Plugins\PrimaryKey\ValueObjects;
5
6
7
use W2w\Lib\Apie\Core\Models\ApiResourceClassMetadata;
8
use W2w\Lib\Apie\Interfaces\ValueObjectInterface;
9
10
/**
11
 * Value object for making a primary key reference to an Apie resource.
12
 */
13
final class PrimaryKeyReference
14
{
15
    /**
16
     * @var ApiResourceClassMetadata
17
     */
18
    private $metadata;
19
20
    /**
21
     * @var string
22
     */
23
    private $identifierValue;
24
25
    /**
26
     * @var string
27
     */
28
    private $resourceSlug;
29
30
    /**
31
     * @param ApiResourceClassMetadata $metadata
32
     * @param string|int|float|bool|object $identifierValue
33
     */
34
    public function __construct(ApiResourceClassMetadata $metadata, string $resourceSlug, $identifierValue)
35
    {
36
        $this->metadata = $metadata;
37
        $this->resourceSlug = $resourceSlug;
38
        if ($identifierValue instanceof ValueObjectInterface) {
39
            $this->identifierValue = (string) $identifierValue->toNative();
40
        } else {
41
            $this->identifierValue = (string) $identifierValue;
42
        }
43
    }
44
45
    /**
46
     * @return ApiResourceClassMetadata
47
     */
48
    public function getMetadata(): ApiResourceClassMetadata
49
    {
50
        return $this->metadata;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getUrl(): string
57
    {
58
        return $this->resourceSlug . '/' . $this->identifierValue;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function getIdentifierValue(): string
65
    {
66
        return $this->identifierValue;
67
    }
68
}
69