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\craft\hubspot\validators; |
10
|
|
|
|
11
|
|
|
use flipbox\craft\hubspot\connections\SavableConnectionInterface; |
12
|
|
|
use flipbox\craft\hubspot\HubSpot; |
13
|
|
|
use flipbox\craft\hubspot\records\Connection; |
14
|
|
|
use Flipbox\HubSpot\Connections\ConnectionInterface; |
15
|
|
|
use Flipbox\HubSpot\Connections\IntegrationConnectionInterface; |
16
|
|
|
use yii\base\Model; |
17
|
|
|
use yii\validators\Validator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Flipbox Factory <[email protected]> |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class ConnectionValidator extends Validator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @inheritdoc |
27
|
|
|
*/ |
28
|
|
|
public function validateAttribute($model, $attribute) |
29
|
|
|
{ |
30
|
|
|
$class = $model->$attribute; |
31
|
|
|
|
32
|
|
|
// Handles are always required, so if it's blank, the required validator will catch this. |
33
|
|
|
if ($class) { |
34
|
|
|
if (!$this->isValid($class)) { |
35
|
|
|
$message = HubSpot::t( |
36
|
|
|
'“{class}” is a not a valid connection.', |
37
|
|
|
['class' => $class] |
38
|
|
|
); |
39
|
|
|
$this->addError($model, $attribute, $message); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$this->validateConnection($model, $class, $attribute); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param string $class |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
protected function isValid(string $class): bool |
51
|
|
|
{ |
52
|
|
|
return $class instanceof ConnectionInterface || |
53
|
|
|
$class instanceof IntegrationConnectionInterface || |
54
|
|
|
is_subclass_of($class, ConnectionInterface::class) || |
55
|
|
|
is_subclass_of($class, IntegrationConnectionInterface::class); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param Model $model |
60
|
|
|
* @param string $class |
61
|
|
|
* @param $attribute |
62
|
|
|
*/ |
63
|
|
|
protected function validateConnection(Model $model, string $class, $attribute) |
64
|
|
|
{ |
65
|
|
|
if (!$model instanceof Connection) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @var ConnectionInterface $connection */ |
70
|
|
|
$connection = $model->getConnection(); |
71
|
|
|
|
72
|
|
|
if (!$connection instanceof SavableConnectionInterface) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!$connection->validate()) { |
77
|
|
|
$message = HubSpot::t( |
78
|
|
|
'Invalid settings.', |
79
|
|
|
['class' => $class] |
80
|
|
|
); |
81
|
|
|
$this->addError($model, $attribute, $message); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|