Skip to content

Commit e121018

Browse files
committed
scenario check type string first
1 parent 2c2175e commit e121018

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/actions/CreateAction.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use yii\base\Model;
1717
use yii\db\ActiveRecordInterface;
1818
use yii\helpers\Url;
19+
use yii\base\InvalidConfigException;
1920
use yii\web\ServerErrorHttpException;
2021
use function array_keys;
2122
use function call_user_func;
@@ -111,9 +112,16 @@ public function run()
111112

112113
/* @var $model \yii\db\ActiveRecord */
113114
$model = new $this->modelClass();
114-
$model->setScenario(is_callable($this->scenario) ?
115-
call_user_func($this->scenario, $this->id, $model) : $this->scenario
116-
);
115+
116+
if (is_string($this->scenario)) {
117+
$scenario = $this->scenario;
118+
} elseif (is_callable($this->scenario)) {
119+
$scenario = call_user_func($this->scenario, $this->id, $model);
120+
} else {
121+
throw new InvalidConfigException('The "scenario" property must be defined either as a string or as a callable.');
122+
}
123+
$model->setScenario($scenario);
124+
117125
RelationshipManager::validateRelationships($model, $this->getResourceRelationships(), $this->allowedRelations);
118126
$model->load($this->getResourceAttributes(), '');
119127
if ($this->isParentRestrictionRequired()) {

src/actions/DeleteAction.php

+12-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Closure;
1111
use Yii;
1212
use yii\base\Model;
13+
use yii\base\InvalidConfigException;
1314
use yii\web\ForbiddenHttpException;
1415
use yii\web\ServerErrorHttpException;
1516

@@ -65,12 +66,20 @@ public function run($id):void
6566
throw new ForbiddenHttpException('Update with relationships not supported yet');
6667
}
6768
$model = $this->isParentRestrictionRequired() ? $this->findModelForParent($id) : $this->findModel($id);
68-
$model->setScenario(is_callable($this->scenario) ?
69-
call_user_func($this->scenario, $this->id, $model) : $this->scenario
70-
);
69+
70+
if (is_string($this->scenario)) {
71+
$scenario = $this->scenario;
72+
} elseif (is_callable($this->scenario)) {
73+
$scenario = call_user_func($this->scenario, $this->id, $model);
74+
} else {
75+
throw new InvalidConfigException('The "scenario" property must be defined either as a string or as a callable.');
76+
}
77+
$model->setScenario($scenario);
78+
7179
if ($this->checkAccess) {
7280
call_user_func($this->checkAccess, $this->id, $model);
7381
}
82+
7483
if ($model->delete() === false) {
7584
throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
7685
}

src/actions/UpdateAction.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Yii;
1616
use yii\base\Model;
1717
use yii\db\ActiveRecord;
18+
use yii\base\InvalidConfigException;
1819
use yii\web\ServerErrorHttpException;
1920

2021
/**
@@ -98,11 +99,20 @@ public function run($id):Item
9899
{
99100
/* @var $model ActiveRecord */
100101
$model = $this->isParentRestrictionRequired() ? $this->findModelForParent($id) : $this->findModel($id);
101-
$model->scenario = is_callable($this->scenario) ?
102-
call_user_func($this->scenario, $this->id, $model) : $this->scenario;
102+
103+
if (is_string($this->scenario)) {
104+
$scenario = $this->scenario;
105+
} elseif (is_callable($this->scenario)) {
106+
$scenario = call_user_func($this->scenario, $this->id, $model);
107+
} else {
108+
throw new InvalidConfigException('The "scenario" property must be defined either as a string or as a callable.');
109+
}
110+
$model->setScenario($scenario);
111+
103112
if ($this->checkAccess) {
104113
call_user_func($this->checkAccess, $this->id, $model);
105114
}
115+
106116
$originalModel = clone $model;
107117
RelationshipManager::validateRelationships($model, $this->getResourceRelationships(), $this->allowedRelations);
108118
if (empty($this->getResourceAttributes()) && $this->hasResourceRelationships()) {

0 commit comments

Comments
 (0)