EmbeddedModelValidator::validateAttribute()   B
last analyzed

Complexity

Conditions 7
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 5
nop 2
dl 0
loc 22
ccs 0
cts 18
cp 0
crap 56
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * SEOmatic plugin for Craft CMS 3.x
4
 *
5
 * A turnkey SEO implementation for Craft CMS that is comprehensive, powerful,
6
 * and flexible
7
 *
8
 * @link      https://nystudio107.com
9
 * @copyright Copyright (c) 2017 nystudio107
10
 */
11
12
namespace nystudio107\seomatic\validators;
13
14
use Craft;
15
16
use yii\base\Model;
17
use yii\validators\Validator;
18
19
/**
20
 * @author    nystudio107
21
 * @package   Seomatic
22
 * @since     3.0.0
23
 */
24
class EmbeddedModelValidator extends Validator
25
{
26
    // Public Methods
27
    // =========================================================================
28
29
    /**
30
     * @inheritdoc
31
     */
32
    public function validateAttribute($model, $attribute)
33
    {
34
        /** @var Model $model */
35
        $value = $model->$attribute;
36
37
        if (!empty($value) && \is_object($value) && $value instanceof Model) {
38
            /** @var Model $value */
39
            if (!$value->validate()) {
40
                $errors = $value->getErrors();
41
                foreach ($errors as $attributeError => $valueErrors) {
42
                    /** @var array $valueErrors */
43
                    foreach ($valueErrors as $valueError) {
44
                        $model->addError(
45
                            $attribute,
46
                            Craft::t('seomatic', 'Object failed to validate')
47
                            . '-' . $attributeError . ' - ' . $valueError
48
                        );
49
                    }
50
                }
51
            }
52
        } else {
53
            $model->addError($attribute, Craft::t('seomatic', 'Is not a Model object.'));
54
        }
55
    }
56
}
57