|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSRestBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Util; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Stores map of values mapped to exception class |
|
16
|
|
|
* Resolves value by exception. |
|
17
|
|
|
* |
|
18
|
|
|
* @author Mikhail Shamin <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class ExceptionValueMap |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Map of values mapped to exception class |
|
24
|
|
|
* key => exception class |
|
25
|
|
|
* value => value associated with exception. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $map; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $map |
|
33
|
|
|
*/ |
|
34
|
15 |
|
public function __construct(array $map) |
|
35
|
|
|
{ |
|
36
|
15 |
|
$this->map = $map; |
|
37
|
15 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Resolves the value corresponding to an exception object. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Exception $exception |
|
43
|
|
|
* |
|
44
|
|
|
* @return mixed|false Value found or false is not found |
|
45
|
|
|
*/ |
|
46
|
12 |
|
public function resolveException(\Exception $exception) |
|
47
|
|
|
{ |
|
48
|
12 |
|
return $this->resolveThrowable($exception); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Resolves the value corresponding to an exception object. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \Throwable $exception |
|
55
|
|
|
* |
|
56
|
|
|
* @return mixed|false Value found or false is not found |
|
57
|
|
|
*/ |
|
58
|
12 |
|
public function resolveThrowable(\Throwable $exception) |
|
59
|
|
|
{ |
|
60
|
12 |
|
return $this->doResolveClass(get_class($exception)); |
|
61
|
9 |
|
} |
|
62
|
1 |
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Resolves the value corresponding to an exception class. |
|
65
|
9 |
|
* |
|
66
|
6 |
|
* @param string $class |
|
67
|
|
|
* |
|
68
|
10 |
|
* @return mixed|false if not found |
|
69
|
|
|
*/ |
|
70
|
10 |
|
private function doResolveClass($class) |
|
71
|
|
|
{ |
|
72
|
|
|
foreach ($this->map as $mapClass => $value) { |
|
73
|
|
|
if (!$value) { |
|
74
|
|
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($class === $mapClass || is_subclass_of($class, $mapClass)) { |
|
|
|
|
|
|
78
|
|
|
return $value; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|