BaseModel::getAttributeValue()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

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