From 618c18744293e5ff4e210e96a3e5722e88c1ce10 Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Wed, 7 Nov 2018 00:22:03 -0600 Subject: [PATCH 1/6] to be reverted, yii2 roa branch alias --- composer.json | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 9af2285..f28a571 100644 --- a/composer.json +++ b/composer.json @@ -26,15 +26,17 @@ { "type": "composer", "url": "https://asset-packagist.org" + }, + { + "type": "vcs", + "url": "git@github.com:tecnocen-com/yii2-roa.git" } ], "require": { - "php": "^7.0", - "ext-curl": "*", - "tecnocen/yii2-roa": "~0.3.1", + "php": "^7.1", + "tecnocen/yii2-roa": "dev-typecast as 0.5.x-dev", "tecnocen/yii2-rmdb": "*", - "yii2tech/ar-softdelete": "*", - "yiisoft/yii2": "~2.0.10" + "yii2tech/ar-softdelete": "*" }, "require-dev": { "codeception/base": "^2.2.11", From 56f094ef80143c805ea51d46332d930f466086d2 Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Wed, 7 Nov 2018 23:11:23 -0600 Subject: [PATCH 2/6] rewrite tests to use typecast --- tests/_app/api/models/Credit.php | 38 ++++++++++--------- tests/_app/api/models/CreditAssignment.php | 32 +++++++--------- tests/_app/api/models/CreditSearch.php | 10 +++-- tests/_app/api/models/CreditWorklog.php | 35 ++++++----------- tests/_app/api/modules/Version.php | 8 ++-- .../resources/CreditAssignmentResource.php | 5 +-- tests/_app/api/resources/CreditResource.php | 5 +-- .../api/resources/CreditWorklogResource.php | 5 +-- tests/_app/models/Credit.php | 6 +-- tests/_app/models/CreditAssignment.php | 9 ++++- tests/_app/models/CreditWorklog.php | 2 +- tests/api/WorkflowCest.php | 8 ++-- 12 files changed, 82 insertions(+), 81 deletions(-) diff --git a/tests/_app/api/models/Credit.php b/tests/_app/api/models/Credit.php index f39eb16..d69ad2f 100644 --- a/tests/_app/api/models/Credit.php +++ b/tests/_app/api/models/Credit.php @@ -2,24 +2,31 @@ namespace app\api\models; -use tecnocen\roa\behaviors\Slug; -use yii\web\Linkable; -use yii\web\NotFoundHttpException; +use app\models as base; +use tecnocen\roa\hal\Contract; +use tecnocen\roa\hal\ContractTrait; /** * ROA contract to handle credit records. - * - * @method string[] getSlugLinks() - * @method string getSelfLink() */ -class Credit extends \app\models\Credit implements Linkable +class Credit extends base\Credit implements Contract { - protected function assignmentClass() + use ContractTrait { + getLinks as getContractLinks; + } + + /** + * @inheritdoc + */ + protected function assignmentClass(): string { return CreditAssignment::class; } - protected function workLogClass() + /** + * @inheritdoc + */ + protected function workLogClass(): string { return CreditWorklog::class; } @@ -27,14 +34,11 @@ protected function workLogClass() /** * @inheritdoc */ - public function behaviors() + protected function slugBehaviorConfig(): array { - return array_merge(parent::behaviors(), [ - 'slug' => [ - 'class' => Slug::class, - 'resourceName' => 'credit', - ], - ]); + return [ + 'resourceName' => 'credit', + ]; } /** @@ -42,7 +46,7 @@ public function behaviors() */ public function getLinks() { - return array_merge($this->getSlugLinks(), [ + return array_merge($this->getContractLinks(), [ 'worklog' => $this->getSelfLink() . '/worklog', ]); } diff --git a/tests/_app/api/models/CreditAssignment.php b/tests/_app/api/models/CreditAssignment.php index cbe57b0..2d0f62e 100644 --- a/tests/_app/api/models/CreditAssignment.php +++ b/tests/_app/api/models/CreditAssignment.php @@ -2,37 +2,33 @@ namespace app\api\models; -use tecnocen\roa\behaviors\Slug; -use yii\web\Linkable; -use yii\web\NotFoundHttpException; +use app\models as base; +use tecnocen\roa\hal\Contract; +use tecnocen\roa\hal\ContractTrait; /** - * ROA contract to handle credit_worklog records. - * - * @method string[] getSlugLinks() - * @method string getSelfLink() + * ROA contract to handle credit_assignment records. */ -class CreditAssignment extends \app\models\CreditAssignment implements Linkable +class CreditAssignment extends base\CreditAssignment implements Contract { + use ContractTrait; + /** * @inheritdoc */ - public function behaviors() + protected function processClass(): string { - return array_merge(parent::behaviors(), [ - 'slug' => [ - 'class' => Slug::class, - 'resourceName' => 'assignment', - 'parentSlugRelation' => 'process', - ], - ]); + return Credit::class; } /** * @inheritdoc */ - public function getLinks() + protected function slugBehaviorConfig(): array { - return $this->getSlugLinks(); + return [ + 'resourceName' => 'assignment', + 'parentSlugRelation' => 'process', + ]; } } diff --git a/tests/_app/api/models/CreditSearch.php b/tests/_app/api/models/CreditSearch.php index b80727c..1120a08 100644 --- a/tests/_app/api/models/CreditSearch.php +++ b/tests/_app/api/models/CreditSearch.php @@ -2,9 +2,10 @@ namespace app\api\models; +use tecnocen\roa\ResourceSearch; use yii\data\ActiveDataProvider; -class CreditSearch extends Credit implements \tecnocen\roa\ResourceSearch +class CreditSearch extends Credit implements ResourceSearch { /** * @inhertidoc @@ -24,12 +25,15 @@ public function rules() /** * @inhertidoc */ - public function search(array $params, $formName = '') - { + public function search( + array $params, + ?string $formName = '' + ): ?ActiveDataProvider { $this->load($params, $formName); if (!$this->validate()) { return null; } + $class = get_parent_class(); return new ActiveDataProvider([ 'query' => $class::find()->andFilterWhere([ diff --git a/tests/_app/api/models/CreditWorklog.php b/tests/_app/api/models/CreditWorklog.php index 383b67d..e47425e 100644 --- a/tests/_app/api/models/CreditWorklog.php +++ b/tests/_app/api/models/CreditWorklog.php @@ -2,44 +2,33 @@ namespace app\api\models; -use tecnocen\roa\behaviors\Slug; -use yii\web\Linkable; -use yii\web\NotFoundHttpException; +use app\models as base; +use tecnocen\roa\hal\Contract; +use tecnocen\roa\hal\ContractTrait; /** * ROA contract to handle credit_worklog records. - * - * @method string[] getSlugLinks() - * @method string getSelfLink() */ -class CreditWorklog extends \app\models\CreditWorklog implements Linkable +class CreditWorklog extends base\CreditWorklog implements Contract { - protected function processClass() - { - return Credit::class; - } + use ContractTrait; /** * @inheritdoc */ - public function behaviors() + protected function processClass(): string { - return array_merge(parent::behaviors(), [ - 'slug' => [ - 'class' => Slug::class, - 'resourceName' => 'worklog', - 'parentSlugRelation' => 'process' - ], - ]); + return Credit::class; } /** * @inheritdoc */ - public function getLinks() + protected function slugBehaviorConfig(): array { - return array_merge($this->getSlugLinks(), [ - 'creditWorklogs' => $this->getSelfLink() . '/worklog', - ]); + return [ + 'resourceName' => 'worklog', + 'parentSlugRelation' => 'process' + ]; } } diff --git a/tests/_app/api/modules/Version.php b/tests/_app/api/modules/Version.php index e357c4c..b0cc0dc 100644 --- a/tests/_app/api/modules/Version.php +++ b/tests/_app/api/modules/Version.php @@ -2,9 +2,11 @@ namespace app\api\modules; -use app\api\resources\CreditResource; -use app\api\resources\CreditWorklogResource; -use app\api\resources\CreditAssignmentResource; +use app\api\resources\{ + CreditResource, + CreditWorklogResource, + CreditAssignmentResource +}; class Version extends \tecnocen\roa\modules\ApiVersion { diff --git a/tests/_app/api/resources/CreditAssignmentResource.php b/tests/_app/api/resources/CreditAssignmentResource.php index e43403a..4d8a2c6 100644 --- a/tests/_app/api/resources/CreditAssignmentResource.php +++ b/tests/_app/api/resources/CreditAssignmentResource.php @@ -2,15 +2,14 @@ namespace app\api\resources; -use Yii; -use yii\web\NotFoundHttpException; use app\api\models\CreditAssignment; +use tecnocen\roa\controllers\Resource; /** * CRUD resource for `Credit Assignment` records * @author Carlos (neverabe) Llamosas */ -class CreditAssignmentResource extends \tecnocen\roa\controllers\OAuth2Resource +class CreditAssignmentResource extends Resource { /** * @inheritdoc diff --git a/tests/_app/api/resources/CreditResource.php b/tests/_app/api/resources/CreditResource.php index 204da03..cf6b802 100644 --- a/tests/_app/api/resources/CreditResource.php +++ b/tests/_app/api/resources/CreditResource.php @@ -2,16 +2,15 @@ namespace app\api\resources; -use Yii; -use yii\web\NotFoundHttpException; use app\api\models\Credit; use app\api\models\CreditSearch; +use tecnocen\roa\controllers\Resource; /** * CRUD resource for `Credit` records * @author Carlos (neverabe) Llamosas */ -class CreditResource extends \tecnocen\roa\controllers\OAuth2Resource +class CreditResource extends Resource { /** * @inheritdoc diff --git a/tests/_app/api/resources/CreditWorklogResource.php b/tests/_app/api/resources/CreditWorklogResource.php index 8ca3706..652f414 100644 --- a/tests/_app/api/resources/CreditWorklogResource.php +++ b/tests/_app/api/resources/CreditWorklogResource.php @@ -2,15 +2,14 @@ namespace app\api\resources; -use Yii; -use yii\web\NotFoundHttpException; use app\api\models\CreditWorklog; +use tecnocen\roa\controllers\Resource; /** * CRUD resource for `Credit Worklog` records * @author Carlos (neverabe) Llamosas */ -class CreditWorklogResource extends \tecnocen\roa\controllers\OAuth2Resource +class CreditWorklogResource extends Resource { /** * @inheritdoc diff --git a/tests/_app/models/Credit.php b/tests/_app/models/Credit.php index 243a4da..04535a1 100644 --- a/tests/_app/models/Credit.php +++ b/tests/_app/models/Credit.php @@ -21,17 +21,17 @@ public static function tableName() return '{{%credit}}'; } - protected function assignmentClass() + protected function assignmentClass(): string { return CreditAssignment::class; } - protected function workLogClass() + protected function workLogClass():string { return CreditWorklog::class; } - public function getWorkflowId() + public function getWorkflowId(): int { return $this->workflow_id; } diff --git a/tests/_app/models/CreditAssignment.php b/tests/_app/models/CreditAssignment.php index ad74621..817ebab 100644 --- a/tests/_app/models/CreditAssignment.php +++ b/tests/_app/models/CreditAssignment.php @@ -4,11 +4,18 @@ class CreditAssignment extends \tecnocen\workflow\models\Assignment { + /** + * @inheritdoc + */ public static function tableName() { return '{{%credit_assignment}}'; } - protected function processClass() + + /** + * @inheritdoc + */ + protected function processClass(): string { return Credit::class; } diff --git a/tests/_app/models/CreditWorklog.php b/tests/_app/models/CreditWorklog.php index bb8c6b9..8ef2fb2 100644 --- a/tests/_app/models/CreditWorklog.php +++ b/tests/_app/models/CreditWorklog.php @@ -9,7 +9,7 @@ public static function tableName() return '{{%credit_worklog}}'; } - protected function processClass() + protected function processClass(): string { return Credit::class; } diff --git a/tests/api/WorkflowCest.php b/tests/api/WorkflowCest.php index 94c494a..0d64065 100644 --- a/tests/api/WorkflowCest.php +++ b/tests/api/WorkflowCest.php @@ -106,18 +106,20 @@ protected function viewDataProvider() 'stages' => [ ['id' => 1], ], + 'totalStages' => 3, ], ], ], 'field total stages' => [ 'urlParams' => [ 'id' => '1', - 'fields' => 'id,name,totalStages' + 'fields' => 'id,name' ], 'httpCode' => HttpCode::OK, 'response' => [ - 'totalStages' => 3, - ] + 'id' => 1, + 'name' => 'workflow 1', + ], ], ]; } From 9df18f3fa10f12f65662eafcf560e16524bbf728 Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Wed, 7 Nov 2018 23:11:38 -0600 Subject: [PATCH 3/6] typecast refactorization --- src/models/Assignment.php | 8 +- src/models/Process.php | 74 ++++++++++--------- src/models/Stage.php | 21 +++--- src/models/Transition.php | 18 +++-- src/models/TransitionPermission.php | 14 ++-- src/models/WorkLog.php | 7 +- src/models/Workflow.php | 12 +-- src/roa/models/Stage.php | 55 +++----------- src/roa/models/StageSearch.php | 10 ++- src/roa/models/Transition.php | 34 ++++----- src/roa/models/TransitionPermission.php | 40 +++------- src/roa/models/TransitionPermissionSearch.php | 12 +-- src/roa/models/TransitionSearch.php | 6 +- src/roa/models/Workflow.php | 71 +++++------------- src/roa/models/WorkflowSearch.php | 11 ++- src/roa/modules/Version.php | 10 ++- src/roa/resources/PermissionResource.php | 4 +- src/roa/resources/StageResource.php | 9 ++- src/roa/resources/TransitionResource.php | 6 +- src/roa/resources/WorkflowResource.php | 6 +- 20 files changed, 185 insertions(+), 243 deletions(-) diff --git a/src/models/Assignment.php b/src/models/Assignment.php index 67b2c89..aa6c636 100644 --- a/src/models/Assignment.php +++ b/src/models/Assignment.php @@ -4,6 +4,7 @@ use tecnocen\rmdb\models\Pivot; use Yii; +use yii\db\ActiveQuery; /** * @property int $process_id @@ -16,7 +17,7 @@ abstract class Assignment extends Pivot /** * @return string class name for the process this worklog is attached to. */ - protected abstract function processClass(); + protected abstract function processClass(): string; /** * @inheritdoc @@ -65,10 +66,11 @@ public function init() } parent::init(); } + /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getProcess() + public function getProcess(): ActiveQuery { return $this->hasOne($this->processClass(), ['id' => 'process_id']); } diff --git a/src/models/Process.php b/src/models/Process.php index 0046589..63f4b88 100644 --- a/src/models/Process.php +++ b/src/models/Process.php @@ -4,6 +4,7 @@ use tecnocen\rmdb\models\Entity; use yii\base\InvalidConfigException; +use yii\db\ActiveQuery; /** * Model class for process which change stages depending on a worklow @@ -32,7 +33,7 @@ abstract class Process extends Entity * @return string full class name of the class to be used for the relation * `getWorkflow()`. */ - public function workflowClass() + public function workflowClass(): string { return Workflow::class; } @@ -41,18 +42,18 @@ public function workflowClass() * @return string full class name of the class to be used to store the * assignment records. */ - protected abstract function assignmentClass(); + protected abstract function assignmentClass(): string; /** * @return string full class name of the class to be used to store the * WorkLog records. */ - protected abstract function WorkLogClass(); + protected abstract function workLogClass(): string; /** * @return int the id of the workflow this process belongs to. */ - public abstract function getWorkflowId(); + public abstract function getWorkflowId(): int; /** * Determines if the current process record has the need of an initial @@ -61,7 +62,7 @@ public abstract function getWorkflowId(); * * @return bool whether the initial worklog was autogenerated. */ - private function hasInitialWorkLog() + private function hasInitialWorkLog(): bool { if (!$this->autogenerateInitialWorklog || !$this->isNewRecord) { return false; @@ -92,10 +93,10 @@ public function load($data, $formName = null) /** * Whether the user is asigned to the process. * - * @param int $userId [description] - * @return boolean + * @param ?int $userId + * @return bool */ - public function userAssigned($userId) + public function userAssigned(?int $userId): bool { return !$this->getAssignments()->exists() || $this->getAssignments() ->andWhere(['user_id' => $userId]); @@ -151,9 +152,9 @@ public function afterSave($insert, $changedAttributes) */ public function init() { - if (!is_subclass_of($this->WorkLogClass(), WorkLog::class)) { + if (!is_subclass_of($this->workLogClass(), WorkLog::class)) { throw new InvalidConfigException( - static::class . '::WorkLogClass() must extend ' + static::class . '::workLogClass() must extend ' . WorkLog::class ); } @@ -169,7 +170,7 @@ public function init() /** * @return Workflow */ - public function getWorkflow() + public function getWorkflow(): Workflow { $workflowClass = $this->workflowClass(); @@ -177,9 +178,9 @@ public function getWorkflow() } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getAssignments() + public function getAssignments(): ActiveQuery { return $this->hasMany($this->assignmentClass(), [ 'process_id' => 'id', @@ -187,27 +188,27 @@ public function getAssignments() } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getWorkLogs() + public function getWorkLogs(): ActiveQuery { - return $this->hasMany($this->WorkLogClass(), [ + return $this->hasMany($this->workLogClass(), [ 'process_id' => 'id', ])->inverseOf('process'); } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery * @see https://dev.mysql.com/doc/refman/5.7/en/example-maximum-column-group-row.html */ - public function getActiveWorkLog() + public function getActiveWorkLog(): ActiveQuery { $query = $this->getWorkLogs()->alias('WorkLog'); $query->multiple = false; - $WorkLogClass = $this->WorkLogClass(); + $workLogClass = $this->workLogClass(); return $query->andWhere([ - 'id' => $WorkLogClass::find() + 'id' => $workLogClass::find() ->select(['MAX(id)']) ->alias('WorkLog_groupwise') ->andWhere('WorkLog.process_id = WorkLog_groupwise.process_id') @@ -222,29 +223,36 @@ public function getActiveWorkLog() * to create said WorkLog. * @param bool $runValidation */ - public function flow(&$WorkLog, $runValidation = true) + public function flow(&$workLog, bool $runValidation = true) { - $WorkLog = $this->ensureWorkLog($WorkLog); - $WorkLog->scenario = WorkLog::SCENARIO_FLOW; + $workLog = $this->ensureWorkLog($workLog); + $workLog->scenario = WorkLog::SCENARIO_FLOW; - return $WorkLog->save($runValidation); + return $workLog->save($runValidation); } - private function ensureWorkLog($WorkLog) + /** + * Ensures that the provided parameter is either an array to create a valid + * instance of the `workLogClass()` class. + * + * @param array|WorkLog $workLog + * @return WorkLog extending the class defined in `workLogClass()` + */ + private function ensureWorkLog($workLog): WorkLog { - $WorkLogClass = $this->WorkLogClass(); - if (is_array($WorkLog)) { - $WorkLog = new $WorkLogClass($WorkLog); - } elseif (!$WorkLog instanceof $WorkLogClass) { + $workLogClass = $this->workLogClass(); + if (is_array($workLog)) { + $workLog = new $workLogClass($workLog); + } elseif (!$workLog instanceof $workLogClass) { throw new InvalidParamException( - "Parameter must be an instance of {$WorkLogClass} or an " + "Parameter must be an instance of {$workLogClass} or an " . 'array to configure an instance' ); } - $WorkLog->process_id = $this->id; - $WorkLog->populateRelation('process', $this); + $workLog->process_id = $this->id; + $workLog->populateRelation('process', $this); - return $WorkLog; + return $workLog; } } diff --git a/src/models/Stage.php b/src/models/Stage.php index 692fbe3..0a57fa8 100644 --- a/src/models/Stage.php +++ b/src/models/Stage.php @@ -1,6 +1,9 @@ hasOne( $this->workflowClass, @@ -100,9 +103,9 @@ public function getWorkflow() } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getTransitions() + public function getTransitions(): ActiveQuery { return $this->hasMany( $this->transitionClass, @@ -111,9 +114,9 @@ public function getTransitions() } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getDetailTransitions() + public function getDetailTransitions(): ActiveQuery { $query = $this->getTransitions(); $query->multiple = false; @@ -129,15 +132,15 @@ public function getDetailTransitions() /** * @return int */ - public function getTotalTransitions() + public function getTotalTransitions(): int { return (int)$this->detailTransitions['totalTransitions']; } /** - * @return \yii\db\ActiveQuery sibling stages for the same workflow + * @return ActiveQuery sibling stages for the same workflow */ - public function getSiblings() + public function getSiblings(): ActiveQuery { return $this->hasMany(static::class, ['workflow_id' => 'workflow_id']) ->alias('siblings'); diff --git a/src/models/Transition.php b/src/models/Transition.php index 4b03229..2275339 100644 --- a/src/models/Transition.php +++ b/src/models/Transition.php @@ -3,6 +3,7 @@ namespace tecnocen\workflow\models; use Yii; +use yii\db\ActiveQuery; /** * Model class for table `{{%workflow_transition}}` @@ -126,13 +127,14 @@ public function rules() * * @param int $userId * @param Process $process - * @return boolean + * @return bool */ - public function userCan($userId, Process $process) + public function userCan(?int $userId, Process $process): bool { if (!$this->getPermissions()->exists()) { return true; } + $authManager = Yii::$app->authManager; foreach ($this->permissions as $permission) { @@ -160,25 +162,25 @@ public function attributeLabels() } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getSourceStage() + public function getSourceStage(): ActiveQuery { return $this->hasOne($this->stageClass, ['id' => 'source_stage_id']); } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getTargetStage() + public function getTargetStage(): ActiveQuery { return $this->hasOne($this->stageClass, ['id' => 'target_stage_id']); } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getPermissions() + public function getPermissions(): ActiveQuery { return $this->hasMany( $this->permissionClass, diff --git a/src/models/TransitionPermission.php b/src/models/TransitionPermission.php index 12a9351..1deb3b6 100644 --- a/src/models/TransitionPermission.php +++ b/src/models/TransitionPermission.php @@ -2,6 +2,8 @@ namespace tecnocen\workflow\models; +use yii\db\ActiveQuery; + /** * Model class for table `{{%workflow_transition}}` * @@ -116,25 +118,25 @@ public function attributeLabels() } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getSourceStage() + public function getSourceStage(): ActiveQuery { return $this->hasOne($this->stageClass, ['id' => 'source_stage_id']); } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getTargetStage() + public function getTargetStage(): ActiveQuery { return $this->hasOne($this->stageClass, ['id' => 'target_stage_id']); } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getTransition() + public function getTransition(): ActiveQuery { return $this->hasOne( $this->transitionClass, diff --git a/src/models/WorkLog.php b/src/models/WorkLog.php index d01ffc6..a25f4d4 100644 --- a/src/models/WorkLog.php +++ b/src/models/WorkLog.php @@ -4,6 +4,7 @@ use tecnocen\rmdb\models\Pivot; use Yii; +use yii\db\ActiveQuery; use yii\web\ForbiddenHttpException; use yii\web\BadRequestHttpException; @@ -22,7 +23,7 @@ abstract class WorkLog extends Pivot /** * @return string class name for the process this worklog is attached to. */ - protected abstract function processClass(); + protected abstract function processClass(): string; /** * @inheritdoc @@ -138,9 +139,9 @@ public function init() parent::init(); } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getProcess() + public function getProcess(): ActiveQuery { return $this->hasOne($this->processClass(), ['id' => 'process_id']); } diff --git a/src/models/Workflow.php b/src/models/Workflow.php index 176dc88..e9b53f8 100644 --- a/src/models/Workflow.php +++ b/src/models/Workflow.php @@ -2,6 +2,8 @@ namespace tecnocen\workflow\models; +use yii\db\ActiveQuery; + /** * Model class for table `{{%workflow}}` * @@ -58,18 +60,18 @@ public function attributeLabels() } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getStages() + public function getStages(): ActiveQuery { return $this->hasMany($this->stageClass, ['workflow_id' => 'id']) ->inverseOf('workflow'); } /** - * @return \yii\db\ActiveQuery + * @return ActiveQuery */ - public function getDetailStages() + public function getDetailStages(): ActiveQuery { $query = $this->getStages(); $query->multiple = false; @@ -85,7 +87,7 @@ public function getDetailStages() /** * @return int */ - public function getTotalStages() + public function getTotalStages(): int { return (int)$this->detailStages['totalStages']; } diff --git a/src/roa/models/Stage.php b/src/roa/models/Stage.php index 13aa973..d863247 100644 --- a/src/roa/models/Stage.php +++ b/src/roa/models/Stage.php @@ -2,46 +2,17 @@ namespace tecnocen\workflow\roa\models; -use tecnocen\roa\behaviors\Curies; -use tecnocen\roa\behaviors\Slug; -use tecnocen\roa\hal\Embeddable; -use tecnocen\roa\hal\EmbeddableTrait; +use tecnocen\roa\hal\Contract; +use tecnocen\roa\hal\ContractTrait; use tecnocen\workflow\models as base; -use yii\web\Linkable; /** * ROA contract to handle workflow stage records. - * - * @method string[] getSlugLinks() - * @method string getSelfLink() */ -class Stage extends base\Stage implements Linkable, Embeddable +class Stage extends base\Stage implements Contract { - use EmbeddableTrait { - EmbeddableTrait::toArray as embedArray; - } - - /** - * @inheritdoc - */ - public function toArray( - array $fields = [], - array $expand = [], - $recursive = true - ) { - return $this->embedArray( - $fields ?: $this->attributes(), - $expand, - $recursive - ); - } - - /** - * @inheritdoc - */ - public function fields() - { - return array_merge($this->attributes(), ['totalTransitions']); + use ContractTrait { + getLinks as getContractLinks; } /** @@ -57,16 +28,12 @@ public function fields() /** * @inheritdoc */ - public function behaviors() + protected function slugBehaviorConfig(): array { - return array_merge(parent::behaviors(), [ - 'slug' => [ - 'class' => Slug::class, - 'resourceName' => 'stage', - 'parentSlugRelation' => 'workflow', - ], - 'curies' => Curies::class, - ]); + return [ + 'resourceName' => 'stage', + 'parentSlugRelation' => 'workflow', + ]; } /** @@ -74,7 +41,7 @@ public function behaviors() */ public function getLinks() { - return array_merge($this->getSlugLinks(), $this->getCuriesLinks(), [ + return array_merge($this->getContractLinks(), [ 'transitions' => $this->getSelfLink() . '/transition', ]); } diff --git a/src/roa/models/StageSearch.php b/src/roa/models/StageSearch.php index 42c04cd..df642f0 100644 --- a/src/roa/models/StageSearch.php +++ b/src/roa/models/StageSearch.php @@ -2,6 +2,7 @@ namespace tecnocen\workflow\roa\models; +use tecnocen\roa\ResourceSearch; use yii\data\ActiveDataProvider; use yii\web\NotFoundHttpException; @@ -12,7 +13,6 @@ */ class StageSearch extends Stage implements \tecnocen\roa\ResourceSearch { - /** * @inhertidoc */ @@ -27,15 +27,19 @@ public function rules() /** * @inhertidoc */ - public function search(array $params, $formName = '') - { + public function search( + array $params, + ?string $formName = '' + ): ?ActiveDataProvider { $this->load($params, $formName); if (!$this->validate()) { return null; } + if (null === $this->workflow) { throw new NotFoundHttpException('Unexistant workflow path.'); } + $class = get_parent_class(); return new ActiveDataProvider([ 'query' => $class::find()->andFilterWhere([ diff --git a/src/roa/models/Transition.php b/src/roa/models/Transition.php index 9cf7c28..c630bee 100644 --- a/src/roa/models/Transition.php +++ b/src/roa/models/Transition.php @@ -2,22 +2,18 @@ namespace tecnocen\workflow\roa\models; -use tecnocen\roa\behaviors\Curies; -use tecnocen\roa\behaviors\Slug; -use tecnocen\roa\hal\Embeddable; -use tecnocen\roa\hal\EmbeddableTrait; +use tecnocen\roa\hal\Contract; +use tecnocen\roa\hal\ContractTrait; use tecnocen\workflow\models as base; -use yii\web\Linkable; /** * ROA contract to handle workflow transitions records. - * - * @method string[] getSlugLinks() - * @method string getSelfLink() */ -class Transition extends base\Transition implements Linkable, Embeddable +class Transition extends base\Transition implements Contract { - use EmbeddableTrait; + use ContractTrait { + getLinks as getContractLinks; + } /** * @inheritdoc @@ -32,17 +28,13 @@ class Transition extends base\Transition implements Linkable, Embeddable /** * @inheritdoc */ - public function behaviors() + protected function slugBehaviorConfig(): array { - return array_merge(parent::behaviors(), [ - 'slug' => [ - 'class' => Slug::class, - 'resourceName' => 'transition', - 'parentSlugRelation' => 'sourceStage', - 'idAttribute' => 'target_stage_id', - ], - 'curies' => Curies::class, - ]); + return [ + 'resourceName' => 'transition', + 'parentSlugRelation' => 'sourceStage', + 'idAttribute' => 'target_stage_id', + ]; } /** @@ -50,7 +42,7 @@ public function behaviors() */ public function getLinks() { - return array_merge($this->getSlugLinks(), $this->getCuriesLinks(), [ + return array_merge($this->getContractLinks(), [ 'permissions' => $this->getSelfLink() . '/permission', 'target_stage' => $this->targetStage->getSelfLink(), ]); diff --git a/src/roa/models/TransitionPermission.php b/src/roa/models/TransitionPermission.php index 1c39ceb..0ba790f 100644 --- a/src/roa/models/TransitionPermission.php +++ b/src/roa/models/TransitionPermission.php @@ -2,24 +2,16 @@ namespace tecnocen\workflow\roa\models; -use tecnocen\roa\behaviors\Curies; -use tecnocen\roa\behaviors\Slug; -use tecnocen\roa\hal\Embeddable; -use tecnocen\roa\hal\EmbeddableTrait; +use tecnocen\roa\hal\Contract; +use tecnocen\roa\hal\ContractTrait; use tecnocen\workflow\models as base; -use yii\web\Linkable; /** * ROA contract to handle workflow transition permissions records. - * - * @method string[] getSlugLinks() - * @method string getSelfLink() */ -class TransitionPermission extends base\TransitionPermission implements - Linkable, - Embeddable +class TransitionPermission extends base\TransitionPermission implements Contract { - use EmbeddableTrait; + use ContractTrait; /** * @inheritdoc @@ -34,25 +26,13 @@ class TransitionPermission extends base\TransitionPermission implements /** * @inheritdoc */ - public function behaviors() + protected function slugBehaviorConfig(): array { - return array_merge(parent::behaviors(), [ - 'slug' => [ - 'class' => Slug::class, - 'idAttribute' => 'permission', - 'resourceName' => 'permission', - 'parentSlugRelation' => 'transition', - ], - 'curies' => Curies::class, - ]); - } - - /** - * @inheritdoc - */ - public function getLinks() - { - return array_merge($this->getSlugLinks(), $this->getCuriesLinks()); + return [ + 'idAttribute' => 'permission', + 'resourceName' => 'permission', + 'parentSlugRelation' => 'transition', + ]; } /** diff --git a/src/roa/models/TransitionPermissionSearch.php b/src/roa/models/TransitionPermissionSearch.php index fc36d84..53aeb1a 100644 --- a/src/roa/models/TransitionPermissionSearch.php +++ b/src/roa/models/TransitionPermissionSearch.php @@ -2,6 +2,7 @@ namespace tecnocen\workflow\roa\models; +use tecnocen\roa\ResourceSearch; use yii\data\ActiveDataProvider; use yii\web\NotFoundHttpException; @@ -10,10 +11,9 @@ * * @author Angel (Faryshta) Guevara */ -class TransitionPermissionSearch extends TransitionPermission - implements \tecnocen\roa\ResourceSearch +class TransitionPermissionSearch extends TransitionPermission implements + ResourceSearch { - /** * @inhertidoc */ @@ -28,8 +28,10 @@ public function rules() /** * @inhertidoc */ - public function search(array $params, $formName = '') - { + public function search( + array $params, + ?string $formName = '' + ): ?ActiveDataProvider { $this->load($params, $formName); if (!$this->validate()) { return null; diff --git a/src/roa/models/TransitionSearch.php b/src/roa/models/TransitionSearch.php index f52d954..a29899d 100644 --- a/src/roa/models/TransitionSearch.php +++ b/src/roa/models/TransitionSearch.php @@ -28,8 +28,10 @@ public function rules() /** * @inhertidoc */ - public function search(array $params, $formName = '') - { + public function search( + array $params, + ?string $formName = '' + ): ?ActiveDataProvider { $this->load($params, $formName); if (!$this->validate()) { return null; diff --git a/src/roa/models/Workflow.php b/src/roa/models/Workflow.php index 774b7be..7d13385 100644 --- a/src/roa/models/Workflow.php +++ b/src/roa/models/Workflow.php @@ -2,47 +2,18 @@ namespace tecnocen\workflow\roa\models; -use tecnocen\roa\behaviors\Curies; -use tecnocen\roa\behaviors\Slug; -use tecnocen\roa\hal\Embeddable; -use tecnocen\roa\hal\EmbeddableTrait; +use tecnocen\roa\hal\Contract; +use tecnocen\roa\hal\ContractTrait; use tecnocen\workflow\models as base; -use yii\web\Linkable; use yii\web\NotFoundHttpException; /** * ROA contract to handle workflow records. - * - * @method string[] getSlugLinks() - * @method string getSelfLink() */ -class Workflow extends base\Workflow implements Linkable, Embeddable +class Workflow extends base\Workflow implements Contract { - use EmbeddableTrait { - EmbeddableTrait::toArray as embedArray; - } - - /** - * @inheritdoc - */ - public function toArray( - array $fields = [], - array $expand = [], - $recursive = true - ) { - return $this->embedArray( - $fields ?: $this->attributes(), - $expand, - $recursive - ); - } - - /** - * @inheritdoc - */ - public function fields() - { - return array_merge($this->attributes(), ['totalStages']); + use ContractTrait { + getLinks as getContractLinks; } /** @@ -53,24 +24,20 @@ public function fields() /** * @inheritdoc */ - public function behaviors() + protected function slugBehaviorConfig(): array { - return array_merge(parent::behaviors(), [ - 'slug' => [ - 'class' => Slug::class, - 'resourceName' => 'workflow', - 'checkAccess' => function ($params) { - if (isset($params['workflow_id']) - && $this->id != $params['workflow_id'] - ) { - throw new NotFoundHttpException( - 'Workflow not associated to element.' - ); - } - }, - ], - 'curies' => Curies::class, - ]); + return [ + 'resourceName' => 'workflow', + 'checkAccess' => function ($params) { + if (isset($params['workflow_id']) + && $this->id != $params['workflow_id'] + ) { + throw new NotFoundHttpException( + 'Workflow not associated to element.' + ); + } + }, + ]; } /** @@ -78,7 +45,7 @@ public function behaviors() */ public function getLinks() { - return array_merge($this->getSlugLinks(), $this->getCuriesLinks(), [ + return array_merge($this->getContractLinks(), [ 'stages' => $this->getSelfLink() . '/stage', ]); } diff --git a/src/roa/models/WorkflowSearch.php b/src/roa/models/WorkflowSearch.php index 62ee7c1..a49e969 100644 --- a/src/roa/models/WorkflowSearch.php +++ b/src/roa/models/WorkflowSearch.php @@ -2,6 +2,7 @@ namespace tecnocen\workflow\roa\models; +use tecnocen\roa\ResourceSearch; use yii\data\ActiveDataProvider; /** @@ -9,9 +10,8 @@ * * @author Angel (Faryshta) Guevara */ -class WorkflowSearch extends Workflow implements \tecnocen\roa\ResourceSearch +class WorkflowSearch extends Workflow implements ResourceSearch { - /** * @inhertidoc */ @@ -26,12 +26,15 @@ public function rules() /** * @inhertidoc */ - public function search(array $params, $formName = '') - { + public function search( + array $params, + ?string $formName = '' + ): ?ActiveDataProvider { $this->load($params, $formName); if (!$this->validate()) { return null; } + $class = get_parent_class(); return new ActiveDataProvider([ 'query' => $class::find()->andFilterWhere([ diff --git a/src/roa/modules/Version.php b/src/roa/modules/Version.php index 862238f..7f9c370 100644 --- a/src/roa/modules/Version.php +++ b/src/roa/modules/Version.php @@ -2,10 +2,12 @@ namespace tecnocen\workflow\roa\modules; -use tecnocen\workflow\roa\resources\PermissionResource; -use tecnocen\workflow\roa\resources\StageResource; -use tecnocen\workflow\roa\resources\TransitionResource; -use tecnocen\workflow\roa\resources\WorkflowResource; +use tecnocen\workflow\roa\resources\{ + PermissionResource, + StageResource, + TransitionResource, + WorkflowResource +}; class Version extends \tecnocen\roa\modules\ApiVersion { diff --git a/src/roa/resources/PermissionResource.php b/src/roa/resources/PermissionResource.php index 2781959..7b312d5 100644 --- a/src/roa/resources/PermissionResource.php +++ b/src/roa/resources/PermissionResource.php @@ -2,16 +2,16 @@ namespace tecnocen\workflow\roa\resources; -use Yii; use tecnocen\workflow\roa\models\TransitionPermission; use tecnocen\workflow\roa\models\TransitionPermissionSearch; +use tecnocen\roa\controllers\Resource; /** * Resource to assign permissions to a transition. * * @author Angel (Faryshta) Guevara */ -class PermissionResource extends \tecnocen\roa\controllers\OAuth2Resource +class PermissionResource extends Resource { /** * @inheritdoc diff --git a/src/roa/resources/StageResource.php b/src/roa/resources/StageResource.php index 796bad0..1aa0850 100644 --- a/src/roa/resources/StageResource.php +++ b/src/roa/resources/StageResource.php @@ -2,15 +2,16 @@ namespace tecnocen\workflow\roa\resources; -use Yii; -use yii\web\NotFoundHttpException; +use tecnocen\roa\controllers\Resource; use tecnocen\workflow\roa\models\Stage; use tecnocen\workflow\roa\models\StageSearch; /** - * Resource to + * Resource to handle `Stage` records. + * + * @author Angel (Faryshta) Guevara */ -class StageResource extends \tecnocen\roa\controllers\OAuth2Resource +class StageResource extends Resource { /** * @inheritdoc diff --git a/src/roa/resources/TransitionResource.php b/src/roa/resources/TransitionResource.php index ba8a6eb..4f69e74 100644 --- a/src/roa/resources/TransitionResource.php +++ b/src/roa/resources/TransitionResource.php @@ -2,16 +2,16 @@ namespace tecnocen\workflow\roa\resources; -use Yii; +use tecnocen\roa\controllers\Resource; use tecnocen\workflow\roa\models\Transition; use tecnocen\workflow\roa\models\TransitionSearch; /** - * Resource to handle transition records. + * Resource to handle `Transition` records. * * @author Angel (Faryshta) Guevara */ -class TransitionResource extends \tecnocen\roa\controllers\OAuth2Resource +class TransitionResource extends Resource { /** * @inhertidoc diff --git a/src/roa/resources/WorkflowResource.php b/src/roa/resources/WorkflowResource.php index 9d51d1d..ce2d282 100644 --- a/src/roa/resources/WorkflowResource.php +++ b/src/roa/resources/WorkflowResource.php @@ -2,14 +2,16 @@ namespace tecnocen\workflow\roa\resources; +use tecnocen\roa\controllers\Resource; use tecnocen\workflow\roa\models\Workflow; use tecnocen\workflow\roa\models\WorkflowSearch; /** - * CRUD resource for `Workflow` records + * Resource to handle `Workflow` records + * * @author Angel (Faryshta) Guevara */ -class WorkflowResource extends \tecnocen\roa\controllers\OAuth2Resource +class WorkflowResource extends Resource { /** * @inheritdoc From 1683fd7b2eec952fe5ca2a9c21a71ef61f8cc89f Mon Sep 17 00:00:00 2001 From: Carlos Llamosas Date: Sat, 13 Jul 2019 02:27:39 -0500 Subject: [PATCH 4/6] CHANGELOG, supported 7.3 in travis, updated dependencies --- .travis.yml | 1 + CHANGELOG.md | 15 ++++++++++++++- composer.json | 4 ++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7e73268..5e262a1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ language: php php: - 7.1 - 7.2 + - 7.3 - nightly matrix: diff --git a/CHANGELOG.md b/CHANGELOG.md index 092a7c3..e61e158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,20 @@ Yii2 Workflow Change Log ========================== -0.3.0 under development +0.4.0 July 13, 2019 + +- Brk: Methods now use the typecast supported in php 7.1 + +0.3.2 December 18, 2018 + +- fix version bump for yii2 roa tests + +0.3.1 December 17, 2018 +------------------------ + +- Bugfix: Requires yii2 roa dev-master + +0.3.0 October 18, 2018 ------------------------ - Enh: Add CHANGELOG.md diff --git a/composer.json b/composer.json index f28a571..1cb164c 100644 --- a/composer.json +++ b/composer.json @@ -34,13 +34,13 @@ ], "require": { "php": "^7.1", - "tecnocen/yii2-roa": "dev-typecast as 0.5.x-dev", + "tecnocen/yii2-roa": "~0.5.0", "tecnocen/yii2-rmdb": "*", "yii2tech/ar-softdelete": "*" }, "require-dev": { "codeception/base": "^2.2.11", - "codeception/verify": "~0.3.1", + "codeception/verify": "~1.0.0", "flow/jsonpath": "~0.3", "phpunit/php-code-coverage": "5.3.*", "yiisoft/yii2-debug": "*" From c4b184f5fbc1ef2b5046244c4c1e720252c2d5fe Mon Sep 17 00:00:00 2001 From: Carlos Llamosas Date: Sat, 13 Jul 2019 02:44:36 -0500 Subject: [PATCH 5/6] add idAttribute user_id --- tests/_app/api/models/CreditAssignment.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/_app/api/models/CreditAssignment.php b/tests/_app/api/models/CreditAssignment.php index c07209f..a67bc5f 100644 --- a/tests/_app/api/models/CreditAssignment.php +++ b/tests/_app/api/models/CreditAssignment.php @@ -28,6 +28,7 @@ protected function slugBehaviorConfig(): array return [ 'resourceName' => 'assignment', 'parentSlugRelation' => 'process', + 'idAttribute' => 'user_id' ]; } } From d7df7d9c9343449b69aa525cf1b271690204a22a Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Sun, 14 Jul 2019 04:30:46 -0500 Subject: [PATCH 6/6] Update composer.json --- composer.json | 4 ---- 1 file changed, 4 deletions(-) diff --git a/composer.json b/composer.json index 1cb164c..0f09c61 100644 --- a/composer.json +++ b/composer.json @@ -26,10 +26,6 @@ { "type": "composer", "url": "https://asset-packagist.org" - }, - { - "type": "vcs", - "url": "git@github.com:tecnocen-com/yii2-roa.git" } ], "require": {