|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://flipboxfactory.com/software/hubspot/license |
|
6
|
|
|
* @link https://www.flipboxfactory.com/software/hubspot/ |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\hubspot\helpers; |
|
10
|
|
|
|
|
11
|
|
|
use flipbox\hubspot\HubSpot; |
|
12
|
|
|
use flipbox\hubspot\transformers\collections\TransformerCollection; |
|
13
|
|
|
use flipbox\hubspot\transformers\collections\TransformerCollectionInterface; |
|
14
|
|
|
use Flipbox\Skeleton\Helpers\ObjectHelper; |
|
15
|
|
|
use Flipbox\Transform\Transformers\TransformerInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author Flipbox Factory <[email protected]> |
|
19
|
|
|
* @since 1.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class TransformerHelper extends \Flipbox\Transform\Helpers\TransformerHelper |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param array|TransformerInterface|callable|null $transformer |
|
25
|
|
|
* @return TransformerInterface|callable|null |
|
26
|
|
|
* @throws \Flipbox\Skeleton\Exceptions\InvalidConfigurationException |
|
27
|
|
|
*/ |
|
28
|
|
|
public static function resolve($transformer = null) |
|
29
|
|
|
{ |
|
30
|
|
|
if (empty($transformer)) { |
|
31
|
|
|
return null; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (is_string($transformer) || is_array($transformer)) { |
|
35
|
|
|
return static::resolve(ObjectHelper::create($transformer)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return parent::resolve($transformer); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $transformer |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function isTransformerCollection($transformer): bool |
|
46
|
|
|
{ |
|
47
|
|
|
return $transformer instanceof TransformerCollectionInterface; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $transformer |
|
52
|
|
|
* @return bool |
|
53
|
|
|
*/ |
|
54
|
|
|
public static function isTransformerCollectionClass($transformer): bool |
|
55
|
|
|
{ |
|
56
|
|
|
return is_string($transformer) && is_subclass_of($transformer, TransformerCollectionInterface::class); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param TransformerCollectionInterface|array|string|null $transformer |
|
61
|
|
|
* @param string|null $default |
|
62
|
|
|
* @return TransformerCollectionInterface|null |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function resolveCollection($transformer = null, $default = TransformerCollection::class) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($transformer === false) { |
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if ($transformer === null && $default !== null) { |
|
71
|
|
|
$transformer = ['class' => $default]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (null !== ($collection = static::returnCollectionFromTransformer($transformer))) { |
|
75
|
|
|
return $collection; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (is_array($transformer)) { |
|
79
|
|
|
try { |
|
80
|
|
|
$class = ObjectHelper::checkConfig($transformer, TransformerCollectionInterface::class); |
|
81
|
|
|
|
|
82
|
|
|
/** @var TransformerCollectionInterface $collection */ |
|
83
|
|
|
$collection = new $class(); |
|
84
|
|
|
|
|
85
|
|
|
static::populateTransformerCollection( |
|
86
|
|
|
$collection, |
|
87
|
|
|
$transformer |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
return $collection; |
|
91
|
|
|
} catch (\Throwable $e) { |
|
92
|
|
|
HubSpot::warning(sprintf( |
|
93
|
|
|
"An exception was thrown while trying to resolve transformer collection: '%s'", |
|
94
|
|
|
(string)$e->getMessage() |
|
95
|
|
|
)); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param TransformerCollectionInterface|string $transformer |
|
104
|
|
|
* @return null|TransformerCollectionInterface |
|
105
|
|
|
*/ |
|
106
|
|
|
protected static function returnCollectionFromTransformer($transformer) |
|
107
|
|
|
{ |
|
108
|
|
|
if (static::isTransformerCollection($transformer)) { |
|
109
|
|
|
return $transformer; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
if (static::isTransformerCollectionClass($transformer)) { |
|
113
|
|
|
return new $transformer(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param TransformerCollectionInterface|null $collection |
|
122
|
|
|
* @param array $config |
|
123
|
|
|
* @return TransformerCollectionInterface|null |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function populateTransformerCollection( |
|
126
|
|
|
TransformerCollectionInterface $collection = null, |
|
127
|
|
|
array $config = [] |
|
128
|
|
|
) { |
|
129
|
|
|
if ($collection === null) { |
|
130
|
|
|
return $collection; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
foreach ($config as $name => $value) { |
|
134
|
|
|
$setter = 'set' . $name; |
|
135
|
|
|
if (method_exists($collection, $setter)) { |
|
136
|
|
|
$collection->$setter($value); |
|
137
|
|
|
continue; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (property_exists($collection, $name)) { |
|
141
|
|
|
$collection->{$name} = $value; |
|
142
|
|
|
continue; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $collection; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|