PHP code example of codekandis / phlags

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

    

codekandis / phlags example snippets


class Permissions extends AbstractFlagable
{
    public const int READ    = 1;
    public const int WRITE   = 2;
    public const int EXECUTE = 4;
}

1
Permissions::READ
new Permissions( 1 )
new Permissions( Permission::READ )
new Permissions( 'READ' );

new Permission( 1 | 'READ' | new Permissions( READ ) )

// with the default flag 'Permissions::NONE' (inherited from `FlagableInterface::NONE`)
$permissions = new Permissions();

// with a single flag
$permissions = new Permissions( Permissions::READ );

// with multiple flags
$permissions = new Permissions( Permissions::READ | Permissions::WRITE );

// with another flagable
$permissions = new Permissions( new Permissions( Permissions::READ ) );

// with string representations
$permissions = new ( 'READ' );
$permissions = new ( 'READ|WRITE' );
$permissions = new ( 'READ|WRITE|EXECUTE' );

// with mixed string representations
$permissions = new ( '1' );
$permissions = new ( '1|2' );
$permissions = new ( '1|WRITE|4' );

$permissions = new Permissions( Permissions::READ );
echo $permissions->getValue();  // 1
echo $permissions();            // 1

$permissions = new Permissions( Permissions::READ | Permissions::WRITE );
$permissions->has( Permissions::READ );     // true
$permissions->has( Permissions::WRITE );    // true
$permissions->has( Permissions::EXECUTE );  // false

$permissions = new Permissions();
$permissions->set( Permissions::READ );
$permissions->unset( Permissions::READ );
$permissions->switch( Permissions::READ );
$permissions->has( Permissions::READ );     // true

$permissions = new Permissions();
$permissions->set( Permissions::READ )
            ->unset( Permissions::READ )
            ->switch( Permissions::READ )
            ->has( Permissions::READ );    // true

$permissions = new Permissions();
(string)$permissions->getValue();  // 0
(string)$permissions();            // 0
(string)$permissions;              // NONE
$permissions->__toString();        // NONE

$permissions = new Permissions( PERMISSIONS::READ | PERMISSIONS::EXECUTE );
(string)$permissions->getValue();  // 5
(string)$permissions();            // 5
(string)$permissions;              // READ|EXECUTE
$permissions->__toString();        // READ|EXECUTE

class Permissions extends AbstractFlagable SomeTraitfulInterface
{
    use SomeTraitfulExtension;

    public const int READ    = 1;
    public const int WRITE   = 2;
    public const int EXECUTE = 4;
}

$pathToFile = '/some-random-file.txt';
$permissions = new Permissions();
$permissions->ifSet( Permissions::DIRECTORY, is_dir( $pathToFile ) );
$permissions->has( Permissions::DIRECTORY );  // false

$pathToFile = '/some-random-directory';
$permissions = new Permissions();
$permissions->ifSet( Permissions::DIRECTORY, is_dir( $pathToDirectory ) );
$permissions->has( Permissions::DIRECTORY );  // true

try
{
    $permissions = new Permissions();
}
catch ( InvalidFlagableException $e )
{
    $errorMessages = $e->getErrorMessages();
}

try
{
    $permissions->set( $value );
}
catch ( InvalidValueException $e )
{
    $errorMessages = $e->getErrorMessages();
}