|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpAbac\Manager; |
|
4
|
|
|
|
|
5
|
|
|
use PhpAbac\Comparison\ArrayComparison; |
|
6
|
|
|
use PhpAbac\Comparison\BooleanComparison; |
|
7
|
|
|
use PhpAbac\Comparison\DatetimeComparison; |
|
8
|
|
|
use PhpAbac\Comparison\NumericComparison; |
|
9
|
|
|
use PhpAbac\Comparison\StringComparison; |
|
10
|
|
|
|
|
11
|
|
|
class ComparisonManager { |
|
12
|
|
|
/** @var \PhpAbac\Manager\AttributeManager **/ |
|
13
|
|
|
protected $attributeManager; |
|
14
|
|
|
/** @var array **/ |
|
15
|
|
|
protected $comparisons = [ |
|
16
|
|
|
'array' => ArrayComparison::class, |
|
17
|
|
|
'boolean' => BooleanComparison::class, |
|
18
|
|
|
'datetime' => DatetimeComparison::class, |
|
19
|
|
|
'numeric' => NumericComparison::class, |
|
20
|
|
|
'string' => StringComparison::class, |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param \PhpAbac\Manager\AttributeManager $manager |
|
25
|
|
|
*/ |
|
26
|
16 |
|
public function __construct(AttributeManager $manager) { |
|
27
|
16 |
|
$this->attributeManager = $manager; |
|
28
|
16 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $type |
|
32
|
|
|
* @param string $method |
|
33
|
|
|
* @param mixed $expectedValue |
|
34
|
|
|
* @param mixed $value |
|
35
|
|
|
* @param array $extraData |
|
36
|
|
|
* @return bool |
|
37
|
|
|
*/ |
|
38
|
2 |
|
public function compare($type, $method, $expectedValue, $value, $extraData = []) { |
|
39
|
2 |
|
if(!isset($this->comparisons[$type])) { |
|
|
|
|
|
|
40
|
|
|
throw new \InvalidArgumentException('The requested comparison class does not exist'); |
|
41
|
|
|
} |
|
42
|
2 |
|
$comparison = new $this->comparisons[$type]($this); |
|
43
|
2 |
|
if(!method_exists($comparison, $method)) { |
|
|
|
|
|
|
44
|
|
|
throw new \InvalidArgumentException('The requested comparison method does not exist'); |
|
45
|
|
|
} |
|
46
|
2 |
|
return $comparison->{$method}($expectedValue, $value, $extraData); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $type |
|
51
|
|
|
* @param string $class |
|
52
|
|
|
*/ |
|
53
|
|
|
public function addComparison($type, $class) { |
|
54
|
|
|
$this->comparisons[$type] = $class; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return \PhpAbac\Manager\AttributeManager |
|
59
|
|
|
*/ |
|
60
|
2 |
|
public function getAttributeManager() { |
|
61
|
2 |
|
return $this->attributeManager; |
|
62
|
|
|
} |
|
63
|
|
|
} |