Completed
Pull Request — master (#39)
by
unknown
02:19
created

BaseModel::setPictures()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 1
cp 0
rs 10
cc 1
eloc 1
nc 1
nop 1
crap 2
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 function getTagProperties() {
23
        return [];
24
    }
25
26 9
    /**
27
     * @return string[]
28 9
     */
29
    public function getYmlAttributes()
30
    {
31
        return $this->attributes();
32
    }
33
34 9
    /**
35
     * @return string
36 9
     */
37
    public function getYml()
38 9
    {
39 9
        $string = '';
40 9
41
        $string .= $this->getYmlStartTag();
42 9
        $string .= $this->getYmlBody();
43
        $string .= $this->getYmlEndTag() . PHP_EOL;
44
45
        return $string;
46
    }
47
48
    /**
49
     * @param array $params
50
     */
51
    public function setParams(array $params)
52
    {
53
54
    }
55
56
    /**
57
     * @param array $pictures
58
     */
59
    public function setPictures(array $pictures)
60
    {
61
62
    }
63
64 9
    /**
65
     * @param array $options
66 9
     */
67 9
    public function setDeliveryOptions(array $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68 9
    {
69 6
70
    }
71 9
72
    /**
73
     * @param $data
74
     * @param null $onValidationError
75
     *
76
     * @return bool
77 9
     * @throws Exception
78
     */
79 9
    public function loadAndValidate($data, $onValidationError = null)
80 9
    {
81 9
        $this->load($data, '');
82 6
83
        if (!$this->validate()) {
84 9
            if (is_callable($onValidationError)) {
85
                $onValidationError($this);
86
                return false;
87
            } else {
88
                throw new Exception('Model values is invalid ' . serialize($this->getErrors()));
89
            }
90 9
        }
91
92 9
        return true;
93
    }
94
95
    /**
96
     * @param $valuesModel
97
     * @param null $onValidationError
98 9
     *
99
     * @return bool
100 9
     * @throws Exception
101 9
     */
102
    public function loadModel($valuesModel, $onValidationError = null)
103 9
    {
104 9
        if (is_array($valuesModel)) {
105 9
            $valuesModel = \yii::createObject($valuesModel);
106 9
        }
107 6
108 6
        $attributes = [];
109
        foreach ($this->attributes() as $attribute) {
110 9
            $methodName = 'get' . ucfirst($attribute);
111
            $attributeValue = $valuesModel->$methodName();
112
113
            $attributes[$attribute] = $attributeValue;
114
        }
115
116
        $this->load($attributes, '');
117 9
118
        if (!$this->validate()) {
119 9
            $e = new Exception('Model ' . get_class($this) . ' values is invalid ' . var_export($this->getErrors(), true) . ' values: ' . var_export($attributes, true));
120
            if (is_callable($onValidationError)) {
121
                $onValidationError($this, $e);
122
                return false;
123
            } else {
124
                throw $e;
125
            }
126 9
        }
127
128 9
        return true;
129 9
    }
130 9
131
    /**
132
     * @return string
133 9
     */
134 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...
135 9
    {
136
        $string = '';
137
        if (static::$tag) {
138
            $string = '<' . static::$tag . $this->getYmlTagProperties() . '>';
139
        }
140
141
        return $string;
142
    }
143
144
    /**
145
     * @return string
146
     */
147 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...
148
    {
149
        $string = '';
150
        if (static::$tag) {
151
            $string .= '</' . static::$tag . '>';
152
        }
153
154
        return $string;
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    protected function getYmlBody()
161
    {
162
        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...
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    protected function getYmlTagProperties()
169
    {
170
        $string = '';
171
        $properties = static::getTagProperties();
172
173
        foreach ($properties as $property) {
174
            $value = $this->getAttributeValue($property);
175
            if ($value !== null) {
176
                $string .= ' ' . $property . '="' . $value . '"';
177
            }
178
        }
179
180
        return $string;
181
    }
182
183
    /**
184
     * @param string $attribute
185
     * @return string
186
     */
187
    protected function getAttributeValue($attribute)
188
    {
189
        return $this->$attribute;
190
    }
191
192
    /**
193
     * @param string $attribute
194
     * @return string
195
     */
196
    protected function getYmlAttribute($attribute)
197
    {
198
        $value = $this->getAttributeValue($attribute);
199
        if ($value === null) {
200
            return '';
201
        }
202
203
204
        if (($key = array_search($attribute, $this->getYmlAttributes())) !== false && is_string($key)) {
205
            $attribute = $key;
206
        }
207
208
        $string = '<' . $attribute . '>' . $value . '</' . $attribute. '>' . PHP_EOL;
209
210
        return $string;
211
    }
212
}
213