|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Generalization over Omnipay and Payum |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/php-merchant |
|
6
|
|
|
* @package php-merchant |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hiqdev\php\merchant; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Ideas taken from Yii. |
|
15
|
|
|
*/ |
|
16
|
|
|
class Helper |
|
17
|
|
|
{ |
|
18
|
1 |
|
public static function configure($object, array $config) |
|
19
|
|
|
{ |
|
20
|
1 |
|
foreach ($config as $name => $value) { |
|
21
|
1 |
|
$object->$name = $value; |
|
22
|
1 |
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
return $object; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Very straightforward object creator. |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public static function createObject(array $config) |
|
31
|
|
|
{ |
|
32
|
1 |
|
$object = new $config['class'](); |
|
33
|
1 |
|
unset($config['class']); |
|
34
|
1 |
|
static::configure($object, $config); |
|
35
|
|
|
|
|
36
|
1 |
|
return $object; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Creates merchant according to given config. |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public static function create(array $config) |
|
43
|
|
|
{ |
|
44
|
1 |
|
return static::createObject(array_merge([ |
|
45
|
1 |
|
'data' => $config, |
|
46
|
1 |
|
'class' => static::findClass($config['gateway'], $config['library'], 'Merchant'), |
|
47
|
1 |
|
], $config)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public static function findClass($gateway, $library, $what) |
|
51
|
|
|
{ |
|
52
|
1 |
|
$library = $library ?: 'Omnipay'; |
|
53
|
1 |
|
$class = static::buildClass($gateway, $library, $what); |
|
54
|
|
|
|
|
55
|
1 |
|
return class_exists($class) ? $class : static::buildClass(null, $library, $what); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Builds class name: hiqdev\php\merchant\gateway\LibraryWhat. |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public static function buildClass($gateway, $library, $what) |
|
62
|
|
|
{ |
|
63
|
1 |
|
$gateway = $gateway ? static::simplify($gateway) . '\\' : ''; |
|
64
|
1 |
|
return 'hiqdev\php\merchant\\' . $gateway . $library . $what; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Converts an ID into a CamelCase name. |
|
69
|
|
|
* Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name. |
|
70
|
|
|
* For example, 'post-tag' is converted to 'PostTag'. |
|
71
|
|
|
* Taken from Yii 2 Inflector. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $id the ID to be converted |
|
74
|
|
|
* @param string $separator the character used to separate the words in the ID |
|
75
|
|
|
* |
|
76
|
|
|
* @return string the resulting CamelCase name |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public static function id2camel($id, $separator = '-') |
|
79
|
|
|
{ |
|
80
|
2 |
|
return str_replace(' ', '', ucwords(implode(' ', explode($separator, $id)))); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Converts to simple name - only lowercase letters and numbers. |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public static function simplify($name) |
|
87
|
|
|
{ |
|
88
|
2 |
|
return preg_replace('/[^a-z0-9]+/', '', strtolower($name)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Converts time to ISO 8601 format. |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function isotime($time) |
|
95
|
|
|
{ |
|
96
|
|
|
return date('c', strtotime($time)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
1 |
|
public static function formatMoney($sum, $decimals = 2) |
|
100
|
|
|
{ |
|
101
|
1 |
|
return number_format($sum, $decimals, '.', ''); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|