diff --git a/readme.md b/readme.md index 087e475..6693f9f 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ ### Configuration -##### 添加文件: `BitFlagBehavior.php` +#### 添加文件: `BitFlagBehavior.php` ```php owner; + valuewner = $this->owner; foreach ($this->bitFlags as $bitFlag => $flags) { - $bitFlagValue = $owner->$bitFlag; + $bitFlagValue = valuewner->$bitFlag; foreach ($flags as $pos => $flag) { - $owner->$flag = ($bitFlagValue & 1 << $pos) >> $pos; + valuewner->$flag = ($bitFlagValue & 1 << $pos) >> $pos; } } } @@ -90,13 +90,13 @@ class BitFlagBehavior extends Behavior */ public function setBitFlag($bitFlag, $flags) { - $owner = $this->owner; + valuewner = $this->owner; foreach ($flags as $pos => $flag) { - $flagValue = $owner->$flag; + $flagValue = valuewner->$flag; if ($flagValue === null) { continue; } - $owner->$bitFlag = $this->getBitFlagUpdate($owner->$bitFlag, $pos, $flagValue); + valuewner->$bitFlag = $this->getBitFlagUpdate(valuewner->$bitFlag, $pos, $flagValue); } } @@ -120,38 +120,38 @@ class BitFlagBehavior extends Behavior /** * {@inheritdoc} */ - public function __get($name) + public function __get(name) { - return $this->_attributes[$name]; + return $this->_attributes[name]; } /** * {@inheritdoc} */ - public function __set($name, $value) + public function __set(name, $value) { - $this->_attributes[$name] = $value; + $this->_attributes[name] = $value; } /** * {@inheritdoc} */ - public function canGetProperty($name, $checkVars = true) + public function canGetProperty(name, $checkVars = true) { - return parent::canGetProperty($name, $checkVars) || ArrayHelper::keyExists($name, $this->_attributes); + return parent::canGetProperty(name, $checkVars) || ArrayHelper::keyExists(name, $this->_attributes); } /** * {@inheritdoc} */ - public function canSetProperty($name, $checkVars = true) + public function canSetProperty(name, $checkVars = true) { - return parent::canSetProperty($name, $checkVars) || ArrayHelper::keyExists($name, $this->_attributes); + return parent::canSetProperty(name, $checkVars) || ArrayHelper::keyExists(name, $this->_attributes); } } ``` -##### 在需要的`Model`里添加`Behavior` +#### 在需要的`Model`里添加`Behavior` ```php andWhereBitFlags(['s0' => 1, 's1' => 0]) ->orWhereBitFlags(['s2' => 0, 's3' => 1]) ->all(); +``` + +#### Migration + +##### 从最高位添加标志位,默认值为 `1` (默认值为 `0` 无操作) + +添加公式: `value + (1 << n)` +删除公式: `value - (1 << n)` + +migration: +```php +update('{table}', ['{column}' => new \yii\db\Expression('{column} + (1 << {n})')]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->update('{table}', ['{column}' => new \yii\db\Expression('{column} - (1 << {n})')]); + } +} +``` + +##### 标志位插入到第 `n` 位,默认值为 `k` + +插入公式: `(value >> n << (n + 1)) + (value & ((1 << n) - 1)) + (k << n)` +删除公式: `(value >> (n + 1) << n) + (value & ((1 << n) - 1))` + +migration: +```php +update('{table}', ['{column}' => new \yii\db\Expression('({column} >> {n} << ({n} + 1)) + ({column} & ((1 << {n}) - 1)) + ({k} << {n})')]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->update('{table}', ['{column}' => new \yii\db\Expression('({column} >> ({n} + 1) << {n}) + ({column} & ((1 << {n}) - 1))')]); + } +} +``` + +封装: + +```php +/** + * @param string $column + * @param int $pos + * @param string $operate + * @param int $defaultValue + * @return \yii\db\Expression + */ +public function getBitFlagExpression($column, $pos, $operate, $defaultValue = 0) +{ + if ($operate == 'insert') { + $expresstion = "($column >> $pos << ($pos + 1)) + ($column & ((1 << $pos) - 1)) + ($defaultValue << $pos)"; + } elseif ($operate == 'delete') { + $expresstion = "($column >> ($pos + 1) << $pos) + ($column & ((1 << $pos) - 1))"; + } else { + throw new \InvalidArgumentException('$operate: '.$operate); + } + + return new \yii\db\Expression($expresstion); +} +``` + +migration: + +```php +update('{table}', ['{column}' => $this->getBitFlagExpression('{column}', 1, 'insert', {k})]); + } + + /** + * {@inheritdoc} + */ + public function safeDown() + { + $this->update('{table}', ['{column}' => $this->getBitFlagExpression('{column}', 1, 'delete')]); + } + + /** + * @param string $column + * @param int $pos + * @param string $operate + * @param int $defaultValue + * @return \yii\db\Expression + */ + public function getBitFlagExpression($column, $pos, $operate, $defaultValue = 0) + { + if ($operate == 'insert') { + $expresstion = "($column >> $pos << ($pos + 1)) + ($column & ((1 << $pos) - 1)) + ($defaultValue << $pos)"; + } elseif ($operate == 'delete') { + $expresstion = "($column >> ($pos + 1) << $pos) + ($column & ((1 << $pos) - 1))"; + } else { + throw new \InvalidArgumentException('$operate: '.$operate); + } + + return new \yii\db\Expression($expresstion); + } +} + ``` \ No newline at end of file