dev-master
9999999-devBit mask behavior and validators for models
GPLv3
The Requires
by Anton Korniychuk
Bit mask behavior and validators for models
Π Π°ΡΡΠΈΡΠ΅Π½ΠΈΠ΅ ΠΏΡΠ΅Π΄Π½Π°Π·Π½Π°ΡΠ΅Π½ΠΎ Π΄Π»Ρ ΡΠ°Π±ΠΎΡΡ Ρ Π±ΠΈΡΠΎΠ²ΡΠΌΠΈ ΠΌΠ°ΡΠΊΠ°ΠΌΠΈ., (*1)
ΠΠ° ΠΏΡΠΈΠΌΠ΅Ρ, Π²Ρ Ρ ΠΎΡΠΈΡΠ΅ Ρ ΡΠ°Π½ΠΈΡΡ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΡ ΠΏΠ΅ΡΠ΅ΠΊΠ»ΡΡΠ°ΡΠ΅Π»Π΅ΠΉ Π² ΠΏΠΎΠ»Π΅ ΡΠΈΠΏΠ° int., (*2)
ΠΠΎΠ·Π½ΠΈΠΊΠ°Π΅Ρ ΡΡΠ΄ Π²ΠΎΠΏΡΠΎΡΠΎΠ²:, (*3)
yii2-bitmask Π΄Π°Π΅Ρ ΡΠ΅ΡΠ°Π΅Ρ Π²ΡΠ΅ ΡΡΠΈ Π²ΠΎΠΏΡΠΎΡΡ, (*4)
Feel free to let me know what else you want added via:, (*5)
The preferred way to install this extension is through composer., (*6)
Either run, (*7)
$ php composer.phar require ancor/yii2-bitmask
or add, (*8)
"ancor/yii2-bitmask": "dev-master"
to the require
section of your composer.json
file., (*9)
use ancor\bitmask\BitmaskBehavior; /** * @property integer $options * ... * @property string $spamOption * @property string $deletedOption * ... */ class User extends \yii\db\ActiveRecord { const OPT_SPAM = 1<<0; const OPT_DELETED = 1<<1; public function behaviors() { return [ 'bitmask' => [ 'options' => [ 'spamOption' => static::OPT_SPAM, 'deletedOption' => static::OPT_DELETED, ], // 'bitmaskAttribute' => 'options', // an attribute which is the mask itself ], ]; } public function rules() { return [ [['spamOption', 'deletedOption'], 'safe'], ]; } public function attributeLabels() { return [ ... 'spamOption' => 'This user is spammer', 'emailNotVerified' => 'User is deleted', ... ]; } }
$model = new User(); echo $model->options; // 0 // ΠΠ°Π·Π½Π°ΡΠΈΡΡ Π±ΠΈΡ $model->spamOption = true; // $model->options == User::OPT_SPAM == 1<<0 == 1 // ΠΡΠΎ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎ ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΉ ΡΡΡΠΎΠΊΠ΅ $model->options = $model->options | User::OPT_SPAM; // ΠΠ½ΠΈΠΌΠ°Π½ΠΈΠ΅! ΠΡΠΈ ΠΏΡΠΈΡΠ²Π°ΠΈΠ²Π°Π½ΠΈΠΈ Π·Π½Π°ΡΠ΅Π½ΠΈΡ - Π»ΡΠ±ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΏΡΠΈΠ½ΡΠ΄ΠΈΡΠ΅Π»ΡΠ½ΠΎ ΠΏΡΠ΅ΠΎΠ±ΡΠ°Π·ΡΠ΅ΡΡΡ Π² boolean // Π‘Π½ΡΡΡ Π±ΠΈΡ $model->spamOption = false; // $model->options == 0 // ΠΡΠΎ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎ ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΉ ΡΡΡΠΎΠΊΠ΅ $model->options = $model->options & ~User::OPT_SPAM; // ΠΡΠΎΠ²Π΅ΡΠΈΡΡ Π½Π°Π»ΠΈΡΠΈΠ΅ Π±ΠΈΡΠ° if ($model->spamOption) ... // ΠΡΠΎ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½ΠΎ ΡΠ»Π΅Π΄ΡΡΡΠ΅ΠΉ ΡΡΡΠΎΠΊΠ΅ if ($model->options & User::OPT_SPAM) ...
// ΠΡΠ΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΠΌ ΠΏΡΠΈΡΠ»Π° ΡΠΎΡΠΌΠ° $post = [ 'User' => [ ... 'spamOption' => false, 'deletedOption' => true, ... ], ]; /** * ΠΠ°Π³ΡΡΠ·ΠΊΠ° ΠΎΠ΄Π½ΠΎΠΉ ΠΊΠΎΠΌΠΌΠ°Π½Π΄ΠΎΠΉ */ echo $model->options; // 0 $model->load($post); echo $model->options; // $model->options == 1<<1 == 2 var_dump($model->spamOption); // false var_dump($model->deletedOption); // true /** * ΠΡΠΈΠΌΠ΅Ρ ΠΠΠ BitmaskBehavior, ΠΏΡΠΈ ΠΎΡΠΏΡΠ°Π²ΠΊΠ΅ ΡΠΎΠΉ ΠΆΠ΅ ΡΠ°ΠΌΠΎΠΉ ΡΠΎΡΠΌΡ * ΠΡΠ΅Π΄ΠΏΠΎΠ»ΠΎΠΆΠΈΠΌ ΡΡΠΎ Π² ΠΌΠΎΠ΄Π΅Π»ΠΈ ΠΎΠ±ΡΡΠ²Π»Π΅Π½Ρ 2 ΡΠ²ΠΎΠΉΡΡΠ²Π° * public $spamOption; * public $deletedOption; */ echo $model->options; // 0 $model->load($post); // Π‘Π²ΠΎΠΉΡΡΠ²ΠΎ options, ΠΊΠΎΠ½Π΅ΡΠ½ΠΎ ΠΆΠ΅ ΠΎΡΡΠ°Π»ΠΎΡΡ Π±Π΅Π· ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ echo $model->options; // 0 if ($post['User']['spamOption']) { $model->options |= User::OPT_SPAM; } else { $model->options &= ~User::OPT_SPAM; } if ($post['User']['deletedOption']) { $model->options |= User::OPT_DELETED; } else { $model->options &= ~User::OPT_DELETED; }
$form->field($model, 'spamOption')->checkbox(); $form->field($model, 'deletedOption')->checkbox();
ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅: ΠΡΠ»ΠΈ Π²Ρ Ρ
ΠΎΡΠΈΡΠ΅ Π·Π°ΠΏΡΠ΅ΡΠΈΡΡ ΠΌΠ΅Π½ΡΡΡ Π½Π΅ΠΊΠΎΡΠΎΡΡΠ΅ Π±ΠΈΡΡ - ΡΠΎ ΠΏΡΠΎΡΡΠΎ Π½Π΅ Π½ΡΠΆΠ½ΠΎ ΠΏΠ΅ΡΠ΅ΡΠΈΡΠ»ΡΡΡ ΠΈΡ
Π² Π²Π°Π»ΠΈΠ΄Π°ΡΠΎΡΠ΅ safe
, (*10)
php public integer[] getBitmaskFields(void)
, (*11)
ΠΠ΅ΡΠΎΠ΄ Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ ΠΌΠ°ΡΡΠΈΠ² Ρ Π½Π°ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Π½ΠΈΡΠΌΠΈ ΠΏΠΎΠ»Π΅ΠΉ ΠΈ ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΠΈΠΌΠΈ ΠΈΠΌ Π±ΠΈΡΠ°ΠΌΠΈ. ΠΠ΅Π· Π·Π½Π°ΡΠ΅Π½ΠΈΠΉ ΠΏΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ, (*12)
ΠΡΠΈΠΌΠ΅Ρ ΠΎΡΠ²Π΅ΡΠ°(Π² Π½Π°ΡΠ΅ΠΌ ΡΠ»ΡΡΠ°Π΅):, (*13)
[ 'spamOption' => 1, // 1<<0 'deletedOption' => 2, // 1<<1 ]
php public boolean[] getBitmaskValues(void)
ΠΠ΅ΡΠΎΠ² Π²ΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ Π½Π°ΠΈΠΌΠ΅Π½ΠΎΠ²Π°Π½ΠΈΡ ΠΏΠΎΠ»Π΅ΠΉ Ρ ΠΈΡ
Π·Π½Π°ΡΠ΅Π½ΠΈΡΠΌΠΈ true\false., (*14)
ΠΡΠΈΠΌΠ΅Ρ ΠΎΡΠ²Π΅ΡΠ°(Π² Π½Π°ΡΠ΅ΠΌ ΡΠ»ΡΡΠ°Π΅):, (*15)
[ 'spamOption' => false, 'deletedOption' => true, ]
php public boolean getOldBit(string $field)
ΠΠΎΠ·Π²ΡΠ°ΡΠ°Π΅Ρ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ "ΡΡΠ°ΡΠΎΠ³ΠΎ" Π°ΡΡΠΈΠ±ΡΡΠ°. (Π Π°Π±ΠΎΡΠ°Π΅Ρ ΡΠ΅ΡΠ΅Π· Π²ΡΠ·ΠΎΠ² ->getOldAttribute()
), (*16)
public static integer modifyBitmask(int $mask, int $bit, boolean $exists)
```php $mask = 0b00100001; $bit = 0b00000100;, (*17)
// ΠΠΎΠ±Π°Π²ΠΈΡΡ Π±ΠΈΡ $options = BitmaskBehavior::modifyBitmask($mask, $bit, true); // 0b00100101 // Π£Π±ΡΠ°ΡΡ Π±ΠΈΡ $options = BitmaskBehavior::modifyBitmask($mask, $bit, false); // 0b00100001, (*18)
#### ΠΠΎΠ»ΡΡΠΈΡΡ ΠΌΠ°ΡΡΠΈΠ² Π±ΠΈΡΠΎΠ² Π½Π° ΠΎΡΠ½ΠΎΠ²Π΅ Π±ΠΈΡΠΎΠ²ΠΎΠΉ ΠΌΠ°ΡΠΊΠΈ ```php public static boolean[] parseBitmask(int $mask, int[] $fields)
```php $mask = 0b00100000; $fields = [ 'firstOption' => 0b00000001, 'secondOption' => 0b00100000, ];, (*19)
$values = BitmaskBehavior::parseBitmask($mask, $fields); print_r($values); // ['firstOption' => false, 'secondOption' => true], (*20)
#### Π‘ΠΎΠ·Π΄Π°ΡΡ Π±ΠΈΡΠΎΠ²ΡΡ ΠΌΠ°ΡΠΊΡ Π½Π° ΠΎΡΠ½ΠΎΠ²Π΅ ΠΌΠ°ΡΡΠΈΠ²Π° Π±ΠΈΡΠΎΠ² ```php public static int makeBitmask(boolean[] $values, int[] $fields) ``` ```php $fields = [ 'firstOption' => 0b00000001, 'secondOption' => 0b00100000, ]; $values = [ 'firstOption' => false, 'secondOption' => true ]; $mask = BitmaskBehavior::makeBitmask($values, $fields); echo $mask; // 0b00100000
ΠΠΏΠΈΡΠ°Π½ΠΈΠ΅: ΠΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΡΠΊΠ°Π·Π°ΡΡ ΠΊΠ°ΠΊΠΈΠ΅ Π±ΠΈΡΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΌΠ΅Π½ΡΡΡ Π² ΠΌΠ°ΡΠΊΠ΅, Π° ΠΊΠ°ΠΊΠΈΠ΅ Π½Π΅Π»ΡΠ·Ρ., (*21)
ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅: ΠΡΠΎΡ Π²Π°Π»ΠΈΠ΄Π°ΡΠΎΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π±Π΅Π· BitmaskBehavior., (*22)
public function rules() { return [ ['options', BitmaskValidator::className(), 'mask' => 1<<3 | 1<<4 | 1<<6], // ΠΠ»ΠΈ Ρ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ΄ΠΎΠΌ ΡΠΎΠΎΠ±ΡΠ΅Π½ΠΈΡ ['options', BitmaskValidator::className(), 'mask' => 1<<3 | 1<<4 | 1<<6, 'message' => ...], // ΠΠΎΠΆΠ½ΠΎ ΡΠ°ΠΊ ΠΆΠ΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ ΠΊΠΎΠ½ΡΡΠ°Π½ΡΡ ['options', BitmaskValidator::className(), 'mask' => static::OPT_SPAM], ]; }
ΠΠΏΠΈΡΠ°Π½ΠΈΠ΅:, (*23)
BitmaskValidator
.BitmaskBehavior
.safe
Π²Π°Π»ΠΈΠ΄Π°ΡΠΎΡΠ°. ΠΠΎ ΡΡΠΎ Π½Π΅ Π²ΡΠ΅.BitmaskValidator
Π±Π»ΠΎΠΊΠΈΡΡΠ΅Ρ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠ΅ Π²ΡΠ΅Ρ
Π±ΠΈΡΠΎΠ² ΠΊ ΠΊΡΠΎΠΌΠ΅ ΡΠ΅Ρ
ΠΊ ΠΊΠΎΡΠΎΡΡΠΌ Π²Π°Π»ΠΈΠ΄Π°ΡΠΎΡ ΠΏΡΠΈΠΌΠ΅Π½Π΅Π½.ΠΡΠΈΠΌΠ΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΡ, (*24)
return [ [ ['spamOption', 'deletedOption'], BitmaskFieldsValidator::className(), // 'maskAttribute' => 'options', // ΠΠΎ ΡΠΌΠΎΠ»ΡΠ°Π½ΠΈΡ ], ];
Bit mask behavior and validators for models
GPLv3