1
|
|
|
<?php namespace FreedomCore\TrinityCore\Support\Common; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Helper |
5
|
|
|
* @package FreedomCore\TrinityCore\Support\Common |
6
|
|
|
*/ |
7
|
|
|
class Helper |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Implementation of the recursive array search by key => $value |
12
|
|
|
* @param array $array |
13
|
|
|
* @param string $key |
14
|
|
|
* @param $value |
15
|
|
|
* @return array |
16
|
|
|
*/ |
17
|
|
|
public static function arrayMultiSearch(array $array, string $key, $value) : array |
18
|
|
|
{ |
19
|
|
|
$results = []; |
20
|
|
|
Helper::array_multi_search_base($array, $key, $value, $results); |
21
|
|
|
return $results; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get character name as in database |
26
|
|
|
* @param string $characterName |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public static function getCharacterName(string $characterName) : string |
30
|
|
|
{ |
31
|
|
|
return ucfirst(strtolower($characterName)); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Throw Runtime Exception |
36
|
|
|
* @param string $message |
37
|
|
|
* @throws \RuntimeException |
38
|
|
|
*/ |
39
|
|
|
public static function throwRuntimeException(string $message) |
40
|
|
|
{ |
41
|
|
|
$trace = debug_backtrace()[1]; |
42
|
|
|
$callParameters = [ |
43
|
|
|
'class' => substr(strrchr($trace['class'], "\\"), 1), |
44
|
|
|
'method' => $trace['function'] |
45
|
|
|
]; |
46
|
|
|
throw new \RuntimeException(sprintf('%s::%s error: %s', $callParameters['class'], $callParameters['method'], $message)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Low level implementation of the recursive array search by key => $value |
51
|
|
|
* @param array|integer $array |
52
|
|
|
* @param string $key |
53
|
|
|
* @param $value |
54
|
|
|
* @param array $results |
55
|
|
|
*/ |
56
|
|
|
private static function array_multi_search_base($array, string $key, $value, array &$results) |
57
|
|
|
{ |
58
|
|
|
if (!is_array($array)) { |
59
|
|
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (isset($array[$key]) && $array[$key] === $value) { |
63
|
|
|
$results[] = $array; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($array as $subArray) { |
67
|
|
|
Helper::array_multi_search_base($subArray, $key, $value, $results); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|