Skip to content

Commit 0d0f005

Browse files
committed
callable scenarios for actions: create, update, delete
1 parent 134701e commit 0d0f005

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

src/actions/CreateAction.php

+18-5
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ class CreateAction extends JsonApiAction
2929
{
3030
use HasResourceTransformer;
3131
use HasParentAttributes;
32+
3233
/**
3334
* @var array
34-
* * Configuration for attaching relationships
35+
* Configuration for attaching relationships
3536
* Should contains key - relation name and array with
3637
* idType - php type of resource ids for validation
3738
* validator = callback for custom id validation
@@ -44,12 +45,24 @@ class CreateAction extends JsonApiAction
4445
* $relatedModels = Relation::find()->where(['id' => $ids])->andWhere([additional conditions])->all();
4546
* if(count($relatedModels) < $ids) {
4647
* throw new HttpException(422, 'Invalid photos ids');
47-
* }],
48+
* }},
4849
* ]
49-
**/
50+
**/
51+
5052
public $allowedRelations = [];
53+
5154
/**
52-
* @var string the scenario to be assigned to the new model before it is validated and saved.
55+
* @var string|callable
56+
* string - the scenario to be assigned to the model before it is validated and saved.
57+
* callable - a PHP callable that will be executed during the action.
58+
* It must return a string representing the scenario to be assigned to the model before it is validated and saved.
59+
* The signature of the callable should be as follows,
60+
* ```php
61+
* function ($action, $model = null) {
62+
* // $model is the requested model instance.
63+
* // If null, it means no specific model (e.g. CreateAction)
64+
* }
65+
* ```
5366
*/
5467
public $scenario = Model::SCENARIO_DEFAULT;
5568

@@ -98,7 +111,7 @@ public function run()
98111

99112
/* @var $model \yii\db\ActiveRecord */
100113
$model = new $this->modelClass([
101-
'scenario' => $this->scenario,
114+
'scenario' => is_callable($this->scenario) ? call_user_func($this->scenario, $this->id) : $this->scenario,
102115
]);
103116
RelationshipManager::validateRelationships($model, $this->getResourceRelationships(), $this->allowedRelations);
104117
$model->load($this->getResourceAttributes(), '');

src/actions/DeleteAction.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ class DeleteAction extends JsonApiAction
2323
use HasParentAttributes;
2424

2525
/**
26-
* @var string the scenario to be assigned to the new model before it is validated and saved.
26+
* @var string|callable
27+
* string - the scenario to be assigned to the model before it is validated and saved.
28+
* callable - a PHP callable that will be executed during the action.
29+
* It must return a string representing the scenario to be assigned to the model before it is validated and saved.
30+
* The signature of the callable should be as follows,
31+
* ```php
32+
* function ($action, $model = null) {
33+
* // $model is the requested model instance.
34+
* // If null, it means no specific model (e.g. CreateAction)
35+
* }
36+
* ```
2737
*/
2838
public $scenario = Model::SCENARIO_DEFAULT;
2939

@@ -56,7 +66,9 @@ public function run($id):void
5666
throw new ForbiddenHttpException('Update with relationships not supported yet');
5767
}
5868
$model = $this->isParentRestrictionRequired() ? $this->findModelForParent($id) : $this->findModel($id);
59-
$model->setScenario($this->scenario);
69+
$model->setScenario(is_callable($this->scenario) ?
70+
call_user_func($this->scenario, $this->id, $model) : $this->scenario
71+
);
6072
if ($this->checkAccess) {
6173
call_user_func($this->checkAccess, $this->id, $model);
6274
}

src/actions/UpdateAction.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,19 @@ class UpdateAction extends JsonApiAction
5151
* ]
5252
**/
5353
public $allowedRelations = [];
54+
5455
/**
55-
* @var string the scenario to be assigned to the model before it is validated and updated.
56+
* @var string|callable
57+
* string - the scenario to be assigned to the model before it is validated and updated.
58+
* callable - a PHP callable that will be executed during the action.
59+
* It must return a string representing the scenario to be assigned to the model before it is validated and updated.
60+
* The signature of the callable should be as follows,
61+
* ```php
62+
* function ($action, $model = null) {
63+
* // $model is the requested model instance.
64+
* // If null, it means no specific model (e.g. CreateAction)
65+
* }
66+
* ```
5667
*/
5768
public $scenario = Model::SCENARIO_DEFAULT;
5869
/**
@@ -88,7 +99,8 @@ public function run($id):Item
8899
{
89100
/* @var $model ActiveRecord */
90101
$model = $this->isParentRestrictionRequired() ? $this->findModelForParent($id) : $this->findModel($id);
91-
$model->scenario = $this->scenario;
102+
$model->scenario = is_callable($this->scenario) ?
103+
call_user_func($this->scenario, $this->id, $model) : $this->scenario;
92104
if ($this->checkAccess) {
93105
call_user_func($this->checkAccess, $this->id, $model);
94106
}

0 commit comments

Comments
 (0)