Completed
Push — master ( 6b87b2...6de35a )
by Adam
16:39 queued 03:06
created

ModelDataMapper::isEmptyValue()   A

Complexity

Conditions 4
Paths 3

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 8
Code Lines 4

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 3
nop 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Component\Form\DataMapper;
14
15
use Symfony\Component\PropertyAccess\PropertyPathInterface;
16
use WellCommerce\Component\Form\Elements\ElementCollection;
17
use WellCommerce\Component\Form\Elements\ElementInterface;
18
use WellCommerce\Component\Form\Elements\FormInterface;
19
20
/**
21
 * Class ModelDataMapper
22
 *
23
 * @author Adam Piotrowski <[email protected]>
24
 */
25
class ModelDataMapper extends AbstractDataMapper
26
{
27
    /**
28
     * Maps model data to form elements
29
     *
30
     * @param FormInterface $form
31
     */
32
    public function mapDataToForm(FormInterface $form)
33
    {
34
        $this->mapModelDataToElementCollection($form->getChildren());
35
    }
36
    
37
    /**
38
     * Maps all submitted values to model data representation
39
     *
40
     * @param FormInterface $form
41
     */
42
    public function mapFormToData(FormInterface $form)
43
    {
44
        $this->mapElementCollectionToModelData($form->getChildren());
45
    }
46
    
47
    /**
48
     * @param ElementCollection $children
49
     */
50
    protected function mapElementCollectionToModelData(ElementCollection $children)
51
    {
52
        $children->forAll(function (ElementInterface $child) {
53
            $this->mapElementToModelData($child);
54
        });
55
    }
56
    
57
    /**
58
     * Maps element value to model data
59
     *
60
     * @param ElementInterface $child
61
     */
62
    protected function mapElementToModelData(ElementInterface $child)
63
    {
64
        $this->setModelValueFromElement($child);
65
        
66
        $children = $child->getChildren();
67
        if ($children->count()) {
68
            $this->mapElementCollectionToModelData($children);
69
        }
70
    }
71
    
72
    /**
73
     * Maps data to single element
74
     *
75
     * @param ElementInterface $child
76
     */
77
    protected function mapModelDataToElement(ElementInterface $child)
78
    {
79
        $this->setDefaultElementValue($child);
80
        
81
        $children = $child->getChildren();
82
        
83
        if ($children->count()) {
84
            $this->mapModelDataToElementCollection($children);
85
        }
86
    }
87
    
88
    /**
89
     * Maps data using recursion to all children
90
     *
91
     * @param ElementCollection $children
92
     */
93
    protected function mapModelDataToElementCollection(ElementCollection $children)
94
    {
95
        foreach ($children->all() as $child) {
96
            $this->mapModelDataToElement($child);
97
        }
98
    }
99
    
100
    /**
101
     * Sets value for element
102
     *
103
     * @param ElementInterface $element
104
     */
105
    protected function setDefaultElementValue(ElementInterface $element)
106
    {
107
        $propertyPath = $element->getPropertyPath();
108
        
109
        if ($propertyPath instanceof PropertyPathInterface) {
110
            if ($this->propertyAccessor->isReadable($this->data, $propertyPath)) {
111
                $value = $this->propertyAccessor->getValue($this->data, $propertyPath);
112
                if ($this->isEmptyValue($value)) {
113
                    $value = $element->getDefaultValue();
114
                }
115
                
116
                if ($element->hasTransformer() && is_object($value)) {
117
                    $transformer = $element->getTransformer();
118
                    $value       = $transformer->transform($value);
119
                }
120
                
121
                $element->setValue($value);
122
            }
123
        }
124
    }
125
    
126
    /**
127
     * Transforms value if needed or directly changes model property
128
     *
129
     * @param ElementInterface $child
130
     */
131
    protected function setModelValueFromElement(ElementInterface $child)
132
    {
133
        $propertyPath = $child->getPropertyPath();
134
        
135
        if ($propertyPath instanceof PropertyPathInterface) {
136
            if ($this->propertyAccessor->isWritable($this->data, $propertyPath)) {
137
                if ($child->hasTransformer()) {
138
                    $transformer = $child->getTransformer();
139
                    $transformer->reverseTransform($this->data, $propertyPath, $child->getValue());
140
                } else {
141
                    $this->propertyAccessor->setValue($this->data, $propertyPath, $child->getValue());
142
                }
143
            }
144
        }
145
    }
146
    
147
    private function isEmptyValue($value): bool
148
    {
149
        if (is_array($value) && empty($value)) {
150
            return true;
151
        }
152
        
153
        return null === $value || '' === $value;
154
    }
155
}
156