Skip to content

callable scenarios for actions: create, update, delete #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/actions/CreateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class CreateAction extends JsonApiAction
{
use HasResourceTransformer;
use HasParentAttributes;

/**
* @var array
* * Configuration for attaching relationships
* Configuration for attaching relationships
* Should contains key - relation name and array with
* idType - php type of resource ids for validation
* validator = callback for custom id validation
Expand All @@ -44,12 +45,23 @@ class CreateAction extends JsonApiAction
* $relatedModels = Relation::find()->where(['id' => $ids])->andWhere([additional conditions])->all();
* if(count($relatedModels) < $ids) {
* throw new HttpException(422, 'Invalid photos ids');
* }],
* }},
* ]
**/
**/

public $allowedRelations = [];

/**
* @var string the scenario to be assigned to the new model before it is validated and saved.
* @var string|callable
* string - the scenario to be assigned to the model before it is validated and saved.
* callable - a PHP callable that will be executed during the action.
* It must return a string representing the scenario to be assigned to the model before it is validated and saved.
* The signature of the callable should be as follows,
* ```php
* function ($action, $model) {
* // $model is the requested model instance.
* }
* ```
*/
public $scenario = Model::SCENARIO_DEFAULT;

Expand Down Expand Up @@ -97,9 +109,10 @@ public function run()
}

/* @var $model \yii\db\ActiveRecord */
$model = new $this->modelClass([
'scenario' => $this->scenario,
]);
$model = new $this->modelClass();
$model->setScenario(is_callable($this->scenario) ?
call_user_func($this->scenario, $this->id, $model) : $this->scenario
);
RelationshipManager::validateRelationships($model, $this->getResourceRelationships(), $this->allowedRelations);
$model->load($this->getResourceAttributes(), '');
if ($this->isParentRestrictionRequired()) {
Expand Down
15 changes: 13 additions & 2 deletions src/actions/DeleteAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ class DeleteAction extends JsonApiAction
use HasParentAttributes;

/**
* @var string the scenario to be assigned to the new model before it is validated and saved.
* @var string|callable
* string - the scenario to be assigned to the model before it is validated and saved.
* callable - a PHP callable that will be executed during the action.
* It must return a string representing the scenario to be assigned to the model before it is validated and saved.
* The signature of the callable should be as follows,
* ```php
* function ($action, $model) {
* // $model is the requested model instance.
* }
* ```
*/
public $scenario = Model::SCENARIO_DEFAULT;

Expand Down Expand Up @@ -56,7 +65,9 @@ public function run($id):void
throw new ForbiddenHttpException('Update with relationships not supported yet');
}
$model = $this->isParentRestrictionRequired() ? $this->findModelForParent($id) : $this->findModel($id);
$model->setScenario($this->scenario);
$model->setScenario(is_callable($this->scenario) ?
call_user_func($this->scenario, $this->id, $model) : $this->scenario
);
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id, $model);
}
Expand Down
15 changes: 13 additions & 2 deletions src/actions/UpdateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,18 @@ class UpdateAction extends JsonApiAction
* ]
**/
public $allowedRelations = [];

/**
* @var string the scenario to be assigned to the model before it is validated and updated.
* @var string|callable
* string - the scenario to be assigned to the model before it is validated and updated.
* callable - a PHP callable that will be executed during the action.
* It must return a string representing the scenario to be assigned to the model before it is validated and updated.
* The signature of the callable should be as follows,
* ```php
* function ($action, $model) {
* // $model is the requested model instance.
* }
* ```
*/
public $scenario = Model::SCENARIO_DEFAULT;
/**
Expand Down Expand Up @@ -88,7 +98,8 @@ public function run($id):Item
{
/* @var $model ActiveRecord */
$model = $this->isParentRestrictionRequired() ? $this->findModelForParent($id) : $this->findModel($id);
$model->scenario = $this->scenario;
$model->scenario = is_callable($this->scenario) ?
call_user_func($this->scenario, $this->id, $model) : $this->scenario;
if ($this->checkAccess) {
call_user_func($this->checkAccess, $this->id, $model);
}
Expand Down
Loading