From 9df6f2e7eff4b8d2d3ab108a7346f84d21fc6f0d Mon Sep 17 00:00:00 2001 From: mdmunir Date: Tue, 2 Feb 2016 14:23:25 +0700 Subject: [PATCH] Refactoring code --- ...ueValidator.php => AutonumberValidator.php | 42 ++++++++----------- Bootstrap.php | 3 +- README.md | 2 +- 3 files changed, 20 insertions(+), 27 deletions(-) rename NextValueValidator.php => AutonumberValidator.php (78%) diff --git a/NextValueValidator.php b/AutonumberValidator.php similarity index 78% rename from NextValueValidator.php rename to AutonumberValidator.php index 5e97feb..fdef03f 100644 --- a/NextValueValidator.php +++ b/AutonumberValidator.php @@ -22,7 +22,7 @@ * @author Misbahul D Munir * @since 1.0 */ -class NextValueValidator extends \yii\validators\Validator +class AutonumberValidator extends \yii\validators\Validator { /** * @var mixed the default value or a PHP callable that returns the default value which will @@ -76,18 +76,25 @@ class NextValueValidator extends \yii\validators\Validator public function validateAttribute($object, $attribute) { if ($this->isEmpty($object->$attribute)) { - $object->$attribute = $this->nextValue($object, $attribute); + $eventId = uniqid(); + $object->on(ActiveRecord::EVENT_BEFORE_INSERT, [$this, 'beforeSave'], [$eventId, $attribute]); + $object->on(ActiveRecord::EVENT_BEFORE_UPDATE, [$this, 'beforeSave'], [$eventId, $attribute]); } } /** - * Calculate next value - * @param \yii\db\ActiveRecord $object - * @param string $attribute - * @return string + * Handle for [[\yii\db\ActiveRecord::EVENT_BEFORE_INSERT]] and [[\yii\db\ActiveRecord::EVENT_BEFORE_UPDATE]] + * @param \yii\base\ModelEvent $event */ - public function nextValue($object, $attribute) + public function beforeSave($event) { + list($id, $attribute) = $event->data; + if (isset(self::$_executed[$id])) { + return; + } + + /* @var $object \yii\db\ActiveRecord */ + $object = $event->sender; if ($this->format instanceof \Closure) { $value = call_user_func($this->format, $object, $attribute); } else { @@ -100,6 +107,7 @@ public function nextValue($object, $attribute) 'attribute' => $attribute, 'value' => $value ])); + $model = AutoNumber::findOne($group); if ($model) { $number = $model->number + 1; @@ -112,28 +120,12 @@ public function nextValue($object, $attribute) $model->update_time = time(); $model->number = $number; - $eventId = uniqid(); - $object->on(ActiveRecord::EVENT_BEFORE_INSERT, [$this, 'beforeSave'], [$model, $eventId]); - $object->on(ActiveRecord::EVENT_BEFORE_UPDATE, [$this, 'beforeSave'], [$model, $eventId]); - if ($value === null) { - return $number; + $object->$attribute = $number; } else { - return str_replace('?', $this->digit ? sprintf("%0{$this->digit}d", $number) : $number, $value); + $object->$attribute = str_replace('?', $this->digit ? sprintf("%0{$this->digit}d", $number) : $number, $value); } - } - /** - * Handle for [[\yii\db\ActiveRecord::EVENT_BEFORE_INSERT]] and [[\yii\db\ActiveRecord::EVENT_BEFORE_UPDATE]] - * @param \yii\base\ModelEvent $event - */ - public function beforeSave($event) - { - /* @var $model AutoNumber */ - list($model, $id) = $event->data; - if (isset(self::$_executed[$id])) { - return; - } self::$_executed[$id] = true; try { $model->save(); diff --git a/Bootstrap.php b/Bootstrap.php index 61f32a9..b01f14a 100644 --- a/Bootstrap.php +++ b/Bootstrap.php @@ -18,6 +18,7 @@ class Bootstrap implements BootstrapInterface */ public function bootstrap($app) { - Validator::$builtInValidators['nextValue'] = __NAMESPACE__ . '\NextValueValidator'; + Validator::$builtInValidators['nextValue'] = __NAMESPACE__ . '\AutonumberValidator'; + Validator::$builtInValidators['autonumber'] = __NAMESPACE__ . '\AutonumberValidator'; } } \ No newline at end of file diff --git a/README.md b/README.md index 893d2e9..d20af39 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ Instead of behavior, you can use this extension as validator public function rules() { return [ - [['sales_num'],'nextValue','format'=>'SA.'.date('Y-m-d').'.?'], + [['sales_num'], 'autonumber', 'format'=>'SA.'.date('Y-m-d').'.?'], ... ]; }