Issues (836)

framework/rest/Action.php (1 issue)

Severity
1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\rest;
9
10
use yii\base\InvalidConfigException;
11
use yii\db\ActiveRecordInterface;
12
use yii\web\NotFoundHttpException;
13
14
/**
15
 * Action is the base class for action classes that implement RESTful API.
16
 *
17
 * For more details and usage information on Action, see the [guide article on rest controllers](guide:rest-controllers).
18
 *
19
 * @author Qiang Xue <[email protected]>
20
 * @since 2.0
21
 */
22
class Action extends \yii\base\Action
23
{
24
    /**
25
     * @var string class name of the model which will be handled by this action.
26
     * The model class must implement [[ActiveRecordInterface]].
27
     * This property must be set.
28
     */
29
    public $modelClass;
30
    /**
31
     * @var callable|null a PHP callable that will be called to return the model corresponding
32
     * to the specified primary key value. If not set, [[findModel()]] will be used instead.
33
     * The signature of the callable should be:
34
     *
35
     * ```php
36
     * function ($id, $action) {
37
     *     // $id is the primary key value. If composite primary key, the key values
38
     *     // will be separated by comma.
39
     *     // $action is the action object currently running
40
     * }
41
     * ```
42
     *
43
     * The callable should return the model found, or throw an exception if not found.
44
     */
45
    public $findModel;
46
    /**
47
     * @var callable|null a PHP callable that will be called when running an action to determine
48
     * if the current user has the permission to execute the action. If not set, the access
49
     * check will not be performed. The signature of the callable should be as follows,
50
     *
51
     * ```php
52
     * function ($action, $model = null) {
53
     *     // $model is the requested model instance.
54
     *     // If null, it means no specific model (e.g. IndexAction)
55
     * }
56
     * ```
57
     */
58
    public $checkAccess;
59
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 6
    public function init()
65
    {
66 6
        if ($this->modelClass === null) {
67
            throw new InvalidConfigException(get_class($this) . '::$modelClass must be set.');
68
        }
69
    }
70
71
    /**
72
     * Returns the data model based on the primary key given.
73
     * If the data model is not found, a 404 HTTP exception will be raised.
74
     * @param string $id the ID of the model to be loaded. If the model has a composite primary key,
75
     * the ID must be a string of the primary key values separated by commas.
76
     * The order of the primary key values should follow that returned by the `primaryKey()` method
77
     * of the model.
78
     * @return ActiveRecordInterface the model found
79
     * @throws NotFoundHttpException if the model cannot be found
80
     */
81
    public function findModel($id)
82
    {
83
        if ($this->findModel !== null) {
84
            return call_user_func($this->findModel, $id, $this);
85
        }
86
87
        /** @var ActiveRecordInterface $modelClass */
88
        $modelClass = $this->modelClass;
89
        $keys = $modelClass::primaryKey();
90
        if (count($keys) > 1) {
91
            $values = explode(',', $id);
92
            if (count($keys) === count($values)) {
93
                $model = $modelClass::findOne(array_combine($keys, $values));
94
            }
95
        } elseif ($id !== null) {
0 ignored issues
show
The condition $id !== null is always true.
Loading history...
96
            $model = $modelClass::findOne($id);
97
        }
98
99
        if (isset($model)) {
100
            return $model;
101
        }
102
103
        throw new NotFoundHttpException("Object not found: $id");
104
    }
105
}
106