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