This commit is contained in:
Netkas 2023-06-21 02:09:13 -04:00
parent 0a8f7589b9
commit 1728d607d7
No known key found for this signature in database
GPG key ID: 5DAF58535614062B
16 changed files with 71 additions and 152 deletions

View file

@ -7,6 +7,7 @@
use FederationLib\Enums\Standard\PeerAssociationType;
use FederationLib\Enums\Standard\PermissionRole;
use FederationLib\Enums\Standard\UserPeerType;
use FederationLib\Exceptions\Standard\InvalidPeerMetadataException;
class Validate
{
@ -102,4 +103,37 @@
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 validateMetadata(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])));
}
}
}
}