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
}
$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']
$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->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->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
$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->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