gwBoundsExceptionInfo::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 7
rs 9.4286
cc 1
eloc 5
nc 1
nop 4
1
<?php
2
3
namespace Gwa\Exception;
4
5
/**
6
 * An exception info container for out of bounds exceptions.
7
 */
8
class gwBoundsExceptionInfo extends gwCoreExceptionInfo implements gwiExceptionInfo
9
{
10
    /**
11
     * @var mixed
12
     */
13
    protected $value;
14
15
    /**
16
     * @var array|string
17
     */
18
    protected $values;
19
20
    /**
21
     * @var bool
22
     */
23
    protected $valuesareillegal;
24
25
    /**
26
     * @var string
27
     */
28
    protected $note;
29
30
    /**
31
     * Constructor.
32
     *
33
     * @param mixed        $value            the actual value that caused the exception
34
     * @param array|string $values           a list of legal values (or illegal values if $valuesareillegal is true)
35
     * @param bool         $valuesareillegal $values array contains illegal values
36
     * @param string       $note             an optional note
37
     */
38
    public function __construct($value, $values, $valuesareillegal = false, $note = '')
39
    {
40
        $this->value = $value;
41
        $this->values = $values;
42
        $this->valuesareillegal = $valuesareillegal;
43
        $this->note = $note;
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    public function fetch()
50
    {
51
        $value = $this->getAsString($this->value);
52
        $strvalues = [];
53
        if (is_array($this->values)) {
54
            foreach ($this->values as $v) {
55
                $strvalues[] = $this->getAsString($v);
56
            }
57
        } else {
58
            $strvalues[] = $this->getAsString($this->values);
59
        }
60
61
        $values = implode(', ', $strvalues);
62
63
        if (!$this->valuesareillegal) {
64
            $output = sprintf('%s MUST BE one of [%s]', $value, $values);
65
        } else {
66
            $output = sprintf('%s CANNOT BE one of [%s]', $value, $values);
67
        }
68
69
        if ($this->note) {
70
            $output .= ' ('.$this->note.')';
71
        }
72
73
        return $output;
74
    }
75
}
76