PHP code example of buffalokiwi / buffalotools_types

1. Go to this page and download the library: Download buffalokiwi/buffalotools_types 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/ */

    

buffalokiwi / buffalotools_types example snippets


class EnumImpl extends Enum
{
  //..Optional class constants containing enum values 
  const KEY1 = 'key1';
  const KEY2 = 'key2';

  //..Optional $enum array property containing all possible enum values
  protected array $enum = [
    self::KEY1,
    self::KEY2
  ];

  //..Optional default value
  protected string $value = self::KEY1;

  //..Optional change event can be used
  protected function onChange( $oldVal, $newVal ) : void
  {
    //..Do something on change
  } 
}

//..Creating an enum using only constants 
class EnumImpl extends Enum
{
  const KEY1 = 'key1';
  const KEY2 = 'key2';
}

//..Creating an enum using the $enum property
class EnumImpl extends Enum
{
  protected array $enum = [
    'key1',
    'key2'
  ];
}

class ValuedEnumImpl extends Enum
{
  //..Optional class constants containing enum values 
  const KEY1 = 'key1';
  const KEY2 = 'key2';

  //..Required $enum array property containing all possible enum values.
  protected array $enum = [
    self::KEY1 => 'stored value 1',
    self::KEY2 => 'stored value 2'
  ];

  //..Optional default value
  protected string $value = self::KEY1;

  //..Optional change event can be used
  protected function onChange( $oldVal, $newVal ) : void
  {
    //..Do something on change
  } 
}

$enum = EnumImpl::KEY1();

$enum = new EnumImpl( EnumImpl::KEY1 );

$enum = new EnumImpl();
$enum->KEY1;

//..Or 
$enum->setValue( EnumImpl::KEY1 );

if ( $enum->KEY1()) {
  // do something
}

if ( $enum->is( EnumImpl::KEY1 )) {
  // do something
}

$enum->value();  //..returns 'key1'

//..Casting enum to a string will return a string equal to the current enum value:
echo (string)$enum;  //..Prints 'key1'

$enum->KEY2;  //..The enum now has a value of "key2"
$enum->setValue( EnumImpl::KEY2 );

if ( $enum->isValid( EnumImpl::KEY2 )) {
  // this is valid
}

if ( $enum->equals( $enum2 )) {  
  //..$enum2 is of the same type and has the same value as $enum
}

//..Outputs ['KEY1' => 'key1', 'KEY2' => 'key2']
$constants = $enum->constants();

//..Outputs: ['KEY1','KEY2']
$constants = $enum->keys();

//..Outputs: ['key1','key2'];
$values = $enum->values();

usort( $enumList, function( IEnum $a, IEnum $b ) {
  return $a->compare( $b );
});

$enum = new ValuedEnumImpl( ValuedEnumImpl::KEY1 ); //..Create a new valued enum equal to 'key1'
$enum->getStoredValue(); //..Returns 'stored value 1'

//..If you want to retrieve the enum value by stored value:
$enum->getByStoredValue( 'stored value 1' ); //..returns 'key1'


//..Retrieve a list of all stored values:
$enum->getStoredValues(); //..Returns ['stored value 1', 'stored value 2']

$index = $enum->indexOf( EnumImpl::KEY1 ); //..returns 0 

$enum = new EnumImpl( EnumImpl::KEY1 );
$enum->moveNext(); //..$enum now equals 'key2'
$enum->movePrevious(); //..$enum now equals 'key1'
$enum->movePrevious(); //..$enum still equals 'key1' and no exception is thrown

$enum2 = new EnumImpl( EnumImpl::KEY2 );

//..$enum has a value of 'key1'
$enum->greaterThan( $enum2 ); //..returns false.  
$enum->lessThan( $enum2 ); //..return true

$enum->greaterThanValue( EnumImpl::KEY2 ); //..return false
$enum->lessThanValue( EnumImpl::KEY2 ); //..returns true

$enum = new EnumImpl( EnumImpl::KEY1 );
$enum->changedFromTo( EnumImpl::KEY1, EnumImpl::KEY2 ); //..returns false 
$enum->setValue( EnumImpl::KEY2 );
$enum->changedFromTo( EnumImpl::KEY1, EnumImpl::KEY2 ); //..returns true 

$enum->changedTo( EnumImpl::KEY2 ); //..Returns true 

$enum->getChanges(); //..Returns [['key1' => 'key2']] when using above example

$enum = new EnumImpl( EnumImpl::KEY1 );

//..Add a change event.  Multiple events can be added.
$enum->setOnChange( function( IEnum $enum, string $old, string $newVal ) : void {
  //..Do something on change.

  //..Optionally, throw any exception to roll back the change.  
  //..The change log will not list failed changes.
  throw new \Exception( 'No change for you' );
});

try {
  //..An exception will be thrown here due to the change event.
  $enum->setValue( EnumImpl::KEY2 );
} catch( \Exception $e ) {
  //..Do nothing
}

$enum->value(); //..This will output 'key1' since the change event throws an exception.

$enum = new RuntimeEnum( ['key1', 'key2'], 'key1' );
 
$enum->value(); //..returns 'key1'

//..Change the value 
$enum->setValue( 'key2' ); 
$enum->value(); //..returns 'key2'

$b = new BitSet( 0 );  

$b = new BitSet( 0 );
$b->enable( 0x2 ); //..Bit 2 is now enabled 
$b->enableAt( 2 ); //..Bit 2 is enabled 
$b->setValue( 2 ); //..Bit 2 is enabled (This is the sum of all enabled bits)

$b->disable( 0x2 ); //..Bit 2 is now disabled 
$b->disableAt( 2 ); //..Bit 2 disabled
$b->setValue( 0 ); //..All bits are disabled 

$b->enable( 0x2 ); //..Enable bit 2
$b->toggle( 0x2 ); //..Bit 2 is now disabled 

$b->enable( 0x2 );
$b->isEnabled( 0x2 ); //..Returns true
$b->isEnabledAt( 2 ); //..Returns true 

$b->clear(); //..Disables all bits 
$b->getValue(); //.Returns zero
$b->enable( 0x1 ); //..Enable bit 1
$b->getValue(); //..Returns one
$b->enable( 0x2 ); //..Enable bit 2
$b->getValue(); //..Returns three
$b->disable( 0x1 ); //..Disable bit 1
$b->getValue(); //..Returns two 

class SetImpl 
{
  const BIT1 = 'bit1';
  const BIT2 = 'bit2';

  protected array $members = [
    self::BIT1,
    self::BIT2
  ];
}

$set = new SetImpl();

$set = new SetImpl( SetImpl::BIT1, 'bit2' );

$set = new SetImpl( ['bit1', 'bit2'] );

$set = new SetImpl();

$set->add( SetImpl::BIT1 ); //..Use the add method
$set->add( 0x1 ); //..Add method also accepts integers 
$set->BIT1 = true; //..Use magic.  The key is the class constant.  
$set->bit1 = true; //..Using magic, but with the bit's name and not the class constant 

$set = new SetImpl();
$set->remove( SetImpl::BIT1 ); 
$set->remove( 0x1 );
$set->BIT1 = false;
$set->bit2 = false;

$set = new SetImpl();
$set->BIT1; //..returns 1
$set->bit1; //..returns 1

$set = new SetImpl();
$set->isMember( 'bit1' ); //..returns true 
$set->isMember( 'bit1', 'bit2' ); //..returns true

$set = new SetImpl();
$set->add( 'bit1' ); 
$set->hasVal( 'bit1' ); //..Returns true
$set->hasVal( 'bit1', 'bit2' ); //..Returns false 

$set = new SetImpl();
$set->isEmpty(); //..Returns true 

$set = new SetImpl( 'bit1' );
$set->hasAny( 'bit1', 'bit2' ); //..Returns true since bit1 is enabled 

$set = new SetImpl();
$set->addMember( 'bit3' ); //..Add a new member to the bit set.
$set->isMember( 'bit3' ); //..Returns true 
$set->bit3; //..returns 0x3 

$set = new SetImpl();
$set->getMembers(); //..returns ['bit1', 'bit2'];

$set = new SetImpl();
$set->add( 'bit1' );
$set->getActiveMembers();  //..Returns ['bit1'];

$set = new SetImpl();
$set->getTotal(); //..Returns 3

$set = new RuntimeSet( ['bit1', 'bit2'], 'bit1' );

$set->isMember( 'bit1' ); //..returns true
$set->hasVal( 'bit1' ); //..returns true 

class MapSetImpl extends MapSet
{
  protected array $members = [
    'bit1' => 'value1',
    'bit2' => 'value2'
  ];
}

$set = new MapSetImpl();
$set->get( 'bit1' ); //..returns 'value1'