-
Notifications
You must be signed in to change notification settings - Fork 7
/
Address.php
171 lines (152 loc) · 4.75 KB
/
Address.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @link https://sprout.barrelstrengthdesign.com
* @copyright Copyright (c) Barrel Strength Design LLC
* @license https://craftcms.github.io/license
*/
namespace barrelstrength\sproutfields\fields;
use barrelstrength\sproutbasefields\base\AddressFieldTrait;
use barrelstrength\sproutbasefields\models\Address as AddressModel;
use barrelstrength\sproutbasefields\SproutBaseFields;
use CommerceGuys\Addressing\Address as CommerceGuysAddress;
use CommerceGuys\Addressing\AddressFormat\AddressFormatRepository;
use CommerceGuys\Addressing\Country\CountryRepository;
use CommerceGuys\Addressing\Formatter\DefaultFormatter;
use CommerceGuys\Addressing\Subdivision\SubdivisionRepository;
use Craft;
use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\PreviewableFieldInterface;
use craft\errors\SiteNotFoundException;
use Throwable;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
use yii\db\Exception;
use yii\db\StaleObjectException;
/**
* Class Address
*
* @package barrelstrength\sproutfields\fields
*
* @property array $elementValidationRules
* @property null|string $settingsHtml
* @property string $contentColumnType
*/
class Address extends Field implements PreviewableFieldInterface
{
use AddressFieldTrait;
/**
* @var string|null
*/
public $value;
public static function supportedTranslationMethods(): array
{
return [
self::TRANSLATION_METHOD_NONE,
self::TRANSLATION_METHOD_SITE,
self::TRANSLATION_METHOD_SITE_GROUP,
self::TRANSLATION_METHOD_LANGUAGE,
self::TRANSLATION_METHOD_CUSTOM,
];
}
/**
* @inheritdoc
*/
public static function displayName(): string
{
return Craft::t('sprout-fields', 'Address (Sprout Fields)');
}
/**
* @inheritdoc
*/
public static function hasContentColumn(): bool
{
return false;
}
/**
* @return string|null
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws SiteNotFoundException
* @throws \yii\base\Exception
*/
public function getSettingsHtml()
{
return SproutBaseFields::$app->addressField->getSettingsHtml($this);
}
/**
* @param $value
* @param ElementInterface|null $element
*
* @return string
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws \yii\base\Exception
*/
public function getInputHtml($value, ElementInterface $element = null): string
{
return SproutBaseFields::$app->addressField->getInputHtml($this, $value, $element);
}
/**
* How the field output will appear for Revisions
*
* @param $value
* @param ElementInterface $element
*
* @return string
*/
public function getStaticHtml($value, ElementInterface $element): string
{
return SproutBaseFields::$app->addressField->getStaticHtml($this, $value, $element);
}
/**
* @param $value
* @param ElementInterface|null $element
*
* @return AddressModel|mixed|null
*/
public function normalizeValue($value, ElementInterface $element = null)
{
return SproutBaseFields::$app->addressField->normalizeValue($this, $value, $element);
}
/**
* @param ElementInterface $element
* @param bool $isNew
*
* @throws Throwable
* @throws Exception
* @throws StaleObjectException
*/
public function afterElementSave(ElementInterface $element, bool $isNew)
{
SproutBaseFields::$app->addressField->afterElementSave($this, $element, $isNew);
parent::afterElementSave($element, $isNew);
}
/**
* @inheritdoc
*/
public function getTableAttributeHtml($value, ElementInterface $element): string
{
if (!$value) {
return '';
}
$addressFormatRepository = new AddressFormatRepository();
$countryRepository = new CountryRepository();
$subdivisionRepository = new SubdivisionRepository();
$formatter = new DefaultFormatter($addressFormatRepository, $countryRepository, $subdivisionRepository);
$address = new CommerceGuysAddress();
$address = $address
->withCountryCode($value->countryCode)
->withAdministrativeArea($value->administrativeAreaCode)
->withLocality($value->locality)
->withPostalCode($value->postalCode)
->withAddressLine1($value->address1)
->withAddressLine2($value->address2);
$html = $formatter->format($address);
$html = str_replace(' ', ' ', $html);
return $html;
}
}