Skip to content

Commit

Permalink
Add enum bitmask example.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Jan 4, 2024
1 parent 8cc56da commit b760cca
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 59 deletions.
98 changes: 49 additions & 49 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/app_custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
],
];

if (str_contains(getenv('DB_URL'), 'mysql')) {
if (str_contains((string)getenv('DB_URL'), 'mysql')) {
$config['Datasources']['default']['flags'][PDO::MYSQL_ATTR_INIT_COMMAND] = "SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''))";
$config['Datasources']['test']['flags'][PDO::MYSQL_ATTR_INIT_COMMAND] = "SET sql_mode=(SELECT REPLACE(@@sql_mode, 'ONLY_FULL_GROUP_BY', ''))";
}
Expand Down
40 changes: 40 additions & 0 deletions plugins/Sandbox/src/Controller/ToolsExamplesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cake\Datasource\ModelAwareTrait;
use RuntimeException;
use Sandbox\Model\Entity\BitmaskedRecord;
use Sandbox\Model\Enum\Flag;
use Shim\Datasource\LegacyModelAwareTrait;

/**
Expand Down Expand Up @@ -178,6 +179,45 @@ public function bitmaskSearch() {
$this->set(compact('bitmaskedRecords', 'flags', 'type', 'sql'));
}

/**
* @return void
*/
public function bitmaskEnums() {
$this->loadModel('Sandbox.BitmaskedRecords');

$required = (bool)$this->request->getQuery('required');
if (!$required) {
$field = 'flag_optional';
} else {
$field = 'flag_required';
}

$config = ['field' => $field, 'bits' => Flag::class, 'mappedField' => 'flags'];
$this->BitmaskedRecords->behaviors()->load('Tools.Bitmasked', $config);

$records = $this->BitmaskedRecords->find()->all()->toArray();
// Just to have demo data
$this->autoSeed($records);

$bitmaskedRecord = $this->BitmaskedRecords->newEmptyEntity();
if ($this->request->is('post')) {
$bitmaskedRecord = $this->BitmaskedRecords->patchEntity($bitmaskedRecord, $this->request->getData());

if ($bitmaskedRecord->getErrors()) {
$this->Flash->error(__('Form contains errors'));

$bitmaskedRecord->setError('flags', $bitmaskedRecord->getError($field));

} else {
$message = 'Flag value `' . $bitmaskedRecord->$field . '` would now be stored.';
$this->Flash->success($message);
}
}

$flags = $this->BitmaskedRecords->behaviors()->Bitmasked->getConfig('bits');
$this->set(compact('field', 'records', 'flags', 'bitmaskedRecord', 'required'));
}

/**
* Slugged behavior and ascii unique URL slugs
*
Expand Down
24 changes: 24 additions & 0 deletions plugins/Sandbox/src/Model/Enum/Flag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Sandbox\Model\Enum;

use Cake\Database\Type\EnumLabelInterface;
use Cake\Utility\Inflector;
use Tools\Model\Enum\EnumOptionsTrait;

enum Flag: int implements EnumLabelInterface
{
use EnumOptionsTrait;

case Important = 1;
case Featured = 2;
case Approved = 4;
case Flagged = 8;

/**
* @return string
*/
public function label(): string {
return Inflector::humanize(Inflector::underscore($this->name));
}
}
8 changes: 4 additions & 4 deletions plugins/Sandbox/src/Model/Enum/UserStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ enum UserStatus: int implements EnumLabelInterface
{
use EnumOptionsTrait;

case INACTIVE = 0;
case ACTIVE = 1;
case DELETED = 2;
case Inactive = 0;
case Active = 1;
case Deleted = 2;

/**
* @return string
*/
public function label(): string {
return Inflector::humanize(mb_strtolower($this->name));
return Inflector::humanize(Inflector::underscore($this->name));
}
}
Loading

0 comments on commit b760cca

Please sign in to comment.