Skip to content

Commit 996432a

Browse files
authored
Merge pull request #100 from JerrySmidt/master
3.5.0
2 parents c02c335 + 3e80bb4 commit 996432a

32 files changed

+340
-193
lines changed

Block/Checkout/AttributeMerger.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Flekto\Postcode\Block\Checkout;
4+
5+
use Magento\Checkout\Block\Checkout\AttributeMerger as BaseAttributeMerger;
6+
7+
/**
8+
* Fields attribute merger extension.
9+
*/
10+
class AttributeMerger extends BaseAttributeMerger
11+
{
12+
/**
13+
* Retrieve field configuration for street address attribute
14+
*
15+
* @param string $attributeCode
16+
* @param array $attributeConfig
17+
* @param string $providerName name of the storage container used by UI component
18+
* @param string $dataScopePrefix
19+
* @return array
20+
*/
21+
protected function getMultilineFieldConfig($attributeCode, array $attributeConfig, $providerName, $dataScopePrefix)
22+
{
23+
$config = parent::getMultilineFieldConfig($attributeCode, $attributeConfig, $providerName, $dataScopePrefix);
24+
25+
if ($attributeCode === 'street') {
26+
$config['component'] = 'Flekto_Postcode/js/form/components/street';
27+
$config['config']['template'] = 'Flekto_Postcode/group/street';
28+
}
29+
30+
return $config;
31+
}
32+
}

Block/Onepage/LayoutProcessor.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,34 @@
66
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
77
use Magento\Framework\View\Element\Template\Context;
88
use Magento\Framework\Exception\LocalizedException;
9+
use Flekto\Postcode\Helper\StoreConfigHelper;
910

1011
class LayoutProcessor extends AbstractBlock implements LayoutProcessorInterface
1112
{
12-
protected $scopeConfig;
13+
/**
14+
* JS layout configuration
15+
*
16+
* @var array
17+
*/
1318
protected $jsLayout;
1419

20+
/**
21+
* @var StoreConfigHelper
22+
*/
23+
private $storeConfigHelper;
24+
1525
/**
1626
* Constructor
1727
*
1828
* @access public
1929
* @param Context $context
30+
* @param StoreConfigHelper $storeConfigHelper
2031
* @param array $data (default: [])
2132
* @return void
2233
*/
23-
public function __construct(Context $context, array $data = [])
34+
public function __construct(Context $context, StoreConfigHelper $storeConfigHelper, array $data = [])
2435
{
25-
$this->scopeConfig = $context->getScopeConfig();
36+
$this->storeConfigHelper = $storeConfigHelper;
2637

2738
parent::__construct($context, $data);
2839
}
@@ -36,14 +47,15 @@ public function __construct(Context $context, array $data = [])
3647
*/
3748
public function process($jsLayout): array
3849
{
39-
$moduleEnabled = $this->scopeConfig->getValue('postcodenl_api/general/enabled', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
40-
41-
if (!$moduleEnabled) {
50+
if (!$this->storeConfigHelper->isEnabled()) {
4251
return $jsLayout;
4352
}
4453

4554
$this->jsLayout = $jsLayout;
4655

56+
// Settings accessible via checkoutProvider.
57+
$this->jsLayout['components']['checkoutProvider']['postcodeEuConfig'] = $this->storeConfigHelper->getJsInit();
58+
4759
// Shipping fields
4860
$shippingFields = &$this->_getJsLayoutRef([
4961
'components',
@@ -221,7 +233,7 @@ private function _updateDataScope($fields, $dataScope): array
221233
*/
222234
private function _changeAddressFieldsPosition($addressFields): array
223235
{
224-
if ($this->scopeConfig->getValue('postcodenl_api/general/change_fields_position') != '1') {
236+
if (!$this->storeConfigHelper->isSetFlag('change_fields_position')) {
225237
return $addressFields;
226238
}
227239

Block/System/Config/Status.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public function getConfig(): array
102102
'enabled' => $this->_storeConfigHelper->isEnabled(),
103103
'module_version' => $this->_storeConfigHelper->getModuleVersion(),
104104
'supported_countries' => $this->_storeConfigHelper->getSupportedCountries(),
105-
'account_name' => $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_name']),
106-
'account_status' => $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_status']), // Defaults to "new", see etc/config.xml.
105+
'account_name' => $this->_storeConfigHelper->getValue('account_name'),
106+
'account_status' => $this->_storeConfigHelper->getValue('account_status'), // Defaults to "new", see etc/config.xml.
107107
'has_credentials' => $this->_storeConfigHelper->hasCredentials(),
108108
];
109109
}
@@ -135,7 +135,7 @@ public function getModuleInfo(): array
135135
*/
136136
public function getApiStatusDescription(): string
137137
{
138-
$status = $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_status']);
138+
$status = $this->_storeConfigHelper->getValue('account_status');
139139

140140
switch ($status) {
141141
case \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_NEW:
@@ -179,7 +179,7 @@ private function _getCachedData(): array
179179
*/
180180
private function _getAccountInfo(): array
181181
{
182-
$status = $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_status']);
182+
$status = $this->_storeConfigHelper->getValue('account_status');
183183
if ($status === \Flekto\Postcode\Helper\ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE) {
184184
return $this->_apiClientHelper->getAccountInfo();
185185
}

GraphQl/Exception/GraphQlHeaderException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class GraphQlHeaderException extends LocalizedException implements ClientAware
2727
* @param int $code
2828
* @param boolean $isSafe
2929
*/
30-
public function __construct(Phrase $phrase, \Exception $cause = null, $code = 0, $isSafe = true)
30+
public function __construct(Phrase $phrase, ?\Exception $cause = null, $code = 0, $isSafe = true)
3131
{
3232
$this->isSafe = $isSafe;
3333
parent::__construct($phrase, $cause, $code);

Helper/ApiClientHelper.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Flekto\Postcode\Helper\StoreConfigHelper;
77
use Flekto\Postcode\Service\Exception\NotFoundException;
88
use Flekto\Postcode\Service\PostcodeApiClient;
9+
use Magento\Customer\Helper\Address as AddressHelper;
910
use Magento\Developer\Helper\Data;
1011
use Magento\Directory\Model\RegionFactory;
1112
use Magento\Framework\App\Helper\AbstractHelper;
@@ -34,6 +35,7 @@ class ApiClientHelper extends AbstractHelper
3435
protected $_storeConfigHelper;
3536
protected $_productMetadata;
3637
protected $_regionFactory;
38+
protected $_addressHelper;
3739

3840
/**
3941
* __construct function.
@@ -49,6 +51,7 @@ class ApiClientHelper extends AbstractHelper
4951
* @param StoreConfigHelper $storeConfigHelper
5052
* @param ProductMetadataInterface $productMetadata
5153
* @param RegionFactory $regionFactory
54+
* @param AddressHelper $addressHelper
5255
* @return void
5356
*/
5457
public function __construct(
@@ -61,7 +64,8 @@ public function __construct(
6164
LocaleResolver $localeResolver,
6265
StoreConfigHelper $storeConfigHelper,
6366
ProductMetadataInterface $productMetadata,
64-
RegionFactory $regionFactory
67+
RegionFactory $regionFactory,
68+
AddressHelper $addressHelper
6569
) {
6670
$this->_moduleList = $moduleList;
6771
$this->_developerHelper = $developerHelper;
@@ -72,6 +76,7 @@ public function __construct(
7276
$this->_storeConfigHelper = $storeConfigHelper;
7377
$this->_productMetadata = $productMetadata;
7478
$this->_regionFactory = $regionFactory;
79+
$this->_addressHelper = $addressHelper;
7580
parent::__construct($context);
7681
}
7782

@@ -129,6 +134,7 @@ public function getAddressDetails(string $context): array
129134
$sessionId = $this->_getSessionId();
130135
$response = $client->internationalGetDetails($context, $sessionId);
131136
$response['region'] = $this->_getRegionFromDetails($response);
137+
$response['streetLines'] = $this->_getStreetLines($response);
132138
$response = $this->_prepareResponse($response, $client);
133139

134140
return $response;
@@ -203,6 +209,54 @@ protected function _getRegionByName(string $name, string $countryIso2): array
203209
return ['id' => $id ?? null, 'name' => $name ?? null];
204210
}
205211

212+
/**
213+
* Get street lines from an address details response.
214+
*
215+
* The amount of lines is limited by the configured number of lines in a street address.
216+
*
217+
* @param array $addressDetails
218+
* @return array - Street lines formatted according to country and config.
219+
*/
220+
protected function _getStreetLines(array $addressDetails): array
221+
{
222+
$address = $addressDetails['address'];
223+
$countryIso2 = $addressDetails['country']['iso2Code'];
224+
$lastLineIndex = $this->_addressHelper->getStreetLines() - 1;
225+
226+
if ($this->_storeConfigHelper->isSetFlag('split_street_values')) {
227+
// Assume fields are fixed street parts, independent of country.
228+
$parts = [
229+
$address['street'],
230+
$address['buildingNumber'] ?? '',
231+
$address['buildingNumberAddition'] ?? '',
232+
];
233+
$lines = array_slice($parts, 0, $lastLineIndex);
234+
$lines[] = implode(' ', array_slice($parts, $lastLineIndex));
235+
} elseif ($countryIso2 === 'LU') {
236+
$lines = [$address['building'] . ', ' . $address['street']];
237+
} elseif ($countryIso2 === 'FR') {
238+
$lines = [trim($address['building'] . ' ' . $address['street'])];
239+
} elseif ($countryIso2 === 'GB') {
240+
$building = $addressDetails['details']['gbrBuilding'];
241+
if ($address['street'] === '') {
242+
$separator = '';
243+
} elseif ($building['number'] === null && $building['addition'] === null) {
244+
$separator = ', ';
245+
} else {
246+
$separator = ' ';
247+
}
248+
249+
// Support multiple lines in British address.
250+
$parts = explode(', ', $address['building'] . $separator . $address['street']);
251+
$lines = array_slice($parts, 0, $lastLineIndex);
252+
$lines[] = implode(', ', array_slice($parts, $lastLineIndex));
253+
} else {
254+
$lines = [trim($address['street'] . ' ' . $address['building'])];
255+
}
256+
257+
return $lines;
258+
}
259+
206260
/**
207261
* Get session identifier.
208262
*
@@ -445,13 +499,13 @@ private function _getDebugInfo(): array
445499
'configuration' => [
446500
'key' => $credentials['key'],
447501
'secret' => substr_replace($credentials['secret'], '***', 3, -3),
448-
'debug' => $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['api_debug']),
502+
'debug' => $this->_storeConfigHelper->getValue('api_debug'),
449503
],
450504
'modules' => $this->_getMagentoModules(),
451505
];
452506

453507
// Module version
454-
$debug['moduleVersion'] = $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['module_version']);
508+
$debug['moduleVersion'] = $this->_storeConfigHelper->getValue('module_version');
455509

456510
// Magento version
457511
$version = $this->_productMetadata->getVersion();

Helper/Data.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function isFormattedOutputDisabled(): bool
7373
{
7474
return
7575
$this->isDisabled()
76-
|| ShowHideAddressFields::FORMAT != $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['show_hide_address_fields']);
76+
|| ShowHideAddressFields::FORMAT != $this->_storeConfigHelper->getValue('show_hide_address_fields');
7777
}
7878

7979
/**
@@ -87,7 +87,7 @@ public function isNlComponentDisabled(): bool
8787
return
8888
$this->isDisabled()
8989
|| false === in_array('NL', $this->_storeConfigHelper->getEnabledCountries())
90-
|| NlInputBehavior::ZIP_HOUSE != $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['nl_input_behavior']);
90+
|| NlInputBehavior::ZIP_HOUSE != $this->_storeConfigHelper->getValue('nl_input_behavior');
9191
}
9292

9393
/**
@@ -99,8 +99,8 @@ public function isNlComponentDisabled(): bool
9999
public function isDisabled(): bool
100100
{
101101
return
102-
false === $this->_storeConfigHelper->isSetFlag(StoreConfigHelper::PATH['enabled'])
103-
|| ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE != $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['account_status']);
102+
false === $this->_storeConfigHelper->isSetFlag('enabled')
103+
|| ApiClientHelper::API_ACCOUNT_STATUS_ACTIVE != $this->_storeConfigHelper->getValue('account_status');
104104
}
105105

106106
/**
@@ -113,8 +113,8 @@ public function isAutofillBypassDisabled(): bool
113113
{
114114
return
115115
$this->isDisabled()
116-
|| ShowHideAddressFields::SHOW == $this->_storeConfigHelper->getValue(StoreConfigHelper::PATH['show_hide_address_fields'])
117-
|| $this->_storeConfigHelper->isSetFlag(StoreConfigHelper::PATH['allow_autofill_bypass']) === false;
116+
|| ShowHideAddressFields::SHOW == $this->_storeConfigHelper->getValue('show_hide_address_fields')
117+
|| $this->_storeConfigHelper->isSetFlag('allow_autofill_bypass') === false;
118118
}
119119

120120
/**

0 commit comments

Comments
 (0)