2017 © Pedro Peláez
 

library bitflagtrait

Trait for simple setting / getting bitflags

image

crypto_scythe/bitflagtrait

Trait for simple setting / getting bitflags

  • Thursday, May 12, 2016
  • by crypto_scythe
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

BitflagTrait

Simple trait for setting and getting bitflags in your classes, (*1)

Installing

Just with composer:, (*2)

composer require crypto_scythe/bitflagtrait

Usage (similiar to examples/useracl.php)

<?php

class UserACL {

  use \crypto_scythe\BitflagTrait;

  const FLAG_CREATE = 1;
  const FLAG_READ = 2;
  const FLAG_UPDATE = 4;
  const FLAG_DELETE = 8;

  protected $acl = 0;

  public function isCreateAllowed() {

    return $this->isBitflagSet( $this->acl, self::FLAG_CREATE );

  }

  public function isReadAllowed() {

    return $this->isBitflagSet( $this->acl, self::FLAG_READ );

  }

  public function isUpdateAllowed() {

    return $this->isBitflagSet( $this->acl, self::FLAG_UPDATE );

  }

  public function isDeleteAllowed() {

    return $this->isBitflagSet( $this->acl, self::FLAG_DELETE );

  }

  public function setCreateAllowed( $value ){

    $this->setFlag( $this->acl, self::FLAG_CREATE, $value );

  }

  public function setReadAllowed($value) {

    $this->setFlag( $this->acl, self::FLAG_READ, $value );

  }

  public function setUpdateAllowed($value) {

    $this->setFlag( $this->acl, self::FLAG_UPDATE, $value );

  }

  public function setDeleteAllowed($value){ 

    $this->setFlag( $this->acl, self::FLAG_DELETE, $value );

  }

  public function __toString() {

    $rights = array_filter( [
        $this->isCreateAllowed() ? 'CREATE' : false,
        $this->isReadAllowed() ? ' READ' : false,
        $this->isUpdateAllowed() ? 'UPDATE' : false,
        $this->isDeleteAllowed() ? 'DELETE' : false
    ] );

    return print_r( $rights, true );

  }

}

$userACL = new UserACL();
$userACL->setCreateAllowed( true );
$userACL->setReadAllowed( false );
$userACL->setUpdateAllowed( true );
$userACL->setDeleteAllowed( false );

echo $userACL;

The Versions

12/05 2016

dev-master

9999999-dev https://github.com/crypto-scythe/bitflagtrait

Trait for simple setting / getting bitflags

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Chris Fasel

php trait pattern bitflag

12/05 2016

1.0.0

1.0.0.0 https://github.com/crypto-scythe/bitflagtrait

Trait for simple setting / getting bitflags

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Chris Fasel

php trait pattern bitflag