Base64Value::getEncoded()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Fxmlrpc\Serialization\Value;
4
5
/**
6
 * Value object representing Base64-encoded and raw string value.
7
 *
8
 * @author Lars Strojny <[email protected]>
9
 */
10
final class Base64Value implements Base64
11
{
12
    /**
13
     * @var string
14
     */
15
    private $encoded;
16
17
    /**
18
     * @var string
19
     */
20
    private $decoded;
21
22
    /**
23
     * @param string $encoded
24
     * @param string $decoded
25
     */
26 7
    public function __construct($encoded, $decoded)
27
    {
28 7
        $this->encoded = $encoded;
29 7
        $this->decoded = $decoded;
30 7
    }
31
32
    /**
33
     * Returns new base64 value object by encoded value.
34
     *
35
     * @param string $string
36
     *
37
     * @return self
38
     */
39 1
    public static function serialize($string)
40
    {
41 1
        return new self(null, $string);
42
    }
43
44
    /**
45
     * Returns new base64 value by string.
46
     *
47
     * @param string $value
48
     *
49
     * @return self
50
     */
51 2
    public static function deserialize($value)
52
    {
53 2
        return new self(trim($value), null);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 5
    public function getEncoded()
60
    {
61 5
        if (!isset($this->encoded)) {
62 2
            $this->encoded = base64_encode($this->decoded);
63 2
        }
64
65 5
        return $this->encoded;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 5
    public function getDecoded()
72
    {
73 5
        if (!isset($this->decoded)) {
74 3
            $this->decoded = base64_decode($this->encoded);
75 3
        }
76
77 5
        return $this->decoded;
78
    }
79
}
80