1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* This file is part of the Type package. |
5
|
|
|
* For the full copyright information please view the LICENCE file that was |
6
|
|
|
* distributed with this package. |
7
|
|
|
* |
8
|
|
|
* @copyright Simon Deeley 2017 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace simondeeley\Helpers; |
12
|
|
|
|
13
|
|
|
use RuntimeException; |
14
|
|
|
use simondeeley\Type\Type; |
15
|
|
|
use simondeeley\Type\TypeEquality; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Helper methods for TypeEquality |
19
|
|
|
* |
20
|
|
|
* This trait provides methods which utilise the flags described in |
21
|
|
|
* {@link TypeEquality} that can be called from TypeEquality::equals to help |
22
|
|
|
* determine if two objects are equal. |
23
|
|
|
* |
24
|
|
|
* @uses Type |
25
|
|
|
* @uses TypeEquality |
26
|
|
|
* @author Simon Deeley <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
trait TypeEqualityHelperMethods |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Check against Type::getType |
32
|
|
|
* |
33
|
|
|
* Makes a comparison between two objects using the return values of |
34
|
|
|
* {@link Type::getType}. If both are equal then the method returns true, |
35
|
|
|
* otherwise false. Note that this method also returns true if the passed |
36
|
|
|
* flags are set to ignore this tye check. |
37
|
|
|
* |
38
|
|
|
* @param Type $type - the object to check against |
39
|
|
|
* @param int|null $flags - optional flags enable or disable certain checks |
40
|
|
|
* @return bool - Returns true if types are equal |
41
|
|
|
* @throws RuntimeException - if $this is not an instance of {@link Type} |
42
|
|
|
*/ |
43
|
|
|
final private function isSameTypeAs(Type $type, int $flags = null): bool |
44
|
|
|
{ |
45
|
|
|
if ($flags & TypeEquality::IGNORE_OBJECT_TYPE) { |
46
|
|
|
return true; // Ignore this check and just return true |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (false === $this instanceof Type) { |
50
|
|
|
throw new RuntimeException(sprintf( |
51
|
|
|
'Cannot compare type equality as %s does not implement %s', |
52
|
|
|
get_class($this), |
53
|
|
|
Type::class |
54
|
|
|
)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return ($this::getType() === $type::getType()) ? true : false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Check against an objects ID |
62
|
|
|
* |
63
|
|
|
* Makes a comparison against an objects identity, obtained through the use |
64
|
|
|
* of spl_object_hash. Method returns true if both objects point to the |
65
|
|
|
* same PHP reference. Note that this method also returns true when the $flag |
66
|
|
|
* is set to ignore this type of check. |
67
|
|
|
* |
68
|
|
|
* @param Type $type - the object to check against |
69
|
|
|
* @param int|null $flags - optional flags enable or disable certain checks |
70
|
|
|
* @return bool - Returns true if identities are equal |
71
|
|
|
*/ |
72
|
|
|
final private function isSameObjectAs(Type $type, int $flags = null): bool |
73
|
|
|
{ |
74
|
|
|
if ($flags & TypeEquality::IGNORE_OBJECT_IDENTITY) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return (spl_object_hash($this) === spl_object_hash($type)) ? true : false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|