2021-08-24 20:29:26 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-08-24 20:29:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the ActivityPhp package.
|
|
|
|
*
|
|
|
|
* Copyright (c) landrok at github.com/landrok
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please see
|
|
|
|
* <https://github.com/landrok/activitypub/blob/master/LICENSE>.
|
|
|
|
*/
|
|
|
|
|
2021-10-04 17:00:58 +01:00
|
|
|
namespace Plugin\ActivityPub\Util\Type;
|
2021-08-24 20:29:26 +01:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
|
|
|
/**
|
2021-10-04 17:00:58 +01:00
|
|
|
* \Plugin\ActivityPub\Util\Type\Validator is an abstract class for
|
2021-08-24 20:29:26 +01:00
|
|
|
* attribute validation.
|
|
|
|
*/
|
|
|
|
abstract class Validator
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Contains all custom validators
|
|
|
|
*
|
2021-09-06 23:47:28 +01:00
|
|
|
* @var array<string, ValidatorInterface>
|
2021-08-24 20:29:26 +01:00
|
|
|
*
|
|
|
|
* [ 'attributeName' => CustomValidatorClassName::class ]
|
|
|
|
*/
|
|
|
|
protected static array $validators = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate an attribute value for given attribute name and
|
|
|
|
* container object.
|
|
|
|
*
|
2021-10-10 09:26:18 +01:00
|
|
|
* @param mixed $container An object
|
2021-08-24 20:29:26 +01:00
|
|
|
*
|
|
|
|
* @throws Exception if $container is not an object
|
|
|
|
*/
|
|
|
|
public static function validate(string $name, mixed $value, mixed $container): bool
|
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
if (!\is_object($container)) {
|
2021-08-24 20:29:26 +01:00
|
|
|
throw new Exception(
|
2021-10-10 09:26:18 +01:00
|
|
|
'Given container is not an object',
|
2021-08-24 20:29:26 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform validation
|
|
|
|
if (isset(self::$validators[$name])) {
|
|
|
|
return self::$validators[$name]->validate(
|
|
|
|
$value,
|
2021-10-10 09:26:18 +01:00
|
|
|
$container,
|
2021-08-24 20:29:26 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to load a default validator
|
|
|
|
$validatorName = sprintf(
|
2021-10-04 17:00:58 +01:00
|
|
|
'\Plugin\ActivityPub\Util\Type\Validator\%sValidator',
|
2021-10-10 09:26:18 +01:00
|
|
|
ucfirst($name),
|
2021-08-24 20:29:26 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (class_exists($validatorName)) {
|
|
|
|
self::add($name, $validatorName);
|
|
|
|
return self::validate($name, $value, $container);
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is no validator for this attribute
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new validator in the pool.
|
|
|
|
* It checks that it implements Validator\Interface
|
|
|
|
*
|
2021-10-10 09:26:18 +01:00
|
|
|
* @param string $name an attribute name to validate
|
2021-08-24 20:29:26 +01:00
|
|
|
* @param object|string $class A validator class name
|
|
|
|
*
|
|
|
|
* @throws Exception if validator class does not implement
|
2021-10-04 17:00:58 +01:00
|
|
|
* \Plugin\ActivityPub\Util\Type\Helper\ValidatorInterface
|
2021-08-24 20:29:26 +01:00
|
|
|
*/
|
|
|
|
public static function add(string $name, object|string $class): void
|
|
|
|
{
|
|
|
|
$validator = new $class();
|
|
|
|
|
|
|
|
if (!($validator instanceof ValidatorInterface)) {
|
|
|
|
throw new Exception(
|
|
|
|
sprintf(
|
|
|
|
'Validator "%s" MUST implement "%s" interface',
|
2021-10-10 09:26:18 +01:00
|
|
|
\get_class($validator),
|
|
|
|
ValidatorInterface::class,
|
|
|
|
),
|
2021-08-24 20:29:26 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$validators[$name] = $validator;
|
|
|
|
}
|
|
|
|
}
|