Completed
Push — master ( edb2b9...da334f )
by Dmitry
13:12
created

src/actions/ValidateFormAction.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * HiPanel core package
4
 *
5
 * @link      https://hipanel.com/
6
 * @package   hipanel-core
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\actions;
12
13
use hipanel\base\Model;
14
use Yii;
15
use yii\base\InvalidRouteException;
16
use yii\helpers\ArrayHelper;
17
use yii\helpers\Html;
18
19
/**
20
 * Class ValidateFormAction.
21
 */
22
class ValidateFormAction extends Action
23
{
24
    public $allowDynamicScenario = true;
25
26
    /**
27
     * @var \Closure|false function used to generate the input ID for the validated field.
28
     * Method signature: `function ($action, $model, $id, $attribute, $errors)`
29
     * Closure MUST return string.
30
     *
31
     * Set this property value to `false`, if input ID must not contain sequential index
32
     */
33
    public $validatedInputId = null;
34
35
    public function init()
36
    {
37
        $scenario = Yii::$app->request->get('scenario');
38
39
        if ($scenario !== null && $this->allowDynamicScenario) {
40
            $this->scenario = $scenario;
41
        }
42
    }
43
44
    public function getModel($options = [])
45
    {
46
        if ($this->model === null) {
0 ignored issues
show
The property model does not exist on object<hipanel\actions\ValidateFormAction>. 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...
47
            return $this->controller->newModel(array_merge([
48
                'scenario' => $this->scenario,
49
            ], $options));
50
        } else {
51
            return Yii::createObject(ArrayHelper::merge(['scenario' => $this->getScenario()], $this->model));
0 ignored issues
show
The property model does not exist on object<hipanel\actions\ValidateFormAction>. 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...
52
        }
53
    }
54
55
    public function run()
56
    {
57
        if (Yii::$app->request->isPost) {
58
            $this->loadCollection();
59
60
            return $this->controller->renderJson($this->validateMultiple());
61
        }
62
63
        throw new InvalidRouteException('Must be POST request');
64
    }
65
66
    public function validateMultiple()
67
    {
68
        $result = [];
69
        foreach ($this->collection->models as $i => $model) {
70
            /* @var Model $model */
71
            $model->validate();
72
            foreach ($model->getErrors() as $attribute => $errors) {
73
                if ($this->validatedInputId instanceof \Closure) {
74
                    $id = call_user_func($this->validatedInputId, $this, $model, $i, $attribute, $errors);
75
                } elseif ($this->validatedInputId === false) {
76
                    $id = Html::getInputId($model, $attribute);
77
                } else {
78
                    $id = Html::getInputId($model, "[$i]" . $attribute);
79
                }
80
                $result[$id] = $errors;
81
            }
82
        }
83
84
        return $result;
85
    }
86
87
    /**
88
     * Sets the $model in the [[collection]].
89
     *
90
     * @param Model $model
91
     */
92
    public function setModel($model)
93
    {
94
        $this->collection->setModel($model);
95
    }
96
}
97