|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Shared |
|
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
|
10
|
|
|
* @license http://mit-license.org/ MIT License |
|
11
|
|
|
* @link http://www.phossa.com/ |
|
12
|
|
|
*/ |
|
13
|
|
|
/*# declare(strict_types=1); */ |
|
14
|
|
|
|
|
15
|
|
|
namespace Phossa2\Shared\Base; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Shared\Message\Message; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ClassNameTrait |
|
21
|
|
|
* |
|
22
|
|
|
* - Implementation of ClassNameInterface. |
|
23
|
|
|
* - Provides PHP 5.5 ::class feature for classes using this trait |
|
24
|
|
|
* - methods are final to prevent accidental overriden in child class |
|
25
|
|
|
* |
|
26
|
|
|
* @package Phossa2\Shared |
|
27
|
|
|
* @author Hong Zhang <[email protected]> |
|
28
|
|
|
* @version 2.0.0 |
|
29
|
|
|
* @since 2.0.0 added |
|
30
|
|
|
* @since 2.0.24 added setProperties() |
|
31
|
|
|
* @since 2.0.29 modified getShortName(), getNamespace() parameters |
|
32
|
|
|
*/ |
|
33
|
|
|
trait ClassNameTrait |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Returns fully qualified class name |
|
37
|
|
|
* |
|
38
|
|
|
* @return string |
|
39
|
|
|
* @access public |
|
40
|
|
|
* @final |
|
41
|
|
|
* @api |
|
42
|
|
|
*/ |
|
43
|
|
|
final public static function getClassName()/*# : string */ |
|
44
|
|
|
{ |
|
45
|
|
|
return get_called_class(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns class name without namespace |
|
50
|
|
|
* |
|
51
|
|
|
* @param string|object $className optional classname |
|
52
|
|
|
* @return string |
|
53
|
|
|
* @access public |
|
54
|
|
|
* @since 2.0.29 add $className parameter |
|
55
|
|
|
* @final |
|
56
|
|
|
* @api |
|
57
|
|
|
*/ |
|
58
|
|
|
final public static function getShortName( |
|
59
|
|
|
$className = '' |
|
60
|
|
|
)/*# : string */ { |
|
61
|
|
|
$base = strrchr(static::getRealClassName($className), '\\'); |
|
62
|
|
|
return $base ? substr($base, 1) : $className; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Returns namespace of current class |
|
67
|
|
|
* |
|
68
|
|
|
* @param string|object $className optional classname |
|
69
|
|
|
* @return string |
|
70
|
|
|
* @access public |
|
71
|
|
|
* @since 2.0.29 add $className parameter |
|
72
|
|
|
* @final |
|
73
|
|
|
* @api |
|
74
|
|
|
*/ |
|
75
|
|
|
final public static function getNameSpace( |
|
76
|
|
|
/*# string */ $className = '' |
|
77
|
|
|
)/*# : string */ { |
|
78
|
|
|
$class = static::getRealClassName($className); |
|
79
|
|
|
return substr($class, 0, strrpos($class, '\\')); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Set object properties |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $properties |
|
86
|
|
|
* @access public |
|
87
|
|
|
* @since 2.0.24 added |
|
88
|
|
|
* @api |
|
89
|
|
|
*/ |
|
90
|
|
|
final public function setProperties(array $properties = []) |
|
91
|
|
|
{ |
|
92
|
|
|
foreach ($properties as $name => $value) { |
|
93
|
|
|
if (property_exists($this, $name)) { |
|
94
|
|
|
$this->$name = $value; |
|
95
|
|
|
} else { |
|
96
|
|
|
trigger_error( |
|
97
|
|
|
Message::get( |
|
98
|
|
|
Message::MSG_PROPERTY_UNKNOWN, |
|
99
|
|
|
$name, |
|
100
|
|
|
get_class($this) |
|
101
|
|
|
), |
|
102
|
|
|
E_USER_WARNING |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the classname |
|
110
|
|
|
* |
|
111
|
|
|
* @param string|object $className |
|
112
|
|
|
* @return string the class name |
|
113
|
|
|
* @access protected |
|
114
|
|
|
*/ |
|
115
|
|
|
protected static function getRealClassName($className)/*# : string */ |
|
116
|
|
|
{ |
|
117
|
|
|
if (is_object($className)) { |
|
118
|
|
|
$class = get_class($className); |
|
119
|
|
|
} else { |
|
120
|
|
|
$class = $className ?: get_called_class(); |
|
121
|
|
|
} |
|
122
|
|
|
return $class; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|