152 lines
No EOL
4.7 KiB
PHP
152 lines
No EOL
4.7 KiB
PHP
<?php
|
|
|
|
namespace FederationLib\Classes;
|
|
|
|
use FederationLib\Enums\Standard\PeerType;
|
|
use FederationLib\Enums\Standard\InternetPeerType;
|
|
use FederationLib\Enums\Standard\PeerAssociationType;
|
|
use FederationLib\Enums\Standard\UserPeerType;
|
|
use FederationLib\Exceptions\Standard\InvalidPeerMetadataException;
|
|
|
|
class Validate
|
|
{
|
|
/**
|
|
* Determines the entity type based on the entity type string.
|
|
*
|
|
* @param string $entity_type
|
|
* @return string
|
|
*/
|
|
public static function getEntityType(string $entity_type): string
|
|
{
|
|
if(in_array($entity_type, InternetPeerType::ALL))
|
|
{
|
|
return PeerType::INTERNET;
|
|
}
|
|
|
|
if(in_array($entity_type, UserPeerType::ALL))
|
|
{
|
|
return PeerType::USER;
|
|
}
|
|
|
|
return PeerType::UNKNOWN;
|
|
}
|
|
|
|
/**
|
|
* Determines if the entity type is valid and supported.
|
|
*
|
|
* @param string $entity_type
|
|
* @return bool
|
|
*/
|
|
public static function validateEntityType(string $entity_type): bool
|
|
{
|
|
return self::getEntityType($entity_type) !== PeerType::UNKNOWN;
|
|
}
|
|
|
|
/**
|
|
* Validates the peer association type.
|
|
*
|
|
* @param string $type
|
|
* @return bool
|
|
*/
|
|
public static function peerAssociationType(string $type): bool
|
|
{
|
|
if (in_array(strtolower($type), PeerAssociationType::ALL))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Validates a client name based on certain criteria.
|
|
*
|
|
* The client name must be alphanumeric, allowing spaces, periods, dashes, and underscores,
|
|
* with a minimum length of 3 characters and a maximum length of 42 characters.
|
|
*
|
|
* @param string $name The client name to validate
|
|
* @return bool Returns true if the client name is valid, false otherwise
|
|
*/
|
|
public static function clientName(string $name): bool
|
|
{
|
|
if (!preg_match('/^[a-zA-Z0-9\s\.\-_]+$/', $name))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
$length = strlen($name);
|
|
return !($length < 3 || $length > 42);
|
|
}
|
|
|
|
/**
|
|
* Validates a client description based on certain criteria.
|
|
*
|
|
* @param string $description The client description to validate
|
|
* @return bool Returns true if the client description is valid, false otherwise
|
|
*/
|
|
public static function clientDescription(string $description): bool
|
|
{
|
|
$length = strlen($description);
|
|
return !($length < 3 || $length > 255);
|
|
}
|
|
|
|
/**
|
|
* Validates if the given permission role is valid.
|
|
*
|
|
* @param string|int $role
|
|
* @return bool
|
|
*/
|
|
public static function permissionRole(string|int $role): bool
|
|
{
|
|
return (int)$role >= 0 && (int)$role <= 5;
|
|
}
|
|
|
|
/**
|
|
* Validates the given metadata for a peer.
|
|
*
|
|
* @param array $data
|
|
* @param array $required
|
|
* @param array $optional
|
|
* @return void
|
|
* @throws InvalidPeerMetadataException
|
|
*/
|
|
public static function metadata(array $data, array $required, array $optional): void
|
|
{
|
|
foreach($required as $property => $type)
|
|
{
|
|
if(!isset($data[$property]))
|
|
{
|
|
throw new InvalidPeerMetadataException(sprintf('The property "%s" is required in the metadata', $property));
|
|
}
|
|
|
|
if(gettype($data[$property]) !== $type)
|
|
{
|
|
throw new InvalidPeerMetadataException(sprintf('The property "%s" must be a %s in metadata, got %s', $property, $type, Security::gettype($data[$property])));
|
|
}
|
|
}
|
|
|
|
foreach($optional as $property => $type)
|
|
{
|
|
if(isset($data[$property]) && gettype($data[$property]) !== $type)
|
|
{
|
|
throw new InvalidPeerMetadataException(sprintf('The property "%s" must be a %s in metadata, got %s', $property, $type, Security::gettype($data[$property])));
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Validates if the given association type is valid.
|
|
*
|
|
* @param string $type
|
|
* @return bool
|
|
*/
|
|
public static function associationType(string $type): bool
|
|
{
|
|
if(in_array($type, PeerAssociationType::ALL))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
} |