Completed
Pull Request — master (#23)
by
unknown
03:14
created

BaseModel::loadModel()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
ccs 11
cts 11
cp 1
rs 8.7972
cc 4
eloc 14
nc 6
nop 2
crap 4
1
<?php
2
namespace pastuhov\ymlcatalog\models;
3
4
use yii\base\Model;
5
use yii\base\Exception;
6
7
/**
8
 * Base model.
9
 *
10
 * @package pastuhov\ymlcatalog\models
11
 */
12
class BaseModel extends Model
13
{
14
    /**
15
     * @var string
16
     */
17
    public static $tag;
18
19
    /**
20
     * @var string[]
21
     */
22
    public static $tagProperties = [];
23
24
    /**
25
     * @return string[]
26 9
     */
27
    public function getYmlAttributes()
28 9
    {
29
        return $this->attributes();
30
    }
31
32
    /**
33
     * @return string
34 9
     */
35
    public function getYml()
36 9
    {
37
        $string = '';
38 9
39 9
        $string .= $this->getYmlStartTag();
40 9
        $string .= $this->getYmlBody();
41
        $string .= $this->getYmlEndTag() . PHP_EOL;
42 9
43
        return $string;
44
    }
45
46
    /**
47
     * Возвращает текст xml без закрытого тэга
48
     *
49
     * @return string
50
     */
51
    public function getYmlWithoutEndTag()
52
    {
53
        $string = '';
54
55
        $string .= $this->getYmlStartTag();
56
        $string .= $this->getYmlBody();
57
58
        return $string;
59
    }
60
61
    /**
62
     * @param array $params
63
     */
64 9
    public function setParams(array $params)
65
    {
66 9
67 9
    }
68 9
69 6
    /**
70
     * @param array $pictures
71 9
     */
72
    public function setPictures(array $pictures)
73
    {
74
75
    }
76
77 9
    /**
78
     * @param array $options
79 9
     */
80 9
    public function setDeliveryOptions(array $options)
81 9
    {
82 6
83
    }
84 9
85
    /**
86
     * @param $valuesModel
87
     * @param null $onValidationError
88
     * 
89
     * @return bool
90 9
     * @throws Exception
91
     */
92 9
    public function loadModel($valuesModel, $onValidationError = null)
93
    {
94
        $attributes = [];
95
        foreach ($this->attributes() as $attribute) {
96
            $methodName = 'get' . ucfirst($attribute);
97
            $attributeValue = $valuesModel->$methodName();
98 9
99
            $attributes[$attribute] = $attributeValue;
100 9
        }
101 9
102
        $this->load($attributes, '');
103 9
104 9
        if (!$this->validate()) {
105 9
            if (is_callable($onValidationError)) {
106 9
                $onValidationError($this);
107 6
                return false;
108 6
            } else {
109
                throw new Exception('Model values is invalid ' . serialize($this->getErrors()));
110 9
            }
111
        }
112
113
        return true;
114
    }
115
116
    /**
117 9
     * @return string
118
     */
119 9 View Code Duplication
    protected function getYmlStartTag()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
120
    {
121
        $string = '';
122
        if (static::$tag) {
123
            $string = '<' . static::$tag . $this->getYmlTagProperties() . '>';
124
        }
125
126 9
        return $string;
127
    }
128 9
129 9
    /**
130 9
     * @return string
131
     */
132 View Code Duplication
    protected function getYmlEndTag()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133 9
    {
134
        $string = '';
135 9
        if (static::$tag) {
136
            $string .= '</' . static::$tag . '>';
137
        }
138
139
        return $string;
140
    }
141
142
    /**
143
     * @return string
144
     */
145
    protected function getYmlBody()
146
    {
147
        return $this->name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<pastuhov\ymlcatalog\models\BaseModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    protected function getYmlTagProperties()
154
    {
155
        $string = '';
156
        $properties = static::$tagProperties;
157
158
        foreach ($properties as $property) {
159
            $value = $this->getAttributeValue($property);
160
            if ($value !== null) {
161
                $string .= ' ' . $property . '="' . $value . '"';
162
            }
163
        }
164
165
        return $string;
166
    }
167
168
    /**
169
     * @param string $attribute
170
     * @return string
171
     */
172
    protected function getAttributeValue($attribute)
173
    {
174
        return $this->$attribute;
175
    }
176
177
    /**
178
     * @param string $attribute
179
     * @return string
180
     */
181
    protected function getYmlAttribute($attribute)
182
    {
183
        $value = $this->getAttributeValue($attribute);
184
        if ($value === null) {
185
            return '';
186
        }
187
188
        $string = '<' . $attribute . '>' . $value . '</' . $attribute. '>' . PHP_EOL;
189
190
        return $string;
191
    }
192
}
193