Skip to content

Commit

Permalink
Refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmunir committed Feb 2, 2016
1 parent 2ee604d commit 9df6f2e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
42 changes: 17 additions & 25 deletions NextValueValidator.php → AutonumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @author Misbahul D Munir <[email protected]>
* @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
Expand Down Expand Up @@ -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 {
Expand All @@ -100,6 +107,7 @@ public function nextValue($object, $attribute)
'attribute' => $attribute,
'value' => $value
]));

$model = AutoNumber::findOne($group);
if ($model) {
$number = $model->number + 1;
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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').'.?'],
...
];
}
Expand Down

0 comments on commit 9df6f2e

Please sign in to comment.