Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

A custom Doctrine type that maps column values to BitMask objects using yaroslavche/bitmask

License

Notifications You must be signed in to change notification settings

yaroslavche/doctrine-bitmask-type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example

# src\Entity\User.php

use BitMask\BitMaskInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class User
{
    const ROLE_USER = 1 << 0;
    const ROLE_EDITOR = 1 << 1;
    const ROLE_MODERATOR = 1 << 2;
    const ROLE_ADMIN = 1 << 3;
    const ROLE_SUPER_ADMIN = 1 << 4;

    // ...
    
    /**
     * bitmask roles
     * @ORM\Column(type="bitmask")
     */
    private $roles;

    public function getRoles(): BitMaskInterface
    {
        return $this->roles;
    }

    public function setRoles(BitMaskInterface $roles)
    {
        $this->roles = $roles;
    }

    public function hasRoleUser(): bool
    {
        return $this->getRoles()->isSetBit(static::ROLE_USER);
    }
    
    // ...
}
# src/Controller/TestController.php

    // ...
    $em = $this->getDoctrine()->getManager();
    $user = new User();
    $rolesBitMask = new BitMask(User::ROLE_USER | User::ROLE_EDITOR);
    $user->setRoles($rolesBitMask);
    $em->persist($user);
    $em->flush();
    $repo = $this->getDoctrine()->getRepository(User::class);
    foreach ($repo->findAll() as $user) {
        dump(
            'is admin: ' . $user->isAdmin(),
            'is admin: ' . $user->getRoles()->isSetBit(User::ROLE_ADMIN)
        );
    }
    // ...

About

A custom Doctrine type that maps column values to BitMask objects using yaroslavche/bitmask

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages