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`, `trait` and `interface` methods. |
31
|
|
|
* |
32
|
|
|
* @since 1.0.0 |
33
|
|
|
* @author Nelson Martell <[email protected]> |
34
|
|
|
* */ |
35
|
|
|
class MethodExtension |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Ensures that a string is a valid property name. |
39
|
|
|
* |
40
|
|
|
* @param string $name Method 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
|
119 |
|
public static function ensureIsValidName(string $name): string |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
119 |
|
return Text::ensureIsValidVarName($name); |
51
|
|
|
} catch (InvalidArgumentException $e) { |
52
|
|
|
$msg = msg('"{0}" is not a valid method name.', $name); |
53
|
|
|
throw new InvalidArgumentException($msg, 20, $e); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Ensures the method is defined in the specified class. |
59
|
|
|
* |
60
|
|
|
* @param string $method |
61
|
|
|
* @param string $class |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
* |
65
|
|
|
* @throws InvalidArgumentException if `$method` is not valid, `$class` do not exists or `$class` has not the |
66
|
|
|
* property. |
67
|
|
|
* |
68
|
|
|
* @see PropertyExtension::ensureIsValidName() |
69
|
|
|
*/ |
70
|
119 |
|
public static function ensureIsDefined(string $method, string $class): string |
71
|
|
|
{ |
72
|
119 |
|
$type = typeof($class, true); |
73
|
|
|
|
74
|
119 |
|
if (!$type->hasMethod(static::ensureIsValidName($method))) { |
75
|
25 |
|
$msg = msg( |
76
|
25 |
|
'"{method}" method do not exists in "{class}" class or parent classes.', |
77
|
25 |
|
compact('method', 'class') |
78
|
25 |
|
); |
79
|
|
|
|
80
|
25 |
|
throw new InvalidArgumentException($msg, 21); |
81
|
|
|
} |
82
|
|
|
|
83
|
100 |
|
return $method; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|