Completed
Push — master ( 402bf1...e7dba6 )
by Terry
03:00
created

AssertionFailedException::getLevel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Terah\Assert;
4
/**
5
 * Assert
6
 *
7
 * LICENSE
8
 *
9
 * This source file is subject to the new BSD license that is bundled
10
 * with this package in the file LICENSE.txt.
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so I can send you a copy immediately.
14
 */
15
class AssertionFailedException extends \Exception
16
{
17
    private $propertyPath;
18
    private $value;
19
    private $constraints;
20
    private $level;
21
22
    public function __construct($message, $code, $propertyPath = null, $value, array $constraints = [], $level='critical')
23
    {
24
        parent::__construct($message, $code);
25
        $this->propertyPath     = $propertyPath;
26
        $this->value            = $value;
27
        $this->constraints      = $constraints;
28
        $this->level            = $level;
29
    }
30
    /**
31
     * User controlled way to define a sub-property causing
32
     * the failure of a currently asserted objects.
33
     *
34
     * Useful to transport information about the nature of the error
35
     * back to higher layers.
36
     *
37
     * @return string
38
     */
39
    public function getPropertyPath()
40
    {
41
        $calling_location = $this->getCallingFileAndLine();
42
        return $this->propertyPath . ' in ' .$calling_location;
43
    }
44
45
    /**
46
     * @return null|string
47
     */
48
    public function getProperty()
49
    {
50
        return $this->propertyPath ? $this->propertyPath : 'General Error';
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getLevel()
57
    {
58
        return $this->level ? $this->level : 'critical';
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    protected function getCallingFileAndLine()
65
    {
66
        foreach ( $this->getTrace() as $trace )
67
        {
68
            $trace = (object)$trace;
69
            if ( empty($trace->file) )
70
            {
71
                continue;
72
            }
73
            $file = static::beforeLast('.php', static::afterLast('/', $trace->file));
74
            if ( in_array($file, ['AssertionChain', 'Assertion']) )
75
            {
76
                continue;
77
            }
78
            return "{$trace->file}:{$trace->line}";
79
        }
80
        return '';
81
    }
82
83
    /**
84
     * Get the value that caused the assertion to fail.
85
     *
86
     * @return mixed
87
     */
88
    public function getValue()
89
    {
90
        return $this->value;
91
    }
92
    /**
93
     * Get the constraints that applied to the failed assertion.
94
     *
95
     * @return array
96
     */
97
    public function getConstraints()
98
    {
99
        return $this->constraints;
100
    }
101
102
    public static function afterLast($needle, $haystack, $return_original=false)
103
    {
104
        if ( ! is_bool(static::strrevpos($haystack, $needle)) )
105
        {
106
            return mb_substr($haystack, static::strrevpos($haystack, $needle) + mb_strlen($needle));
107
        }
108
        return $return_original ? $haystack : '';
109
    }
110
111
    public static function strrevpos($string, $needle)
112
    {
113
        $revStr = mb_strpos(strrev($string), strrev($needle));
114
        return $revStr === false ? false : mb_strlen($string) - $revStr - mb_strlen($needle);
115
    }
116
117
    public static function beforeLast($needle, $haystack)
118
    {
119
        return mb_substr($haystack, 0, static::strrevpos($haystack, $needle));
120
    }
121
}
122