NestedAccessorException   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
c 1
b 0
f 0
dl 0
loc 129
rs 10
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createAsCannotGetValue() 0 9 1
A createAsCannotSetValue() 0 11 4
A createAsSourceIsScalar() 0 8 1
A _createAsCannotDeleteValue() 0 8 1
A _createAsUnknownSetMode() 0 8 1
A _createAsCannotSetValue() 0 8 1
A _createAsCannotAppendValue() 0 8 1
1
<?php
2
3
namespace Smoren\NestedAccessor\Exceptions;
4
5
use Smoren\ExtendedExceptions\BaseException;
6
use Smoren\NestedAccessor\Components\NestedAccessor;
7
8
/**
9
 * Class NestedAccessorException
10
 * @author Smoren <[email protected]>
11
 */
12
class NestedAccessorException extends BaseException
13
{
14
    public const SOURCE_IS_SCALAR = 1;
15
    public const CANNOT_GET_VALUE = 2;
16
    public const CANNOT_SET_VALUE = 3;
17
    public const CANNOT_APPEND_VALUE = 4;
18
    public const CANNOT_DELETE_VALUE = 5;
19
    public const UNKNOWN_SET_MODE = 6;
20
21
    /**
22
     * Creates a new exception instance for "source is scalar" error
23
     * @param mixed $source source
24
     * @return NestedAccessorException
25
     */
26
    public static function createAsSourceIsScalar($source): NestedAccessorException
27
    {
28
        return new NestedAccessorException(
29
            'source is scalar',
30
            NestedAccessorException::SOURCE_IS_SCALAR,
31
            null,
32
            [
33
                'source_type' => gettype($source),
34
            ]
35
        );
36
    }
37
38
    /**
39
     * Creates a new exception instance for "cannot get value" error
40
     * @param string $path path key
41
     * @param int $count errors count
42
     * @return NestedAccessorException
43
     */
44
    public static function createAsCannotGetValue(string $path, int $count): NestedAccessorException
45
    {
46
        return new NestedAccessorException(
47
            "cannot get value by path '{$path}'",
48
            NestedAccessorException::CANNOT_GET_VALUE,
49
            null,
50
            [
51
                'path' => $path,
52
                'count' => $count,
53
            ]
54
        );
55
    }
56
57
    /**
58
     * Creates a new exception instance for "cannot set|append|delete value" error
59
     * @param int $mode set mode
60
     * @param string $path path key
61
     * @return NestedAccessorException
62
     */
63
    public static function createAsCannotSetValue(int $mode, string $path): NestedAccessorException
64
    {
65
        switch($mode) {
66
            case NestedAccessor::SET_MODE_SET:
67
                return static::_createAsCannotSetValue($path);
68
            case NestedAccessor::SET_MODE_APPEND:
69
                return static::_createAsCannotAppendValue($path);
70
            case NestedAccessor::SET_MODE_DELETE:
71
                return static::_createAsCannotDeleteValue($path);
72
            default:
73
                return static::_createAsUnknownSetMode($mode);
74
        }
75
    }
76
77
    /**
78
     * Creates a new exception instance for "cannot set value" error
79
     * @param string $path path key
80
     * @return NestedAccessorException
81
     */
82
    protected static function _createAsCannotSetValue(string $path): NestedAccessorException
83
    {
84
        return new NestedAccessorException(
85
            "cannot set value by path '{$path}'",
86
            NestedAccessorException::CANNOT_SET_VALUE,
87
            null,
88
            [
89
                'path' => $path,
90
            ]
91
        );
92
    }
93
94
    /**
95
     * Creates a new exception instance for "cannot append value" error
96
     * @param string $path path key
97
     * @return NestedAccessorException
98
     */
99
    protected static function _createAsCannotAppendValue(string $path): NestedAccessorException
100
    {
101
        return new NestedAccessorException(
102
            "cannot append value by path '{$path}'",
103
            NestedAccessorException::CANNOT_APPEND_VALUE,
104
            null,
105
            [
106
                'path' => $path,
107
            ]
108
        );
109
    }
110
111
    /**
112
     * Creates a new exception instance for "cannot delete value" error
113
     * @param string $path path key
114
     * @return NestedAccessorException
115
     */
116
    protected static function _createAsCannotDeleteValue(string $path): NestedAccessorException
117
    {
118
        return new NestedAccessorException(
119
            "cannot delete value by path '{$path}'",
120
            NestedAccessorException::CANNOT_DELETE_VALUE,
121
            null,
122
            [
123
                'path' => $path,
124
            ]
125
        );
126
    }
127
128
    /**
129
     * Creates a new exception instance for "unknown set mode" error
130
     * @param int $mode set mode
131
     * @return NestedAccessorException
132
     */
133
    protected static function _createAsUnknownSetMode(int $mode): NestedAccessorException
134
    {
135
        return new NestedAccessorException(
136
            "unknown set mode",
137
            NestedAccessorException::UNKNOWN_SET_MODE,
138
            null,
139
            [
140
                'mode' => $mode,
141
            ]
142
        );
143
    }
144
}
145