biboletin / enum
Enums
Installs: 10
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/biboletin/enum
Requires (Dev)
- phpstan/phpstan: 2.1.x-dev
- phpunit/phpunit: ^12
- squizlabs/php_codesniffer: *
- symfony/var-dumper: 7.3.x-dev
- vimeo/psalm: ^7.0@dev
README
A PHP library providing a collection of enum classes for various purposes including cryptographic operations, versioning, and caching.
Installation
You can install the package via composer:
composer require biboletin/enum
Requirements
- PHP 8.1 or higher (PHP enums are required)
Available Enums
HashAlgorithm
An enum representing different hash algorithms for cryptographic operations.
Available Cases:
SHA256: SHA-256 hash algorithm (256-bit)SHA384: SHA-384 hash algorithm (384-bit)SHA512: SHA-512 hash algorithm (512-bit)
Methods:
length(): Returns the length of the hash output in bytesdigest(string $data, bool $binary = true): Generates a hash digest of the provided datahmac(string $data, string $key, bool $binary = true): Generates an HMAC using the specified keyfromString(string $value): Creates a HashAlgorithm instance from a string value
Example Usage:
use Biboletin\Enum\HashAlgorithm; // Get hash length $length = HashAlgorithm::SHA256->length(); // Returns 32 // Generate a hash digest $digest = HashAlgorithm::SHA256->digest('Hello, World!'); // Generate an HMAC $hmac = HashAlgorithm::SHA256->hmac('Hello, World!', 'secret_key'); // Create from string $algorithm = HashAlgorithm::fromString('sha256'); // Returns HashAlgorithm::SHA256
CipherAlgorithm
An enum defining various cipher algorithms used for encryption.
Available Cases:
- AES in CBC mode:
AES_128_CBC,AES_192_CBC,AES_256_CBC - AES in GCM mode:
AES_128_GCM,AES_192_GCM,AES_256_GCM - ChaCha20-Poly1305:
CHACHA20_POLY1305 - SM4 in various modes:
SM4_GCM,SM4_CCM,SM4_CTR,SM4_CBC
Example Usage:
use Biboletin\Enum\CipherAlgorithm; // Use a specific cipher algorithm $cipher = CipherAlgorithm::AES_256_GCM; // Get the string value $cipherValue = $cipher->value; // Returns 'aes-256-gcm'
CacheDriver
An enum representing different cache drivers that can be used in an application.
Available Cases:
APCU: APC User CacheFILE: File-based cachingMEMORY: In-memory cachingREDIS: Redis cachingMEMCACHED: Memcached cachingDATABASE: Database caching
Methods:
fromString(string $value): Creates a CacheDriver instance from a string valueisValid(string $value): Checks if a string value is a valid cache driverall(): Returns an array of all cache driver enum instancestoArray(): Converts the enum instance to an associative arraygetLowercaseName(): Returns the name in lowercasegetUppercaseName(): Returns the name in uppercasegetName(): Returns the namegetValue(): Returns the valuegetLowercaseValue(): Returns the value in lowercasegetUppercaseValue(): Returns the value in uppercasegetTitleCaseValue(): Returns the value in title case
Example Usage:
use Biboletin\Enum\CacheDriver; // Create from string $driver = CacheDriver::fromString('redis'); // Returns CacheDriver::REDIS // Check if a value is valid $isValid = CacheDriver::isValid('redis'); // Returns true // Get all cache drivers $allDrivers = CacheDriver::all(); // Convert to array $driverArray = CacheDriver::REDIS->toArray(); // Returns ['name' => 'REDIS', 'value' => 'redis'] // Get formatted names/values $name = CacheDriver::REDIS->getName(); // Returns 'REDIS' $value = CacheDriver::REDIS->getValue(); // Returns 'redis' $upperValue = CacheDriver::REDIS->getUppercaseValue(); // Returns 'REDIS' $titleValue = CacheDriver::REDIS->getTitleCaseValue(); // Returns 'Redis'
AppVersion
An enum defining the version of the application.
Available Cases:
V1_0_0: Version 1.0.0 of the application
Methods:
withPrefix(string $prefix): Returns the version value with a specified prefix
Example Usage:
use Biboletin\Enum\AppVersion; // Get the version $version = AppVersion::V1_0_0; // Returns AppVersion::V1_0_0 // Get the version with a prefix $versionWithPrefix = AppVersion::V1_0_0->withPrefix('v'); // Returns 'v1.0.0'
ApiVersion
An enum defining the API version used in the application.
Available Constants:
V1: Version 1 of the API
Example Usage:
use Biboletin\Enum\ApiVersion; // Get the API version $apiVersion = ApiVersion::V1; // Returns 'v1'
CryptoVersion
An enum defining the version of the cryptographic protocol used.
Available Cases:
V1: Version 1 of the cryptographic protocol
Example Usage:
use Biboletin\Enum\CryptoVersion; // Get the crypto version $cryptoVersion = CryptoVersion::V1; // Returns CryptoVersion::V1 // Get the string value $versionValue = CryptoVersion::V1->value; // Returns 'v1'
DatabaseDriver
An enum representing different database drivers that can be used in an application.
Available Cases:
MYSQL: MySQL database driverPOSTGRESQL: PostgreSQL database driverSQLITE: SQLite database driverMONGODB: MongoDB database driverSQLSERVER: SQL Server database driverORACLE: Oracle database driverREDIS: Redis database driverCASSANDRA: Cassandra database driverDYNAMODB: DynamoDB database driverCOUCHBASE: Couchbase database driverELASTICSEARCH: Elasticsearch database driverCLICKHOUSE: ClickHouse database driverMARIADB: MariaDB database driverCOCKROACHDB: CockroachDB database driverFIREBIRD: Firebird database driver
Methods:
fromString(string $value): Creates a DatabaseDriver instance from a string valueisValid(string $value): Checks if a string value is a valid database drivergetAllDrivers(): Returns an array of all database driver enum instancesgetLowercaseName(): Returns the name in lowercasegetUppercaseName(): Returns the name in uppercasegetTitleCaseName(): Returns the name in title casegetSnakeCaseName(): Returns the name in snake casegetKebabCaseName(): Returns the name in kebab case
Example Usage:
use Biboletin\Enum\DatabaseDriver; // Create from string $driver = DatabaseDriver::fromString('mysql'); // Returns DatabaseDriver::MYSQL // Check if a value is valid $isValid = DatabaseDriver::isValid('mysql'); // Returns true // Get all database drivers $allDrivers = DatabaseDriver::getAllDrivers(); // Get formatted names $lowercaseName = DatabaseDriver::MYSQL->getLowercaseName(); // Returns 'mysql' $uppercaseName = DatabaseDriver::MYSQL->getUppercaseName(); // Returns 'MYSQL' $titleCaseName = DatabaseDriver::MYSQL->getTitleCaseName(); // Returns 'Mysql' $snakeCaseName = DatabaseDriver::MYSQL->getSnakeCaseName(); // Returns 'mysql' $kebabCaseName = DatabaseDriver::MYSQL->getKebabCaseName(); // Returns 'mysql'
HttpStatus
An enum representing HTTP status codes as defined by IETF RFCs.
Available Cases:
- Informational responses (100–199)
- Successful responses (200–299)
- Redirection messages (300–399)
- Client error responses (400–499)
- Server error responses (500–599)
Methods:
message(): Returns the message associated with the status codecategory(): Returns the category of the status coderesolve(int $code): Resolves a status code to its corresponding enum caseisClientError(): Checks if the status code is a client errorisServerError(): Checks if the status code is a server errorisSuccess(): Checks if the status code is a successisRedirection(): Checks if the status code is a redirectionisInformational(): Checks if the status code is informationaltoArray(): Converts the enum instance to an array
Example Usage:
use Biboletin\Enum\HttpStatus; // Get a status code $status = HttpStatus::OK; // 200 OK // Get the message $message = HttpStatus::NOT_FOUND->message(); // Returns 'Not Found' // Check the category $isClientError = HttpStatus::NOT_FOUND->isClientError(); // Returns true // Resolve from code $status = HttpStatus::resolve(404); // Returns HttpStatus::NOT_FOUND
StatusCodeCategory
An enum representing categories of HTTP status codes.
Available Cases:
INFORMATIONAL: Informational responses (100–199)SUCCESS: Successful responses (200–299)REDIRECTION: Redirection messages (300–399)CLIENT_ERROR: Client error responses (400–499)SERVER_ERROR: Server error responses (500–599)
Methods:
fromString(string $value): Creates a StatusCodeCategory instance from a string valueisValid(string $value): Checks if a string value is a valid status code categorygetReadableName(): Returns the category name in a human-readable formatall(): Returns an array of all status code categoriestoArray(): Converts the enum instance to an arraygetLowercaseName(): Returns the name in lowercasegetUppercaseName(): Returns the name in uppercasegetTitleCaseName(): Returns the name in title case
Example Usage:
use Biboletin\Enum\StatusCodeCategory; // Create from string $category = StatusCodeCategory::fromString('client error'); // Returns StatusCodeCategory::CLIENT_ERROR // Check if a value is valid $isValid = StatusCodeCategory::isValid('client error'); // Returns true // Get a readable name $readableName = StatusCodeCategory::CLIENT_ERROR->getReadableName(); // Returns 'Client Error' // Get all categories $allCategories = StatusCodeCategory::all();
RedirectType
An enum representing different types of HTTP redirects.
Available Cases:
TEMPORARY: Temporary redirect (302)PERMANENT: Permanent redirect (301)SEE_OTHER: See other (303)NOT_MODIFIED: Not modified (304)USE_PROXY: Use proxy (305)SWITCH_PROXY: Switch proxy (306)TEMPORARY_REDIRECT: Temporary redirect (307)PERMANENT_REDIRECT: Permanent redirect (308)MULTIPLE_CHOICES: Multiple choices (300)
Methods:
fromInt(int $value): Creates a RedirectType instance from an integer valueisValid(int $value): Checks if an integer value is a valid redirect typetoInt(): Returns the redirect type as an integertoString(): Returns the redirect type as a stringall(): Returns an array of all redirect typestoArray(): Converts the enum instance to an array
Example Usage:
use Biboletin\Enum\RedirectType; // Create from integer $redirectType = RedirectType::fromInt(301); // Returns RedirectType::PERMANENT // Check if a value is valid $isValid = RedirectType::isValid(301); // Returns true // Convert to integer $code = RedirectType::PERMANENT->toInt(); // Returns 301 // Convert to string $description = RedirectType::PERMANENT->toString(); // Returns 'Permanent Redirect'
HttpMethod
An enum representing HTTP methods used in web requests.
Available Cases:
GET: Retrieve dataPOST: Submit dataPUT: Update dataDELETE: Delete dataHEAD: Same as GET but without the response bodyOPTIONS: Describe the communication optionsPATCH: Partially update dataTRACE: Perform a message loop-back testCONNECT: Establish a tunnel to the server
Methods:
isSafe(): Checks if the method is safe (doesn't alter the server state)isIdempotent(): Checks if the method is idempotent (can be called multiple times with the same effect)isCacheable(): Checks if the method is cacheableisWriteOperation(): Checks if the method is a write operationisReadOperation(): Checks if the method is a read operationfromString(string $method): Creates an HttpMethod instance from a string valueall(): Returns an array of all HTTP methodsisValidMethod(string $method): Checks if a string value is a valid HTTP method- Various methods to get the HTTP method from server request in different formats
Example Usage:
use Biboletin\Enum\HttpMethod; // Create from string $method = HttpMethod::fromString('GET'); // Returns HttpMethod::GET // Check properties $isSafe = HttpMethod::GET->isSafe(); // Returns true $isIdempotent = HttpMethod::GET->isIdempotent(); // Returns true $isCacheable = HttpMethod::GET->isCacheable(); // Returns true $isWriteOperation = HttpMethod::POST->isWriteOperation(); // Returns true $isReadOperation = HttpMethod::GET->isReadOperation(); // Returns true // Get all methods $allMethods = HttpMethod::all();
ContentType
An enum representing MIME content types used in HTTP requests and responses.
Available Cases:
JSON: application/jsonXML: application/xmlHTML: text/htmlFORM_URLENCODED: application/x-www-form-urlencodedTEXT: text/plainMULTIPART_FORM_DATA: multipart/form-dataOCTET_STREAM: application/octet-stream- And many others
Methods:
isJson(),isXml(),isHtml(), etc.: Check if the content type is of a specific typetoString(): Returns the content type as a stringfromString(string $value): Creates a ContentType instance from a string valueall(): Returns an array of all content typesisValid(string $value): Checks if a string value is a valid content typefromHeader(string $header): Creates a ContentType instance from an HTTP headerfromFileExtension(string $extension): Creates a ContentType instance from a file extension- Various methods to determine content type from file paths, names, etc.
Example Usage:
use Biboletin\Enum\ContentType; // Create from string $contentType = ContentType::fromString('application/json'); // Returns ContentType::JSON // Check type $isJson = ContentType::JSON->isJson(); // Returns true // Convert to string $string = ContentType::JSON->toString(); // Returns 'application/json' // Create from file extension $contentType = ContentType::fromFileExtension('json'); // Returns ContentType::JSON
Environment
An enum representing different application environments.
Available Cases:
DEVELOPMENT: Development environmentPRODUCTION: Production environmentTESTING: Testing environmentSTAGING: Staging environment
Methods:
isDevelopment(): Checks if the environment is developmentisProduction(): Checks if the environment is productionisTesting(): Checks if the environment is testingisStaging(): Checks if the environment is stagingfromString(string $value): Creates an Environment instance from a string valueall(): Returns an array of all environmentsisValid(string $value): Checks if a string value is a valid environment
Example Usage:
use Biboletin\Enum\Environment; // Create from string $env = Environment::fromString('development'); // Returns Environment::DEVELOPMENT // Check environment $isDev = Environment::DEVELOPMENT->isDevelopment(); // Returns true $isProd = Environment::PRODUCTION->isProduction(); // Returns true // Get all environments $allEnvs = Environment::all(); // Check if a value is valid $isValid = Environment::isValid('development'); // Returns true
LogLevel
An enum representing different log levels for application logging.
Available Cases:
Emergency: System is unusableAlert: Action must be taken immediatelyCritical: Critical conditionsError: Error conditionsWarning: Warning conditionsNotice: Normal but significant conditionInfo: Informational messagesDebug: Debug-level messagesNone: No logging
Methods:
isEmergency(),isAlert(),isCritical(), etc.: Check if the log level is of a specific typetoString(): Returns the log level as a stringfromString(string $value): Creates a LogLevel instance from a string valueall(): Returns an array of all log levelsisValid(string $value): Checks if a string value is a valid log level
Example Usage:
use Biboletin\Enum\LogLevel; // Create from string $logLevel = LogLevel::fromString('error'); // Returns LogLevel::Error // Check level $isError = LogLevel::Error->isError(); // Returns true // Convert to string $string = LogLevel::Error->toString(); // Returns 'error' // Get all log levels $allLevels = LogLevel::all(); // Check if a value is valid $isValid = LogLevel::isValid('error'); // Returns true
License
The MIT License (MIT). Please see the License File for more information.