Skip to content

Commit 1fc6ca9

Browse files
committed
[REFACTOR] Clean up code and added license
1 parent e17ebd3 commit 1fc6ca9

13 files changed

Lines changed: 929 additions & 136 deletions

File tree

Block/Checkout/LayoutProcessor.php

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,97 @@
11
<?php
2+
/**
3+
* Copyright © Experius B.V. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
27

38
namespace Experius\ExtraCheckoutAddressFields\Block\Checkout;
49

5-
class LayoutProcessor implements \Magento\Checkout\Block\Checkout\LayoutProcessorInterface
10+
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
11+
12+
class LayoutProcessor implements LayoutProcessorInterface
613
{
714
protected $helper;
815

16+
/**
17+
* LayoutProcessor constructor.
18+
*
19+
* @param \Experius\ExtraCheckoutAddressFields\Helper\Data $helper
20+
*/
921
public function __construct(
1022
\Experius\ExtraCheckoutAddressFields\Helper\Data $helper
11-
)
12-
{
23+
) {
1324
$this->helper = $helper;
1425
}
1526

16-
public function process($result) {
27+
/**
28+
* @param array $result
29+
* @return array
30+
*/
31+
public function process($result)
32+
{
1733
$result = $this->getShippingFormFields($result);
1834
$result = $this->getBillingFormFields($result);
1935
return $result;
2036
}
2137

22-
public function getAdditionalFields($addressType='shipping'){
23-
if($addressType=='shipping') {
38+
/**
39+
* @param string $addressType
40+
* @return array
41+
*/
42+
public function getAdditionalFields($addressType='shipping')
43+
{
44+
if ($addressType=='shipping') {
2445
return $this->helper->getExtraCheckoutAddressFields('extra_checkout_shipping_address_fields');
2546
}
2647
return $this->helper->getExtraCheckoutAddressFields('extra_checkout_billing_address_fields');
2748
}
2849

29-
public function getShippingFormFields($result){
30-
if(isset($result['components']['checkout']['children']['steps']['children']
50+
/**
51+
* @param $result
52+
* @return mixed
53+
*/
54+
public function getShippingFormFields($result)
55+
{
56+
if (isset($result['components']['checkout']['children']['steps']['children']
3157
['shipping-step']['children']['shippingAddress']['children']
3258
['shipping-address-fieldset'])
33-
){
34-
35-
$shippingPostcodeFields = $this->getFields('shippingAddress.custom_attributes','shipping');
59+
) {
60+
$shippingPostcodeFields = $this->getFields('shippingAddress.custom_attributes', 'shipping');
3661

3762
$shippingFields = $result['components']['checkout']['children']['steps']['children']
3863
['shipping-step']['children']['shippingAddress']['children']
3964
['shipping-address-fieldset']['children'];
4065

41-
if(isset($shippingFields['street'])){
66+
if (isset($shippingFields['street'])) {
4267
unset($shippingFields['street']['children'][1]['validation']);
4368
unset($shippingFields['street']['children'][2]['validation']);
4469
}
4570

46-
$shippingFields = array_replace_recursive($shippingFields,$shippingPostcodeFields);
71+
$shippingFields = array_replace_recursive($shippingFields, $shippingPostcodeFields);
4772

4873
$result['components']['checkout']['children']['steps']['children']
4974
['shipping-step']['children']['shippingAddress']['children']
5075
['shipping-address-fieldset']['children'] = $shippingFields;
51-
5276
}
5377

5478
return $result;
5579
}
5680

57-
public function getBillingFormFields($result){
58-
if(isset($result['components']['checkout']['children']['steps']['children']
81+
/**
82+
* @param $result
83+
* @return mixed
84+
*/
85+
public function getBillingFormFields($result)
86+
{
87+
if (isset($result['components']['checkout']['children']['steps']['children']
5988
['billing-step']['children']['payment']['children']
6089
['payments-list'])) {
61-
6290
$paymentForms = $result['components']['checkout']['children']['steps']['children']
6391
['billing-step']['children']['payment']['children']
6492
['payments-list']['children'];
6593

6694
foreach ($paymentForms as $paymentMethodForm => $paymentMethodValue) {
67-
6895
$paymentMethodCode = str_replace('-form', '', $paymentMethodForm);
6996

7097
if (!isset($result['components']['checkout']['children']['steps']['children']['billing-step']['children']['payment']['children']['payments-list']['children'][$paymentMethodCode . '-form'])) {
@@ -75,7 +102,7 @@ public function getBillingFormFields($result){
75102
['billing-step']['children']['payment']['children']
76103
['payments-list']['children'][$paymentMethodCode . '-form']['children']['form-fields']['children'];
77104

78-
$billingPostcodeFields = $this->getFields('billingAddress' . $paymentMethodCode . '.custom_attributes','billing');
105+
$billingPostcodeFields = $this->getFields('billingAddress' . $paymentMethodCode . '.custom_attributes', 'billing');
79106

80107
$billingFields = array_replace_recursive($billingFields, $billingPostcodeFields);
81108

@@ -88,23 +115,34 @@ public function getBillingFormFields($result){
88115
return $result;
89116
}
90117

91-
public function getFields($scope,$addressType){
118+
/**
119+
* @param $scope
120+
* @param $addressType
121+
* @return array
122+
*/
123+
public function getFields($scope, $addressType)
124+
{
92125
$fields = [];
93-
foreach($this->getAdditionalFields($addressType) as $field){
94-
$fields[$field] = $this->getField($field,$scope);
126+
foreach ($this->getAdditionalFields($addressType) as $field) {
127+
$fields[$field] = $this->getField($field, $scope);
95128
}
96129
return $fields;
97130
}
98131

99-
public function getField($attributeCode,$scope) {
132+
/**
133+
* @param $attributeCode
134+
* @param $scope
135+
* @return array
136+
*/
137+
public function getField($attributeCode, $scope)
138+
{
100139
$field = [
101140
'config' => [
102141
'customScope' => $scope,
103142
],
104-
'dataScope' => $scope . '.'.$attributeCode,
143+
'dataScope' => $scope . '.' . $attributeCode,
105144
];
106145

107146
return $field;
108147
}
109-
110-
}
148+
}

COPYING.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Copyright © 2020-present Experius B.V.
2+
3+
This file is part of Experius/ExtraCheckoutAddressFields.
4+
5+
Experius/Test is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU General Public License as published by
7+
the Free Software Foundation, either version 3 of the License, or
8+
(at your option) any later version.
9+
10+
This program is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU General Public License for more details.
14+
15+
You should have received a copy of the GNU General Public License
16+
along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
Please see LICENSE.txt for the full text of GNU General Public License

Helper/Data.php

Lines changed: 85 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,85 @@
1-
<?php
2-
3-
namespace Experius\ExtraCheckoutAddressFields\Helper;
4-
5-
use Magento\Framework\App\Helper\Context;
6-
use Magento\Framework\App\Helper\AbstractHelper;
7-
8-
class Data extends AbstractHelper{
9-
10-
protected $fieldsetConfig;
11-
12-
protected $logger;
13-
14-
public function __construct(
15-
\Magento\Framework\DataObject\Copy\Config $fieldsetConfig,
16-
\Psr\Log\LoggerInterface $logger
17-
) {
18-
$this->fieldsetConfig = $fieldsetConfig;
19-
$this->logger = $logger;
20-
}
21-
22-
public function getExtraCheckoutAddressFields($fieldset='extra_checkout_billing_address_fields',$root='global'){
23-
24-
$fields = $this->fieldsetConfig->getFieldset($fieldset, $root);
25-
26-
$extraCheckoutFields = [];
27-
28-
if (is_array($fields)) {
29-
foreach ($fields as $field => $fieldInfo) {
30-
$extraCheckoutFields[] = $field;
31-
}
32-
}
33-
34-
return $extraCheckoutFields;
35-
36-
}
37-
38-
public function transportFieldsFromExtensionAttributesToObject(
39-
$fromObject,
40-
$toObject,
41-
$fieldset='extra_checkout_billing_address_fields'
42-
)
43-
{
44-
foreach($this->getExtraCheckoutAddressFields($fieldset) as $extraField) {
45-
46-
$set = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
47-
$get = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
48-
49-
$value = $fromObject->$get();
50-
try {
51-
$toObject->$set($value);
52-
} catch (\Exception $e) {
53-
$this->logger->critical($e->getMessage());
54-
}
55-
}
56-
57-
return $toObject;
58-
}
59-
}
1+
<?php
2+
/**
3+
* Copyright © Experius B.V. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Experius\ExtraCheckoutAddressFields\Helper;
9+
10+
use Magento\Framework\App\Helper\AbstractHelper;
11+
use Magento\Framework\DataObject\Copy\Config;
12+
use Psr\Log\LoggerInterface;
13+
14+
class Data extends AbstractHelper
15+
{
16+
/**
17+
* @var Config
18+
*/
19+
protected $fieldsetConfig;
20+
21+
/**
22+
* @var LoggerInterface
23+
*/
24+
protected $logger;
25+
26+
/**
27+
* Data constructor.
28+
*
29+
* @param Config $fieldsetConfig
30+
* @param LoggerInterface $logger
31+
*/
32+
public function __construct(
33+
Config $fieldsetConfig,
34+
LoggerInterface $logger
35+
) {
36+
$this->fieldsetConfig = $fieldsetConfig;
37+
$this->logger = $logger;
38+
}
39+
40+
/**
41+
* @param string $fieldset
42+
* @param string $root
43+
* @return array
44+
*/
45+
public function getExtraCheckoutAddressFields($fieldset='extra_checkout_billing_address_fields', $root='global')
46+
{
47+
$fields = $this->fieldsetConfig->getFieldset($fieldset, $root);
48+
49+
$extraCheckoutFields = [];
50+
51+
if (is_array($fields)) {
52+
foreach ($fields as $field => $fieldInfo) {
53+
$extraCheckoutFields[] = $field;
54+
}
55+
}
56+
57+
return $extraCheckoutFields;
58+
}
59+
60+
/**
61+
* @param $fromObject
62+
* @param $toObject
63+
* @param string $fieldset
64+
* @return mixed
65+
*/
66+
public function transportFieldsFromExtensionAttributesToObject(
67+
$fromObject,
68+
$toObject,
69+
$fieldset='extra_checkout_billing_address_fields'
70+
) {
71+
foreach ($this->getExtraCheckoutAddressFields($fieldset) as $extraField) {
72+
$set = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
73+
$get = 'get' . str_replace(' ', '', ucwords(str_replace('_', ' ', $extraField)));
74+
75+
$value = $fromObject->$get();
76+
try {
77+
$toObject->$set($value);
78+
} catch (\Exception $e) {
79+
$this->logger->critical($e->getMessage());
80+
}
81+
}
82+
83+
return $toObject;
84+
}
85+
}

0 commit comments

Comments
 (0)