Skip to content

Commit

Permalink
Use regular expressions for validation
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer committed Jan 10, 2024
1 parent a0f4f05 commit e6a4d5d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
47 changes: 23 additions & 24 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,53 +35,52 @@
abstract class Entity
{
/**
* Check if a string does not contain any whitespaces.
* Check if a string is a valid URL.
*
* @param string $string The string
* @param string $url The URL
*
* @return string The validated string
* @return string The validated URL
*
* @throws ValidationFailedException
*/
protected function validateNoWhitespace(string $string): string
protected function validateUrl(string $url): string
{
$string = trim($string);
$url = trim($url);
$validator = Validation::createValidator();
$violations = $validator->validate(
$string,
[
new Assert\Regex([
'pattern' => '/\s/',
'match' => false,
'message' => 'This value contains whitespaces.'
]),
new Assert\NotBlank()
]
);
$violations = $validator->validate($url, new Assert\Url());
if ($violations->count() > 0) {
throw new ValidationFailedException(null, $violations);
}
return $string;
return $url;
}

/**
* Check if a string is a valid URI.
* Check if a string matches a given regular expression.
*
* @param string $uri The URI
* @param string $string The string
* @param string $regEx The regular expression
*
* @return string The validated URI
* @return string The validated string
*
* @throws ValidationFailedException
*/
protected function validateUri(string $uri): string
protected function validateRegEx(string $string, string $regEx): string
{
$uri = trim($uri);
$validator = Validation::createValidator();
$violations = $validator->validate($uri, new Assert\Url());
$violations = $validator->validate(
$string,
[
new Assert\Regex([
'pattern' => $regEx,
'message' => 'This value does not match the regular expression "{{ pattern }}".'
]),
new Assert\NotBlank()
]
);
if ($violations->count() > 0) {
throw new ValidationFailedException(null, $violations);
}
return $uri;
return $string;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Entity/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function getSchema(): string
public function setNamespace(string $namespace): void
{
try {
$this->namespace = $this->validateUri($namespace);
$this->namespace = $this->validateUrl($namespace);
} catch (ValidationFailedException $exception) {
throw $exception;
}
Expand All @@ -115,7 +115,7 @@ public function setNamespace(string $namespace): void
public function setSchema(string $schema): void
{
try {
$this->xmlSchema = $this->validateUri($schema);
$this->xmlSchema = $this->validateUrl($schema);
} catch (ValidationFailedException $exception) {
throw $exception;
}
Expand All @@ -133,7 +133,7 @@ public function setSchema(string $schema): void
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) {
Expand Down
6 changes: 5 additions & 1 deletion src/Entity/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ public function setLastChanged(?DateTime $dateTime = null): void
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);
Expand Down
16 changes: 14 additions & 2 deletions src/Entity/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ public function setDescription(string $description): void
}
}

/**
* 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.
*
Expand All @@ -175,8 +187,8 @@ public function setDescription(string $description): void
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) {
Expand Down

0 comments on commit e6a4d5d

Please sign in to comment.