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\Model; |
16
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Traits\AllReadableTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Models a classname which has both a namespace and a classname |
21
|
|
|
*/ |
22
|
|
|
class ClassName |
23
|
|
|
{ |
24
|
|
|
use AllReadableTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $namespace = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $classname; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructs the ClassName by taking a fully qualified classname |
38
|
|
|
* and splitting it into its namespace and classname sections |
39
|
|
|
* |
40
|
|
|
* @param string $classname |
41
|
|
|
*/ |
42
|
41 |
|
public function __construct(string $classname) |
43
|
|
|
{ |
44
|
41 |
|
preg_match('/(.+)\\\\([a-z_A-Z0-9]+)/', $classname, $matches); |
45
|
|
|
|
46
|
41 |
|
if (!$matches) |
|
|
|
|
47
|
|
|
{ |
48
|
8 |
|
$this->classname = $classname; |
49
|
|
|
} |
50
|
|
|
else |
51
|
|
|
{ |
52
|
34 |
|
$this->classname = $matches[2]; |
53
|
34 |
|
$this->namespace = $matches[1]; |
54
|
|
|
} |
55
|
41 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the fully qualified classname |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
20 |
|
public function __toString() |
63
|
|
|
{ |
64
|
|
|
return |
65
|
20 |
|
($this->namespace ? $this->namespace . '\\' : '') |
66
|
20 |
|
. $this->classname; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Checks if this Classname is equal to another |
71
|
|
|
* |
72
|
|
|
* @param ClassName $object |
73
|
|
|
* @return boolean |
74
|
|
|
*/ |
75
|
3 |
|
public function equals(self $object) : bool |
76
|
|
|
{ |
77
|
|
|
return |
78
|
3 |
|
$object->classname === $this->classname && |
79
|
3 |
|
$object->namespace === $this->namespace; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.