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\criteria\element; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\base\ElementInterface; |
13
|
|
|
use craft\errors\ElementNotFoundException; |
14
|
|
|
use craft\errors\FieldNotFoundException; |
15
|
|
|
use flipbox\hubspot\criteria\ObjectAccessorInterface; |
16
|
|
|
use flipbox\hubspot\criteria\traits\CacheTrait; |
17
|
|
|
use flipbox\hubspot\criteria\traits\ConnectionTrait; |
18
|
|
|
use flipbox\hubspot\criteria\traits\TransformerCollectionTrait; |
19
|
|
|
use flipbox\hubspot\fields\Objects; |
20
|
|
|
use flipbox\hubspot\traits\TransformElementIdTrait; |
21
|
|
|
use yii\base\BaseObject; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Flipbox Factory <[email protected]> |
25
|
|
|
* @since 1.0.0 |
26
|
|
|
*/ |
27
|
|
|
class ObjectFromElementAccessor extends BaseObject implements ObjectAccessorInterface |
28
|
|
|
{ |
29
|
|
|
use TransformElementIdTrait, |
30
|
|
|
TransformerCollectionTrait, |
31
|
|
|
ConnectionTrait, |
32
|
|
|
CacheTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var ElementInterface |
36
|
|
|
*/ |
37
|
|
|
public $element; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Objects |
41
|
|
|
*/ |
42
|
|
|
public $field; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return ElementInterface |
46
|
|
|
* @throws ElementNotFoundException |
47
|
|
|
*/ |
48
|
|
|
protected function getElement(): ElementInterface |
49
|
|
|
{ |
50
|
|
|
if ($this->element instanceof ElementInterface) { |
51
|
|
|
return $this->element; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (is_numeric($this->element)) { |
55
|
|
|
return $this->element = Craft::$app->getElements()->getElementById($this->element); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (is_string($this->element)) { |
59
|
|
|
return $this->element = Craft::$app->getElements()->getElementByUri($this->element); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
throw new ElementNotFoundException("Unable to resolve element."); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return Objects |
67
|
|
|
* @throws FieldNotFoundException |
68
|
|
|
*/ |
69
|
|
|
protected function getField(): Objects |
70
|
|
|
{ |
71
|
|
|
if ($this->field instanceof Objects) { |
72
|
|
|
return $this->field; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$field = null; |
76
|
|
|
if (is_numeric($this->field)) { |
77
|
|
|
$field = Craft::$app->getFields()->getFieldById($this->field); |
78
|
|
|
} elseif (is_string($this->element)) { |
79
|
|
|
$field = Craft::$app->getFields()->getFieldByHandle($this->field); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($field instanceof Objects) { |
83
|
|
|
return $this->field = $field; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
throw new FieldNotFoundException("Unable to resolve field."); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritdoc |
91
|
|
|
* @throws FieldNotFoundException |
92
|
|
|
* @throws ElementNotFoundException |
93
|
|
|
*/ |
94
|
|
|
public function getId(): string |
95
|
|
|
{ |
96
|
|
|
return $this->transformElementId( |
97
|
|
|
$this->getElement(), |
98
|
|
|
$this->getField() |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|