-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHelper.php
121 lines (108 loc) · 2.61 KB
/
Helper.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace Boxberry\Common;
class Helper
{
/**
* Инициализация парамметров
*
* @param $target
* @param array|null $parameters
*/
public static function initialize($target, array $parameters = null)
{
if ($parameters) {
foreach ($parameters as $key => $value) {
$method = 'set' . ucfirst(static::camelCase($key));
if (method_exists($target, $method)) {
$target->$method($value);
}
}
}
}
/**
* @param $str
*
* @internal
* @return null|string|string[]
*/
protected static function camelCase($str)
{
$str = self::convertToLowercase($str);
return preg_replace_callback(
'/_([a-z])/',
function ($match) {
return strtoupper($match[1]);
},
$str
);
}
/**
* @internal
*
* @param $str
*
* @return string
*/
protected static function convertToLowercase($str)
{
$explodedStr = explode('_', $str);
$lowercasedStr = [];
if (count($explodedStr) > 1) {
foreach ($explodedStr as $value) {
$lowercasedStr[] = strtolower($value);
}
$str = implode('_', $lowercasedStr);
}
return $str;
}
/**
* @internal
*
* @param $array
*
* @return array
*/
public static function filterEmpty($array)
{
return array_filter(
$array,
function ($val, $key) {
return $key && $val;
},
ARRAY_FILTER_USE_BOTH
);
}
/**
* @internal
*
* @param $className
*
* @return string
*/
public static function getDeliveryShortName($className)
{
if (0 === strpos($className, '\\')) {
$className = substr($className, 1);
}
if (0 === strpos($className, 'Boxberry\\')) {
return trim(str_replace('\\', '_', substr($className, 8, -7)), '_');
}
return '\\' . $className . '\\Delivery';
}
/**
* @param string $shortName
*
* @return string
*/
public static function getDeliveryClassName($shortName)
{
if (0 === strpos($shortName, '\\')) {
return $shortName;
}
$shortName = str_replace('_', '\\', $shortName);
if (false === strpos($shortName, '\\')) {
$shortName .= '\\';
}
return '\\Boxberry\\' . $shortName . 'Delivery';
}
}