stringkey/bit-magic-bundle

Symfony bundle that allows you to do bit manipulations in code and with symfony forms

Installs: 27

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:symfony-bundle

0.0.7 2025-01-01 13:59 UTC

This package is not auto-updated.

Last update: 2025-07-02 16:24:50 UTC


README

Toolkit to manipulate bitfields in code and symfony forms

Using the bundle

Installation

To include the bundle in your Symfony 6.4 LTS or Symfony 7 project, run:

composer require stringkey/bit-magic-bundle

Creating a Symfony 6.4 demo app

Create a new Symfony 6.4 LTS project

symfony new bit-bundle-test --version=lts

Include the minimum required bundles

composer require maker orm form validator twig-bundle security-csrf

Setup the database connection string, since the bundle works with standard integer types there should be no issues with any database create a .env.local file

cp .env .env.local

And create the database

php bin/console doctrine:database:create

Create an entity

php bin/console make:entity BitmaskTest

Add 2 integer properties and name them

  • enableMask
  • valueMask

After creating the entity, generate the migration and execute it

php bin/console doc:mig:diff
php bin/console doc:mig:mig
php bin/console make:crud BitmaskTest

Run the symfony development server

symfony serve

And navigate to http://localhost:8000/bitmask/test

When clicking new a form with 2 fields are shown, modify the code in: src/Form/BitmaskTestType.php

Don't forget to include the usages

use Stringkey\BitMagicBundle\Form\BitMaskType;
use Stringkey\BitMagicBundle\Utilities\BitOperations;

Modify the build form method

    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $choices = BitOperations::createOptions(16, 0xffff); // creates the choice fields

        /** @var BitmaskTest $bitmaskTest */
        $bitmaskTest = $builder->getData(); // fetch the entity
        $enableOption = ['enable_mask' => $bitmaskTest->getEnableMask(), 'choices' => $choices];

        $builder->add('enableMask', BitMaskType::class, ['choices' => $choices]);
        $builder->add('valueMask', BitMaskType::class, $enableOption);
    }

Refresh the create page the form should now show 2 rows of checkboxes the top one controls which fields in the bottom row are enabled