PHP code example of matthewpageuk / laravel-bitty-enums
1. Go to this page and download the library: Download matthewpageuk/laravel-bitty-enums library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
matthewpageuk / laravel-bitty-enums example snippets
use MatthewPageUK\BittyEnums\Contracts\BittyEnum;
enum Colour: int implements BittyEnum
{
case Red = 1;
case Green = 2;
case Blue = 4;
case White = 8;
case Black = 16;
case Pink = 32;
}
use App\Enums\Colour;
use MatthewPageUK\BittyEnums\Contracts\BittyContainer;
$container = app()->make(BittyContainer::class)->setClass(Colour::class);
use MatthewPageUK\BittyEnums\Support\Container as BittyContainer;
$favouriteColours = (new BittyContainer(Colour::class))
->set(Colour::Red)
->set(Colour::Green)
->set(Colour::Blue);
// Set values
$favouriteColours = app()->make(BittyContainer::class)
->setClass(Colour::class)
->set(Colour::Red)
->set(Colour::Green)
->set(Colour::Blue);
// Passing an array of values
$favouriteColours = app()->make(BittyContainer::class)
->setClass(Colour::class)
->set([Colour::Red, Colour::Green, Colour::Blue]);
// Unset a value
$favouriteColours->unset(Colour::Red);
// Check if the container has a value
if ($favouriteColours->has(Colour::Red)) {
echo 'Red is one of your favourite colours';
}
// Check if the container has any of the values
if ($favouriteColours->hasAny([Colour::Red, Colour::Green])) {
echo 'You like red or green';
}
// Check if the container has all of the values
if ($favouriteColours->hasAll([Colour::Red, Colour::Green])) {
echo 'You like red and green';
}
// Pass another container to check if any of the values exist
if ($product->colours->hasAny($favouriteColours)) {
echo 'This product is available in one of your favourite colours';
}
public function __construct(string $class, int $selected = 0);
public function clear(): BittyContainer;
public function getChoices(): array;
public function getValue(): int;
public function has(BittyEnum $choice): bool;
public function hasAll(array|BittyContainer $choices): bool;
public function hasAny(array|BittyContainer $choices): bool;
public function set(array|BittyContainer|BittyEnum $choice): BittyContainer;
public function setAll(): BittyContainer;
public function unset(array|BittyContainer|BittyEnum $choice): BittyContainer;
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedBigInteger('colours');
$table->unsignedInteger('price');
$table->timestamps();
});
use App\Enums\Colour;
use MatthewPageUK\BittyEnums\Casts\BittyEnumCast;
class Product extends Model
{
protected $casts = [
'colours' => BittyEnumCast::class . ':' . Colour:class,
];
}
// Check if value exists
$product = Product::find(1);
if ($product->colours->has(Colour::Blue)) {
echo 'This product is available in blue';
}
// Check if any of the values exist
$customerPreferences = app()->make(BittyContainer::class)
->setClass(Colour::class)
->set(Colour::Blue)
->set(Colour::Red)
->set(Colour::Green);
$product = Product::find(1);
if ($product->colours->hasAny($customerPreferences)) {
echo 'This product is available in one of the customers preferred colours';
}
use App\Enums\Colours;
use MatthewPageUK\BittyEnums\Traits\WithBittyEnumQueryScope;
class Product extends Model
{
use WithBittyEnumQueryScope;
...
}
// Products with the colour blue
Product::whereBittyEnumHas('colours', Colour::Blue)->get();
// Products with the colour blue or red
Product::whereBittyEnumHasAny('colours', [Colour::Blue, Colour::Red])->get();
// Products with the colour blue and red
Product::whereBittyEnumHasAll('colours', [Colour::Blue, Colour::Red])->get();
// Products without the colour blue
Product::whereBittyEnumDoesntHave('colours', Colour::Blue)->get();
// Products without the colour blue or red
$customerPreferences = new BittyEnumContainer(Colour::class)
->set(Colour::Blue)
->set(Colour::Red);
Product::whereBittyEnumDoesntHaveAny('colours', $customerPreferences)->get();
return [
'max_bits' => 16,
];
bash
php artisan bitty-enum:make Colours
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.