Completed
Push — master ( 3c661d...2a57f9 )
by Will
26s queued 12s
created

Checkout/Component/CheckoutComponentNamespaced.php (1 issue)

1
<?php
2
3
namespace SilverShop\Checkout\Component;
4
5
use SilverShop\Model\Order;
6
7
/**
8
 * Proxy class to handle namespacing field names for checkout components
9
 */
10
class CheckoutComponentNamespaced extends CheckoutComponent
11
{
12
    /**
13
     * @var CheckoutComponent
14
     */
15
    protected $proxy;
16
17
    public function __construct(CheckoutComponent $component)
18
    {
19
        $this->proxy = $component;
20
    }
21
22
    /**
23
     * @return CheckoutComponent
24
     */
25
    public function Proxy()
26
    {
27
        return $this->proxy;
28
    }
29
30
    public function getFormFields(Order $order)
31
    {
32
        $fields = $this->proxy->getFormFields($order);
33
        $allFields = $fields->dataFields();
34
        if ($allFields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $allFields of type SilverStripe\Forms\FormField[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
35
            foreach ($allFields as $field) {
36
                $field->setName($this->namespaceFieldName($field->getName()));
37
            }
38
        }
39
40
        return $fields;
41
    }
42
43
    public function validateData(Order $order, array $data)
44
    {
45
        return $this->proxy->validateData($order, $this->unnamespaceData($data));
46
    }
47
48
    public function getData(Order $order)
49
    {
50
        return $this->namespaceData($this->proxy->getData($order));
51
    }
52
53
    public function setData(Order $order, array $data)
54
    {
55
        return $this->proxy->setData($order, $this->unnamespaceData($data));
56
    }
57
58
    public function getRequiredFields(Order $order)
59
    {
60
        $fields = $this->proxy->getRequiredFields($order);
61
        $namespaced = array();
62
        foreach ($fields as $field) {
63
            $namespaced[] = $this->namespaceFieldName($field);
64
        }
65
        return $namespaced;
66
    }
67
68
    public function dependsOn()
69
    {
70
        return $this->proxy->dependsOn();
71
    }
72
73
    public function name()
74
    {
75
        return $this->proxy->name();
76
    }
77
78
    public function providesPaymentData()
79
    {
80
        return $this->proxy->providesPaymentData();
81
    }
82
83
    //namespacing functions
84
85
    public function namespaceData(array $data)
86
    {
87
        $newdata = array();
88
        foreach ($data as $key => $value) {
89
            $newdata[$this->namespaceFieldName($key)] = $value;
90
        }
91
        return $newdata;
92
    }
93
94
    public function unnamespaceData(array $data)
95
    {
96
        $newdata = array();
97
        foreach ($data as $key => $value) {
98
            if (strpos($key, $this->name()) === 0) {
99
                $newdata[$this->unnamespaceFieldName($key)] = $value;
100
            }
101
        }
102
        return $newdata;
103
    }
104
105
    public function namespaceFieldName($name)
106
    {
107
        return $this->name() . "_" . $name;
108
    }
109
110
    public function unnamespaceFieldName($name)
111
    {
112
        return substr($name, strlen($this->name() . "_"));
113
    }
114
}
115