PHP code example of bakame / aide-enums

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

    

bakame / aide-enums example snippets


<?ph

HttpMethod::fromName('Get') === HttpMethod::Get;
HttpMethod::tryFromName('Get') === HttpMethod::Get;
HttpMethod::tryFromName('Unknown'); // returns null
HttpMethod::tryFrom('Put') === HttpMethod::Put
HttpMethod::From('Unknown'); //throw a ValueError



use Bakame\Aide\Enum\Factory;

enum HttpMethod
{
    use Factory;

    case Get;
    case Post;
    case Put;
    case Head;
    case Options;
}



HttpMethod::size();        //returns the number of cases
HttpMethod::isBacked();
HttpMethod::isPure();      // returns the inverse of the `isBacked` method
HttpMethod::names();       // returns a list of all the names in the enumeration
HttpMethod::values();      // returns a list of all the names in the enumeration
HttpMethod::nameOf(404);   // returns the name associated with the given value
                           // or null if it does not exist for the submitted value.



use Bakame\Aide\Enum\Info;

enum HttpMethod: string
{
    use Info;

    case Get = 'GET';
    case Post = 'POST';
    case Put = 'PUT';
    case Head = 'HEAD';
    case Options = 'OPTIONS'
}



HttpMethod::hasName('GET'); //returns false;
HttpMethod::hasValue('GET'); //returns true;
HttpMethod::has('Head'); //returns true;
HttpMethod::hasCase('Header', 'HEAD'); //returns false;



use Bakame\Aide\Enum\Hasser;

enum HttpMethod: string
{
    use Hasser;

    case Get = 'GET';
    case Post = 'POST';
    case Put = 'PUT';
    case Head = 'HEAD';
    case Options = 'OPTIONS'
}



HttpMethod::Get->equals(HttpMethod::Post);      //returns false
HttpMethod::Get->isOneOf('GET', 'Get', 'get');  //returns true because `Get` is present
HttpMethod::Get->notEquals('get');              //returns true;
HttpMethod::Get->isNotOneOf('Head');            //returns true;



use Bakame\Aide\Enum\Compare;

enum HttpMethod: string
{
    use Compare;

    case Get = 'GET';
    case Post = 'POST';
    case Put = 'PUT';
    case Head = 'HEAD';
    case Options = 'OPTIONS'
}



HttpMethod::toAssociative(); // returns tha associative array
HttpMethod::toJavaScriptObject();  // returns a JavaScript object equivalent code as string
HttpMethod::toJavaScriptClass();  // returns a JavaScript class equivalent code as string



use Bakame\Aide\Enum\Convert;

enum HttpMethod: string
{
    use Convert;

    case Get = 'GET';
    case Post = 'POST';
    case Put = 'PUT';
    case Head = 'HEAD';
    case Options = 'OPTIONS'
}



use Bakame\Aide\Enum\Helper;

enum HttpMethod: string
{
    use Helper;

    case Get = 'GET';
    case Post = 'POST';
    case Put = 'PUT';
    case Head = 'HEAD';
    case Options = 'OPTIONS'
}

enum HttpStatusCode: int
{
    case HTTP_OK = 200;
    case HTTP_REDIRECTION = 302;
    case HTTP_NOT_FOUND = 404;
    case HTTP_SERVER_ERROR = 500;
}

use Bakame\Aide\Enum\JavaScriptConverter;

echo JavaScriptConverter::new()->convertToObject(HttpStatusCode::class);

echo JavaScriptConverter::new()->convertToClass(HttpStatusCode::class);


use Bakame\Aide\Enum\JavaScriptConverter;
use Illuminate\Support\Str;

$converter = JavaScriptConverter::new()
    ->useImmutability()
    ->useExportDefault()
    ->useTrailingComma()
    ->useSymbol()
    ->indentSize(4)
    ->propertyNameCase(
        fn (string $name) => Str::of($name)->replace('HTTP_', '')->lower()->studly()->toString()
    );

echo $converter->convertToObject(HttpStatusCode::class, 'StatusCode');

enum Color
{
    case Red;
    case Blue;
    case Green;
}

use Bakame\Aide\Enum\JavaScriptConverter;

echo JavaScriptConverter::new()->convertToObject(Color::class);

use Bakame\Aide\Enum\JavaScriptConverter;

echo JavaScriptConverter::new()
    ->ignoreSymbol()
    ->valueStartAt(2)
    ->convertToClass(Color::class, 'Colour');