2017 © Pedro Peláez
 

library bitflag

Bitflag decorator for Ornament ORM

image

ornament/bitflag

Bitflag decorator for Ornament ORM

  • Saturday, March 17, 2018
  • by monomelodies
  • Repository
  • 1 Watchers
  • 0 Stars
  • 145 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 1 % Grown

The README.md

ornament/bitflag

Bitflag decorator for Ornament ORM, (*1)

For a model Foo with a property 'status', we often want to define a number of bitflags, e.g. 'on = 1', 'valid = 2' etc. The Bitflag decorator makes this easy., (*2)

Create a backed enum defining the desired flags. Type hint the property to receive the Bitflag as such, and add the attribute Ornament\Bitflag\Options to said property. The argument to Options is the classname of your enum., (*3)

All defined cases of the enum are now magically available for getting and setting as properties on bitflag property:, (*4)

<?php

<?php

use Ornament\Core;
use Ornament\Bitflag\{ Bitflag, Options };

enum Status : int
{
    case on = 1;
    case initialized = 2;
}

class Model
{
    use Core\Model;

    #[Options(Status::class)]
    public Bitflag $status;
}

$model = Model::fromIterable(['status' => 3]);

// Now this works, assuming `$model` is the instance:
var_dump($model->status->on); // true in this example, since 3 & 1 = 1
$model->status->on = true; // bit 1 is now on (status |= 1)
$model->status->on = false; // bit 1 is now off (status &= ~1)
var_dump($model->status->initialized); // true, since 2 & 2 = 2



Bitflag properties also support JSON serialization (via `Ornament\Bitflag\Property::jsonSerialize()`). A map of `true`/`false` values will be exported. Similarly, the `getArrayCopy` method will return a hash of flags/bits where the bit was set to true. ## Accessing the underlying bit value You don't need to; simply use the backed enum:
<?php echo Status::on->value; // 1

Similarly, use Status::cases() if you need all possible values., (*5)

The Versions

17/03 2018

dev-master

9999999-dev

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

17/03 2018

0.4.1

0.4.1.0

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

09/03 2018

dev-develop

dev-develop

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

09/03 2018

0.4.0

0.4.0.0

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

02/02 2018

0.3.1

0.3.1.0

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

27/01 2018

0.3.0

0.3.0.0

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

01/10 2017

0.2.2

0.2.2.0

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

01/10 2017

0.2.1

0.2.1.0

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

27/04 2017

0.2.0

0.2.0.0

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

25/09 2016

0.1.1

0.1.1.0

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst

25/09 2016

0.1.0

0.1.0.0

Bitflag decorator for Ornament ORM

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijn Ophorst