Claim::getValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Tymon\JWTAuth\Claims;
4
5
use Tymon\JWTAuth\Exceptions\InvalidClaimException;
6
7
abstract class Claim implements ClaimInterface
8
{
9
    /**
10
     * The claim name
11
     *
12
     * @var string
13
     */
14
    protected $name;
15
16
    /**
17
     * The claim value
18
     *
19
     * @var mixed
20
     */
21
    private $value;
22
23
    /**
24
     * @param mixed  $value
25
     */
26 66
    public function __construct($value)
27
    {
28 66
        $this->setValue($value);
29 66
    }
30
31
    /**
32
     * Set the claim value, and call a validate method if available
33
     *
34
     * @param $value
35
     * @throws \Tymon\JWTAuth\Exceptions\InvalidClaimException
36
     * @return $this
37
     */
38 66
    public function setValue($value)
39
    {
40 66
        if (! $this->validate($value)) {
41
            throw new InvalidClaimException('Invalid value provided for claim "' . $this->getName() . '": ' . $value);
42
        }
43
44 66
        $this->value = $value;
45
46 66
        return $this;
47
    }
48
49
    /**
50
     * Get the claim value
51
     *
52
     * @return mixed
53
     */
54 66
    public function getValue()
55
    {
56 66
        return $this->value;
57
    }
58
59
    /**
60
     * Set the claim name
61
     *
62
     * @param string $name
63
     * @return $this
64
     */
65 6
    public function setName($name)
66
    {
67 6
        $this->name = $name;
68
69 6
        return $this;
70
    }
71
72
    /**
73
     * Get the claim name
74
     *
75
     * @return string
76
     */
77 66
    public function getName()
78
    {
79 66
        return $this->name;
80
    }
81
82
    /**
83
     * Validate the Claim value
84
     *
85
     * @param  $value
86
     * @return boolean
87
     */
88 66
    protected function validate($value)
89
    {
90 66
        return true;
91
    }
92
93
    /**
94
     * Build a key value array comprising of the claim name and value
95
     *
96
     * @return array
97
     */
98
    public function toArray()
99
    {
100
        return [$this->getName() => $this->getValue()];
101
    }
102
103
    /**
104
     * Get the claim as a string
105
     *
106
     * @return string
107
     */
108
    public function __toString()
109
    {
110
        return json_encode($this->toArray(), JSON_UNESCAPED_SLASHES);
111
    }
112
}
113