|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Composite Utils package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the |
|
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @package spaark/composite-utils |
|
11
|
|
|
* @author Emily Shepherd <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Service; |
|
16
|
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType; |
|
18
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType; |
|
19
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\AbstractType; |
|
20
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\ScalarType; |
|
21
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\NullType; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Compares two AbstractTypes to check if they are compatible |
|
25
|
|
|
*/ |
|
26
|
|
|
class TypeComparator |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Compares two AbstractTypes, ensuring they are compatible |
|
30
|
|
|
* |
|
31
|
|
|
* @param AbstractType $parent |
|
32
|
|
|
* @param AbstractType $child |
|
33
|
|
|
* @return boolean |
|
34
|
|
|
*/ |
|
35
|
31 |
|
public function compatible |
|
36
|
|
|
( |
|
37
|
|
|
AbstractType $parent, |
|
38
|
|
|
AbstractType $child |
|
39
|
|
|
) |
|
40
|
|
|
: bool |
|
41
|
|
|
{ |
|
42
|
|
|
if |
|
43
|
|
|
( |
|
44
|
31 |
|
($child instanceof NullType && $parent->nullable) || |
|
45
|
31 |
|
($parent instanceof MixedType) |
|
46
|
|
|
) |
|
47
|
|
|
{ |
|
48
|
23 |
|
return true; |
|
49
|
|
|
} |
|
50
|
15 |
|
elseif ($parent instanceof ScalarType) |
|
51
|
|
|
{ |
|
52
|
10 |
|
return get_class($parent) === get_class($child); |
|
53
|
|
|
} |
|
54
|
8 |
|
elseif ($parent instanceof ObjectType) |
|
55
|
|
|
{ |
|
56
|
|
|
if |
|
57
|
|
|
( |
|
58
|
7 |
|
$child instanceof ObjectType && |
|
59
|
6 |
|
is_a |
|
60
|
|
|
( |
|
61
|
6 |
|
$child->classname->__toString(), |
|
62
|
6 |
|
$parent->classname->__toString(), |
|
63
|
7 |
|
true |
|
64
|
|
|
) |
|
65
|
|
|
) |
|
66
|
|
|
{ |
|
67
|
6 |
|
return true; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
throw new \DomainException |
|
74
|
|
|
( |
|
75
|
1 |
|
'Unknown type: ' . get_class($parent) |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|