StringValue::isStringableObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * User: delboy1978uk
4
 * Date: 13/11/2016
5
 * Time: 13:26
6
 */
7
8
namespace Del\Common\Value;
9
10
use InvalidArgumentException;
11
12
class StringValue extends AbstractValue
13
{
14
    /** @var bool $stringable */
15
    private $stringable;
16
17
    /**
18
     * StringValue constructor.
19
     *
20
     * @param string $value
21
     */
22 1
    public function __construct($value)
23
    {
24 1
        if(!$this->canBeString($value)) {
25 1
            throw new InvalidArgumentException('You must supply a value.');
26
        }
27 1
        $this->value = (string) $value;
28 1
    }
29
30
    /**
31
     * @return string
32
     */
33 1
    public function getValue()
34
    {
35 1
        return parent::getValue();
36
    }
37
38
    /**
39
     * @param $value
40
     * @return bool
41
     */
42 1
    private function canBeString($value)
43
    {
44 1
        $this->stringable = false;
45 1
        $this->isStringableObject($value);
46 1
        $this->isNullValue($value);
47 1
        $this->isScalarValue($value);
48 1
        return $this->stringable;
49
    }
50
51
    /**
52
     * @param $value
53
     */
54 1
    private function isStringableObject($value)
55
    {
56 1
        $bool = is_object($value) && method_exists($value, '__toString');
57 1
        $this->updateStringable($bool);
58 1
    }
59
60
    /**
61
     * @param $value
62
     */
63 1
    private function isNullValue($value)
64
    {
65 1
        $bool = is_null($value);
66 1
        $this->updateStringable($bool);
67 1
    }
68
69
    /**
70
     * @param $value
71
     */
72 1
    private function isScalarValue($value)
73
    {
74 1
        $bool = is_scalar($value);
75 1
        $this->updateStringable($bool);
76 1
    }
77
78
    /**
79
     * @param $bool
80
     */
81 1
    private function updateStringable($bool)
82
    {
83 1
        if ($this->stringable === false) {
84 1
            $this->stringable = $bool;
85
        }
86
    }
87
}