1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP: Nelson Martell Library file |
5
|
|
|
* |
6
|
|
|
* Copyright © 2021 Nelson Martell (http://nelson6e65.github.io) |
7
|
|
|
* |
8
|
|
|
* Licensed under The MIT License (MIT) |
9
|
|
|
* For full copyright and license information, please see the LICENSE |
10
|
|
|
* Redistributions of files must retain the above copyright notice. |
11
|
|
|
* |
12
|
|
|
* @copyright 2021 Nelson Martell |
13
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
14
|
|
|
* @since 1.0.0 |
15
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
16
|
|
|
* */ |
17
|
|
|
|
18
|
|
|
declare(strict_types=1); |
19
|
|
|
|
20
|
|
|
namespace NelsonMartell\Extensions; |
21
|
|
|
|
22
|
|
|
use InvalidArgumentException; |
23
|
|
|
use NelsonMartell\IComparer; |
24
|
|
|
use NelsonMartell\StrictObject; |
25
|
|
|
|
26
|
|
|
use function NelsonMartell\msg; |
27
|
|
|
use function NelsonMartell\typeof; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Provides extension methods to handle class properties. |
31
|
|
|
* |
32
|
|
|
* @since 1.0.0 |
33
|
|
|
* @author Nelson Martell <[email protected]> |
34
|
|
|
* */ |
35
|
|
|
class PropertyExtension |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Ensures that a string is a valid property name. |
39
|
|
|
* |
40
|
|
|
* @param string $name Property name to be ensured. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
* @throws InvalidArgumentException if name do not follows the PHP variables naming convention. |
44
|
|
|
* |
45
|
|
|
* @see Text::ensureIsValidVarName() |
46
|
|
|
*/ |
47
|
145 |
|
public static function ensureIsValidName(string $name): string |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
145 |
|
return Text::ensureIsValidVarName($name); |
51
|
|
|
} catch (InvalidArgumentException $e) { |
52
|
|
|
$msg = msg('"{0}" is not a valid property name.', $name); |
53
|
|
|
throw new InvalidArgumentException($msg, 10, $e); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Ensures that the property is defined in the specified class. |
59
|
|
|
* |
60
|
|
|
* @param string $property |
61
|
|
|
* @param string $class |
62
|
|
|
* @param bool $includeMagic |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
* |
66
|
|
|
* @throws InvalidArgumentException if `$property` is not valid, `$class` do not exists or `$class` has not the |
67
|
|
|
* property. |
68
|
|
|
* |
69
|
|
|
* @see PropertyExtension::ensureIsValidName() |
70
|
|
|
*/ |
71
|
145 |
|
public static function ensureIsDefined(string $property, string $class, bool $includeMagic = false): string |
72
|
|
|
{ |
73
|
145 |
|
$type = typeof($class, true); |
74
|
|
|
|
75
|
145 |
|
if (!$type->hasProperty(static::ensureIsValidName($property), true, $includeMagic)) { |
76
|
26 |
|
$msg = msg( |
77
|
26 |
|
'"{property}" property is not defined in "{class}" class or parent classes' |
78
|
26 |
|
. ($includeMagic ? ' (including magic properties).' : '.'), |
79
|
26 |
|
compact('property', 'class') |
80
|
26 |
|
); |
81
|
|
|
|
82
|
26 |
|
throw new InvalidArgumentException($msg, 11); |
83
|
|
|
} |
84
|
|
|
|
85
|
119 |
|
return $property; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|