Completed
Push — master ( 871cd8...ab1e03 )
by Kirill
03:44
created

BaseModel::getYmlAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 4
cp 0.75
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1.0156
1
<?php
2
namespace pastuhov\ymlcatalog\models;
3
4
use pastuhov\ymlcatalog\EscapedAttributes;
5
use yii\base\Model;
6
use yii\base\Exception;
7
8
/**
9
 * Base model.
10
 *
11
 * @package pastuhov\ymlcatalog\models
12
 */
13
class BaseModel extends Model
14
{
15
    /**
16
     * @var string
17
     */
18
    public static $tag;
19
20
    /**
21
     * @var string[]
22
     */
23
    public static $tagProperties = [];
24
25
    /**
26
     * Список атрибутов модели значений для фильтрации согласно требованиям Яндекс Маркета
27
     *
28
     * @var string[]
29
     */
30
    protected $escapedAttributes = [];
31
32
    /**
33
     * @return string[]
34
     */
35 5
    public function getYmlAttributes()
36 5
    {
37 5
        return $this->attributes();
38
    }
39
40
    /**
41
     * @return string
42
     */
43 5
    public function getYml()
44
    {
45 5
        $string = '';
46
47 5
        $string .= $this->getYmlStartTag();
48 5
        $string .= $this->getYmlBody();
49 5
        $string .= $this->getYmlEndTag() . PHP_EOL;
50
51 5
        return $string;
52
    }
53
54
    /**
55
     * @param array $params
56
     */
57
    public function setParams(array $params)
58
    {
59
60
    }
61
62
    /**
63
     * @param array $pictures
64
     */
65
    public function setPictures(array $pictures)
66
    {
67
68
    }
69
70
    /**
71
     * @param array $options
72
     */
73
    public function setDeliveryOptions(array $options)
74
    {
75
76
    }
77
78
    /**
79
     * @param $valuesModel
80
     * @param null $onValidationError
81
     *
82
     * @return bool
83
     * @throws Exception
84
     */
85 5
    public function loadModel($valuesModel, $onValidationError = null)
86
    {
87 5
        if ($valuesModel instanceof EscapedAttributes) {
88 5
            $escapedAttributes = $valuesModel->getEscapedAttributes();
89 5
            if (!is_array($escapedAttributes)) {
90
                throw new Exception('Escaped attributes is not array');
91
            }
92
93 5
            $this->escapedAttributes = $escapedAttributes;
94 5
        }
95
96 5
        $attributes = [];
97 5
        foreach ($this->attributes() as $attribute) {
98 5
            $methodName = 'get' . ucfirst($attribute);
99 5
            $attributeValue = $valuesModel->$methodName();
100
101 5
            $attributes[$attribute] = $attributeValue;
102 5
        }
103
104 5
        $this->load($attributes, '');
105
106 5
        if (!$this->validate()) {
107 5
            if (is_callable($onValidationError)) {
108 5
                $onValidationError($this);
109 5
                return false;
110
            } else {
111
                throw new Exception('Model values is invalid ' . serialize($this->getErrors()));
112
            }
113
        }
114
115 5
        return true;
116
    }
117
118
    /**
119
     * @return string
120
     */
121 5 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...
122
    {
123 5
        $string = '';
124 5
        if (static::$tag) {
125 5
            $string = '<' . static::$tag . $this->getYmlTagProperties() . '>';
126 5
        }
127
128 5
        return $string;
129
    }
130
131
    /**
132
     * @return string
133
     */
134 5 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...
135
    {
136 5
        $string = '';
137 5
        if (static::$tag) {
138 5
            $string .= '</' . static::$tag . '>';
139 5
        }
140
141 5
        return $string;
142
    }
143
144
    /**
145
     * @return string
146
     */
147 5
    protected function getYmlBody()
148
    {
149 5
        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...
150
    }
151
152
    /**
153
     * @return string
154
     */
155 5
    protected function getYmlTagProperties()
156
    {
157 5
        $string = '';
158 5
        $properties = static::$tagProperties;
159
160 5
        foreach ($properties as $property) {
161 5
            $value = $this->getAttributeValue($property);
162 5
            if ($value !== null) {
163 5
                $string .= ' ' . $property . '="' . $value . '"';
164 5
            }
165 5
        }
166
167 5
        return $string;
168
    }
169
170
    /**
171
     * @param string $attribute
172
     * @return string
173
     */
174 5
    protected function getAttributeValue($attribute)
175
    {
176 5
        if ($this->$attribute !== null) {
177 5
            $result = trim($this->$attribute);
178 5
            if (in_array($attribute, $this->escapedAttributes)) {
179 4
                $result = htmlspecialchars($result);
180 4
            }
181
182 5
            return $result;
183
        }
184 5
    }
185
186
    /**
187
     * @param string $attribute
188
     * @return string
189
     */
190 5
    protected function getYmlAttribute($attribute)
191
    {
192 5
        $value = $this->getAttributeValue($attribute);
193 5
        if ($value === null) {
194 5
            return '';
195
        }
196
197 5
        $string = '<' . $attribute . '>' . $value . '</' . $attribute. '>' . PHP_EOL;
198
199 5
        return $string;
200
    }
201
}
202