From e6a4d5d508fc87991c27b917ce7671db5abfdbd6 Mon Sep 17 00:00:00 2001 From: Sebastian Meyer Date: Wed, 10 Jan 2024 11:45:39 +0100 Subject: [PATCH] Use regular expressions for validation --- src/Entity.php | 51 +++++++++++++++++++++---------------------- src/Entity/Format.php | 6 ++--- src/Entity/Record.php | 6 ++++- src/Entity/Set.php | 16 ++++++++++++-- 4 files changed, 47 insertions(+), 32 deletions(-) diff --git a/src/Entity.php b/src/Entity.php index 476a6b2..1b5d69a 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -35,25 +35,44 @@ use Symfony\Component\Validator\Validation; abstract class Entity { /** - * Check if a string does not contain any whitespaces. + * Check if a string is a valid URL. + * + * @param string $url The URL + * + * @return string The validated URL + * + * @throws ValidationFailedException + */ + protected function validateUrl(string $url): string + { + $url = trim($url); + $validator = Validation::createValidator(); + $violations = $validator->validate($url, new Assert\Url()); + if ($violations->count() > 0) { + throw new ValidationFailedException(null, $violations); + } + return $url; + } + + /** + * Check if a string matches a given regular expression. * * @param string $string The string + * @param string $regEx The regular expression * * @return string The validated string * * @throws ValidationFailedException */ - protected function validateNoWhitespace(string $string): string + protected function validateRegEx(string $string, string $regEx): string { - $string = trim($string); $validator = Validation::createValidator(); $violations = $validator->validate( $string, [ new Assert\Regex([ - 'pattern' => '/\s/', - 'match' => false, - 'message' => 'This value contains whitespaces.' + 'pattern' => $regEx, + 'message' => 'This value does not match the regular expression "{{ pattern }}".' ]), new Assert\NotBlank() ] @@ -64,26 +83,6 @@ abstract class Entity return $string; } - /** - * Check if a string is a valid URI. - * - * @param string $uri The URI - * - * @return string The validated URI - * - * @throws ValidationFailedException - */ - protected function validateUri(string $uri): string - { - $uri = trim($uri); - $validator = Validation::createValidator(); - $violations = $validator->validate($uri, new Assert\Url()); - if ($violations->count() > 0) { - throw new ValidationFailedException(null, $violations); - } - return $uri; - } - /** * Check if a string is well-formed XML. * diff --git a/src/Entity/Format.php b/src/Entity/Format.php index 87cc1ab..7c7f940 100644 --- a/src/Entity/Format.php +++ b/src/Entity/Format.php @@ -97,7 +97,7 @@ class Format extends Entity public function setNamespace(string $namespace): void { try { - $this->namespace = $this->validateUri($namespace); + $this->namespace = $this->validateUrl($namespace); } catch (ValidationFailedException $exception) { throw $exception; } @@ -115,7 +115,7 @@ class Format extends Entity public function setSchema(string $schema): void { try { - $this->xmlSchema = $this->validateUri($schema); + $this->xmlSchema = $this->validateUrl($schema); } catch (ValidationFailedException $exception) { throw $exception; } @@ -133,7 +133,7 @@ class Format extends Entity public function __construct(string $prefix, string $namespace, string $schema) { try { - $this->prefix = $this->validateNoWhitespace($prefix); + $this->prefix = $this->validateRegEx($prefix, '/^[A-Za-z0-9\-_\.!~\*\'\(\)]+$/'); $this->setNamespace($namespace); $this->setSchema($schema); } catch (ValidationFailedException $exception) { diff --git a/src/Entity/Record.php b/src/Entity/Record.php index c49be22..fbd9509 100644 --- a/src/Entity/Record.php +++ b/src/Entity/Record.php @@ -245,7 +245,11 @@ class Record extends Entity public function __construct(string $identifier, Format $format, ?string $data = null, ?DateTime $lastChanged = null) { try { - $this->identifier = $this->validateNoWhitespace($identifier); + $this->identifier = $this->validateRegEx( + $identifier, + // xs:anyURI + '/^(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?\/{0,2}[0-9a-zA-Z;\/?:@&=+$\\.\\-_!~*\'()%]+)?(#[0-9a-zA-Z;\/?:@&=+$\\.\\-_!~*\'()%]+)?$/' + ); $this->setFormat($format); $this->setContent($data); $this->setLastChanged($lastChanged); diff --git a/src/Entity/Set.php b/src/Entity/Set.php index e2f7992..7ada955 100644 --- a/src/Entity/Set.php +++ b/src/Entity/Set.php @@ -163,6 +163,18 @@ class Set extends Entity } } + /** + * Set the name for this set. + * + * @param string $name The name + * + * @return void + */ + public function setName(string $name): void + { + $this->name = trim($name); + } + /** * Get new entity of set. * @@ -175,8 +187,8 @@ class Set extends Entity public function __construct(string $spec, string $name, string $description = '') { try { - $this->spec = $this->validateNoWhitespace($spec); - $this->name = trim($name); + $this->spec = $this->validateRegEx($spec, '/^([A-Za-z0-9\-_\.!~\*\'\(\)])+(:[A-Za-z0-9\-_\.!~\*\'\(\)]+)*$/'); + $this->setName($name); $this->setDescription($description); $this->records = new ArrayCollection(); } catch (ValidationFailedException $exception) {