From c3a0cf43430438bfe7e7b0ccccfadb72a74331d6 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Fri, 12 Sep 2014 15:57:05 +0200 Subject: Remove some code duplication (validation rules) --- app/Model/Category.php | 32 ++++++++++++++------ app/Model/Comment.php | 35 +++++++++++++++------- app/Model/Project.php | 36 ++++++++++++++-------- app/Model/SubTask.php | 4 +-- app/Model/Task.php | 81 ++++++++++++++++++++++++++++---------------------- app/Model/User.php | 69 +++++++++++++++++++++++++----------------- 6 files changed, 160 insertions(+), 97 deletions(-) (limited to 'app') diff --git a/app/Model/Category.php b/app/Model/Category.php index db26ebca..fb54594b 100644 --- a/app/Model/Category.php +++ b/app/Model/Category.php @@ -173,12 +173,12 @@ class Category extends Base */ public function validateCreation(array $values) { - $v = new Validator($values, array( + $rules = array( new Validators\Required('project_id', t('The project id is required')), - new Validators\Integer('project_id', t('The project id must be an integer')), new Validators\Required('name', t('The name is required')), - new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50) - )); + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), @@ -195,17 +195,31 @@ class Category extends Base */ public function validateModification(array $values) { - $v = new Validator($values, array( + $rules = array( new Validators\Required('id', t('The id is required')), - new Validators\Integer('id', t('The id must be an integer')), - new Validators\Integer('project_id', t('The project id must be an integer')), new Validators\Required('name', t('The name is required')), - new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50) - )); + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), $v->getErrors() ); } + + /** + * Common validation rules + * + * @access private + * @return array + */ + private function commonValidationRules() + { + return array( + new Validators\Integer('id', t('The id must be an integer')), + new Validators\Integer('project_id', t('The project id must be an integer')), + new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50) + ); + } } diff --git a/app/Model/Comment.php b/app/Model/Comment.php index 43c275bc..cd361b1d 100644 --- a/app/Model/Comment.php +++ b/app/Model/Comment.php @@ -155,13 +155,12 @@ class Comment extends Base */ public function validateCreation(array $values) { - $v = new Validator($values, array( - new Validators\Required('task_id', t('This value is required')), - new Validators\Integer('task_id', t('This value must be an integer')), + $rules = array( new Validators\Required('user_id', t('This value is required')), - new Validators\Integer('user_id', t('This value must be an integer')), - new Validators\Required('comment', t('Comment is required')) - )); + new Validators\Required('task_id', t('This value is required')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), @@ -178,15 +177,31 @@ class Comment extends Base */ public function validateModification(array $values) { - $v = new Validator($values, array( + $rules = array( new Validators\Required('id', t('This value is required')), - new Validators\Integer('id', t('This value must be an integer')), - new Validators\Required('comment', t('Comment is required')) - )); + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), $v->getErrors() ); } + + /** + * Common validation rules + * + * @access private + * @return array + */ + private function commonValidationRules() + { + return array( + new Validators\Integer('id', t('This value must be an integer')), + new Validators\Integer('task_id', t('This value must be an integer')), + new Validators\Integer('user_id', t('This value must be an integer')), + new Validators\Required('comment', t('Comment is required')) + ); + } } diff --git a/app/Model/Project.php b/app/Model/Project.php index 9aef3c7e..6e75838c 100644 --- a/app/Model/Project.php +++ b/app/Model/Project.php @@ -623,6 +623,23 @@ class Project extends Base ->save(array('is_public' => 0, 'token' => '')); } + /** + * Common validation rules + * + * @access private + * @return array + */ + private function commonValidationRules() + { + return array( + new Validators\Integer('id', t('This value must be an integer')), + new Validators\Integer('is_active', t('This value must be an integer')), + new Validators\Required('name', t('The project name is required')), + new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50), + new Validators\Unique('name', t('This project must be unique'), $this->db->getConnection(), self::TABLE), + ); + } + /** * Validate project creation * @@ -632,11 +649,7 @@ class Project extends Base */ public function validateCreation(array $values) { - $v = new Validator($values, array( - new Validators\Required('name', t('The project name is required')), - new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50), - new Validators\Unique('name', t('This project must be unique'), $this->db->getConnection(), self::TABLE) - )); + $v = new Validator($values, $this->commonValidationRules()); return array( $v->execute(), @@ -653,14 +666,11 @@ class Project extends Base */ public function validateModification(array $values) { - $v = new Validator($values, array( - new Validators\Required('id', t('The project id is required')), - new Validators\Integer('id', t('This value must be an integer')), - new Validators\Required('name', t('The project name is required')), - new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50), - new Validators\Unique('name', t('This project must be unique'), $this->db->getConnection(), self::TABLE), - new Validators\Integer('is_active', t('This value must be an integer')) - )); + $rules = array( + new Validators\Required('id', t('This value is required')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), diff --git a/app/Model/SubTask.php b/app/Model/SubTask.php index a4fae764..400c79f4 100644 --- a/app/Model/SubTask.php +++ b/app/Model/SubTask.php @@ -234,7 +234,7 @@ class SubTask extends Base new Validators\Required('title', t('The title is required')), ); - $v = new Validator($values, $rules + $this->commonValidationRules()); + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), @@ -256,7 +256,7 @@ class SubTask extends Base new Validators\Required('task_id', t('The task id is required')), ); - $v = new Validator($values, $rules + $this->commonValidationRules()); + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), diff --git a/app/Model/Task.php b/app/Model/Task.php index 7245417a..6f62c3d2 100644 --- a/app/Model/Task.php +++ b/app/Model/Task.php @@ -675,25 +675,41 @@ class Task extends Base } /** - * Validate task creation + * Common validation rules * - * @access public - * @param array $values Form values - * @return array $valid, $errors [0] = Success or not, [1] = List of errors + * @access private + * @return array */ - public function validateCreation(array $values) + private function commonValidationRules() { - $v = new Validator($values, array( - new Validators\Required('project_id', t('The project is required')), + return array( + new Validators\Integer('id', t('This value must be an integer')), new Validators\Integer('project_id', t('This value must be an integer')), new Validators\Integer('column_id', t('This value must be an integer')), new Validators\Integer('owner_id', t('This value must be an integer')), new Validators\Integer('creator_id', t('This value must be an integer')), new Validators\Integer('score', t('This value must be an integer')), - new Validators\Required('title', t('The title is required')), + new Validators\Integer('category_id', t('This value must be an integer')), new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200), new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()), - )); + ); + } + + /** + * Validate task creation + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateCreation(array $values) + { + $rules = array( + new Validators\Required('project_id', t('The project is required')), + new Validators\Required('title', t('The title is required')), + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), @@ -710,11 +726,12 @@ class Task extends Base */ public function validateDescriptionCreation(array $values) { - $v = new Validator($values, array( + $rules = array( new Validators\Required('id', t('The id is required')), - new Validators\Integer('id', t('This value must be an integer')), new Validators\Required('description', t('The description is required')), - )); + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), @@ -731,16 +748,11 @@ class Task extends Base */ public function validateModification(array $values) { - $v = new Validator($values, array( + $rules = array( new Validators\Required('id', t('The id is required')), - new Validators\Integer('id', t('This value must be an integer')), - new Validators\Integer('project_id', t('This value must be an integer')), - new Validators\Integer('column_id', t('This value must be an integer')), - new Validators\Integer('owner_id', t('This value must be an integer')), - new Validators\Integer('score', t('This value must be an integer')), - new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200), - new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()), - )); + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), @@ -757,14 +769,13 @@ class Task extends Base */ public function validateAssigneeModification(array $values) { - $v = new Validator($values, array( + $rules = array( new Validators\Required('id', t('The id is required')), - new Validators\Integer('id', t('This value must be an integer')), new Validators\Required('project_id', t('The project is required')), - new Validators\Integer('project_id', t('This value must be an integer')), new Validators\Required('owner_id', t('This value is required')), - new Validators\Integer('owner_id', t('This value must be an integer')), - )); + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), @@ -781,14 +792,14 @@ class Task extends Base */ public function validateCategoryModification(array $values) { - $v = new Validator($values, array( + $rules = array( new Validators\Required('id', t('The id is required')), - new Validators\Integer('id', t('This value must be an integer')), new Validators\Required('project_id', t('The project is required')), - new Validators\Integer('project_id', t('This value must be an integer')), new Validators\Required('category_id', t('This value is required')), - new Validators\Integer('category_id', t('This value must be an integer')), - )); + + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), @@ -805,12 +816,12 @@ class Task extends Base */ public function validateProjectModification(array $values) { - $v = new Validator($values, array( + $rules = array( new Validators\Required('id', t('The id is required')), - new Validators\Integer('id', t('This value must be an integer')), new Validators\Required('project_id', t('The project is required')), - new Validators\Integer('project_id', t('This value must be an integer')), - )); + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), diff --git a/app/Model/User.php b/app/Model/User.php index 918ecf2a..b99be2cb 100644 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -224,26 +224,49 @@ class User extends Base } /** - * Validate user creation + * Common validation rules * - * @access public - * @param array $values Form values - * @return array $valid, $errors [0] = Success or not, [1] = List of errors + * @access private + * @return array */ - public function validateCreation(array $values) + private function commonValidationRules() { - $v = new Validator($values, array( + return array( new Validators\Required('username', t('The username is required')), new Validators\MaxLength('username', t('The maximum length is %d characters', 50), 50), new Validators\Unique('username', t('The username must be unique'), $this->db->getConnection(), self::TABLE, 'id'), + new Validators\Email('email', t('Email address invalid')), + new Validators\Integer('default_project_id', t('This value must be an integer')), + new Validators\Integer('is_admin', t('This value must be an integer')), + ); + } + + /** + * Common password validation rules + * + * @access private + * @return array + */ + private function commonPasswordValidationRules() + { + return array( new Validators\Required('password', t('The password is required')), new Validators\MinLength('password', t('The minimum length is %d characters', 6), 6), new Validators\Required('confirmation', t('The confirmation is required')), new Validators\Equals('password', 'confirmation', t('Passwords don\'t match')), - new Validators\Integer('default_project_id', t('This value must be an integer')), - new Validators\Integer('is_admin', t('This value must be an integer')), - new Validators\Email('email', t('Email address invalid')), - )); + ); + } + + /** + * Validate user creation + * + * @access public + * @param array $values Form values + * @return array $valid, $errors [0] = Success or not, [1] = List of errors + */ + public function validateCreation(array $values) + { + $v = new Validator($values, array_merge($this->commonValidationRules(), $this->commonPasswordValidationRules())); return array( $v->execute(), @@ -260,19 +283,11 @@ class User extends Base */ public function validateModification(array $values) { - if (! empty($values['password'])) { - return $this->validatePasswordModification($values); - } - - $v = new Validator($values, array( + $rules = array( new Validators\Required('id', t('The user id is required')), - new Validators\Required('username', t('The username is required')), - new Validators\MaxLength('username', t('The maximum length is %d characters', 50), 50), - new Validators\Unique('username', t('The username must be unique'), $this->db->getConnection(), self::TABLE, 'id'), - new Validators\Integer('default_project_id', t('This value must be an integer')), - new Validators\Integer('is_admin', t('This value must be an integer')), - new Validators\Email('email', t('Email address invalid')), - )); + ); + + $v = new Validator($values, array_merge($rules, $this->commonValidationRules())); return array( $v->execute(), @@ -289,14 +304,12 @@ class User extends Base */ public function validatePasswordModification(array $values) { - $v = new Validator($values, array( + $rules = array( new Validators\Required('id', t('The user id is required')), new Validators\Required('current_password', t('The current password is required')), - new Validators\Required('password', t('The password is required')), - new Validators\MinLength('password', t('The minimum length is %d characters', 6), 6), - new Validators\Required('confirmation', t('The confirmation is required')), - new Validators\Equals('password', 'confirmation', t('Passwords don\'t match')), - )); + ); + + $v = new Validator($values, array_merge($rules, $this->commonPasswordValidationRules())); if ($v->execute()) { -- cgit v1.2.3