MetadataContainer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 80
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setMetadataAttribute() 0 8 2
A hasMetadataAttribute() 0 4 1
A getMetadataAttribute() 0 8 2
A hasMetadata() 0 4 1
A getMetadata() 0 4 1
A removeMetadataAttribute() 0 4 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Document\Behaviour;
5
use Mikemirten\Component\JsonApi\Document\Exception\MetadataAttributeNotFoundException;
6
use Mikemirten\Component\JsonApi\Document\Exception\MetadataAttributeOverrideException;
7
8
/**
9
 * Metadata-container behaviour
10
 *
11
 * @see http://jsonapi.org/format/#document-meta
12
 *
13
 * @package Mikemirten\Component\JsonApi\Document\Behaviour
14
 */
15
trait MetadataContainer
16
{
17
    /**
18
     * Metadata attributes
19
     *
20
     * @var array
21
     */
22
    protected $metadata = [];
23
24
    /**
25
     * Set attribute of metadata
26
     *
27
     * @param string $name
28
     * @param $value
29
     */
30 38
    public function setMetadataAttribute(string $name, $value)
31
    {
32 38
        if (array_key_exists($name, $this->metadata)) {
33 13
            throw new MetadataAttributeOverrideException($this, $name);
34
        }
35
36 38
        $this->metadata[$name] = $value;
37 38
    }
38
39
    /**
40
     * Has attribute of metadata
41
     *
42
     * @param  string $name
43
     * @return bool
44
     */
45 26
    public function hasMetadataAttribute(string $name): bool
46
    {
47 26
        return array_key_exists($name, $this->metadata);
48
    }
49
50
    /**
51
     * Get attribute of metadata
52
     *
53
     * @param string $name
54
     * @return mixed
55
     */
56 26
    public function getMetadataAttribute(string $name)
57
    {
58 26
        if (array_key_exists($name, $this->metadata)) {
59 13
            return $this->metadata[$name];
60
        }
61
62 13
        throw new MetadataAttributeNotFoundException($this, $name);
63
    }
64
65
    /**
66
     * Contains any metadata ?
67
     *
68
     * @return bool
69
     */
70 18
    public function hasMetadata(): bool
71
    {
72 18
        return count($this->metadata) > 0;
73
    }
74
75
    /**
76
     * Get all attributes of metadata
77
     *
78
     * @return array
79
     */
80 52
    public function getMetadata(): array
81
    {
82 52
        return $this->metadata;
83
    }
84
85
    /**
86
     * Remove attribute of metadata
87
     *
88
     * @param string $name
89
     */
90 13
    public function removeMetadataAttribute(string $name)
91
    {
92 13
        unset($this->metadata[$name]);
93
    }
94
}