Add new Enums to enhance error handling and session management

This commit is contained in:
netkas 2024-09-13 13:50:50 -04:00
parent 2cf1a55e8f
commit 178e16627f
7 changed files with 197 additions and 0 deletions

View file

@ -0,0 +1,59 @@
<?php
namespace Socialbox\Enums;
/**
* Enumeration of standard header names used in HTTP communication.
*/
enum StandardHeaders : string
{
case CONTENT_TYPE = 'Content-Type';
case CLIENT_NAME = 'Client-Name';
case CLIENT_VERSION = 'Client-Version';
case SESSION_UUID = 'Session-UUID';
case FROM_PEER = 'From-Peer';
case SIGNATURE = 'Signature';
/**
* Determines if the current instance is required based on its type.
*
* @return bool Returns true if the instance is of type CONTENT_TYPE, CLIENT_VERSION, or CLIENT_NAME; otherwise, false.
*/
public function isRequired(): bool
{
return match($this)
{
self::CONTENT_TYPE,
self::CLIENT_VERSION,
self::CLIENT_NAME
=> true,
default => false,
};
}
/**
* Retrieves an array of required headers.
*
* @return array An array containing only the headers that are marked as required.
*/
public static function getRequiredHeaders(): array
{
/** @var StandardHeaders $header */
return array_filter(StandardHeaders::toArray(), fn($header) => $header->isRequired());
}
/**
* @return array
*/
public static function toArray(): array
{
$results = [];
foreach(StandardHeaders::values() as $header)
{
$results[$header->getValue()] = $header;
}
return $results;
}
}