|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mailxpert\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Mailxpert\Exceptions\MailxpertSDKException; |
|
6
|
|
|
use Mailxpert\MailxpertClient; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ModelFactory |
|
10
|
|
|
* @package Mailxpert\Model |
|
11
|
|
|
*/ |
|
12
|
|
|
class ModelFactory |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $endpoint |
|
16
|
|
|
* |
|
17
|
|
|
* @return string |
|
18
|
|
|
* @throws MailxpertSDKException |
|
19
|
|
|
*/ |
|
20
|
|
|
public static function getFactory($endpoint) |
|
21
|
|
|
{ |
|
22
|
|
|
$endpoint = static::cleanEndpoint($endpoint); |
|
23
|
|
|
|
|
24
|
|
|
switch ($endpoint) { |
|
25
|
|
|
case (preg_match('/^(\/|)contact_lists(\/{1}[\w\{\}]*|)$/', $endpoint) ? $endpoint : !$endpoint): |
|
26
|
|
|
return '\\Mailxpert\\Model\\ContactListFactory'; |
|
27
|
|
|
case (preg_match('/^(\/|)contacts(\/{1}[\w\{\}]*|)$/', $endpoint) ? $endpoint : !$endpoint): |
|
28
|
|
|
return '\\Mailxpert\\Model\\ContactFactory'; |
|
29
|
|
|
case (preg_match('/^(\/|)segments(\/{1}[\w\{\}]*|)$/', $endpoint) ? $endpoint : !$endpoint): |
|
30
|
|
|
return '\\Mailxpert\\Model\\SegmentFactory'; |
|
31
|
|
|
case (preg_match('/^(\/|)custom_fields(\/{1}[\w\{\}]*|)$/', $endpoint) ? $endpoint : !$endpoint): |
|
32
|
|
|
return '\\Mailxpert\\Model\\CustomFieldFactory'; |
|
33
|
|
|
case (preg_match( |
|
34
|
|
|
'/^(\/|)custom_fields(\/{1}[\w\{\}]*|)\/choices(\/{1}[\w\{\}]*|)$/', |
|
35
|
|
|
$endpoint |
|
36
|
|
|
) ? $endpoint : !$endpoint): |
|
37
|
|
|
return '\\Mailxpert\\Model\\CustomFieldChoiceFactory'; |
|
38
|
|
|
default: |
|
39
|
|
|
throw new MailxpertSDKException(sprintf('No model found for endpoint %s', $endpoint)); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $endpoint |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function cleanEndpoint($endpoint) |
|
49
|
|
|
{ |
|
50
|
|
|
if ((strpos($endpoint, 'http') === 0) && (strpos($endpoint, MailxpertClient::API_VERSION) !== false)) { |
|
51
|
|
|
$parts = explode('/', $endpoint); |
|
52
|
|
|
|
|
53
|
|
|
if (in_array(MailxpertClient::API_VERSION, $parts)) { |
|
54
|
|
|
while (in_array(MailxpertClient::API_VERSION, $parts)) { |
|
55
|
|
|
array_shift($parts); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return implode('/', $parts); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $endpoint; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $endpoint |
|
67
|
|
|
* @param mixed $data |
|
68
|
|
|
* |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
* @throws MailxpertSDKException |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function getNode($endpoint, $data) |
|
73
|
|
|
{ |
|
74
|
|
|
$factory = static::getFactory($endpoint); |
|
75
|
|
|
|
|
76
|
|
|
return call_user_func([$factory, 'parse'], $data); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|