-
Notifications
You must be signed in to change notification settings - Fork 0
/
MagicTrait.php
57 lines (48 loc) · 1.24 KB
/
MagicTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
declare(strict_types=1);
namespace Horat1us\Environment;
/**
* Class MagicTrait
* @package Horat1us\Environment
*/
trait MagicTrait
{
use ConfigTrait;
/**
* @param string $callerName
* @return string
*/
private function getEnvironmentKeySuffix(string $callerName): string
{
if (strlen($callerName) < 3
|| strpos($callerName, "get") !== 0
) {
throw new \RuntimeException("Trait method must be named as get{getEnvironmentKeySuffix}");
}
$methodName = substr($callerName, 3, strlen($callerName) - 3);
// map camelCase string to CAMEL_CASE
$suffix = strtoupper(
substr(
preg_replace('/([A-Z])/', '_$1', $methodName),
1,
strlen($methodName) * 2
)
);
return $suffix;
}
/**
* @throws Exception\Missing
* @return array|false|mixed|null|string
*/
private function getEnvironment()
{
$fieldName = $this->getEnvironmentKeySuffix(
$this->getCalledMethod()
);
return $this->getEnv($fieldName);
}
private function getCalledMethod(): string
{
return debug_backtrace()[1]['function'];
}
}