Skip to content

Commit

Permalink
Add version validation rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixarntz committed Jun 18, 2024
1 parent 2586132 commit 830830b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Validation/Rules/Date_Validation_Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ public function sanitize( $value ) {
try {
$this->validate( $value );
} catch ( Validation_Exception $e ) {
// If a valid string is contained within the overall string, extract it.
if ( is_string( $value ) && preg_match( '/\d{4}-\d{2}-\d{2}/', $value, $matches ) ) {
return $matches[0];
}
return '';
}

Expand Down
11 changes: 11 additions & 0 deletions src/Validation/Rules/Datetime_Validation_Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ public function sanitize( $value ) {
try {
$this->validate( $value );
} catch ( Validation_Exception $e ) {
// If a valid string is contained within the overall string, extract it.
if (
is_string( $value )
&& preg_match(
'/\d{4}-\d{2}-\d{2}[Tt ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}(?::\d{2})?)?/',
$value,
$matches
)
) {
return $matches[0];
}
return '';
}

Expand Down
84 changes: 84 additions & 0 deletions src/Validation/Rules/Version_Validation_Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Class Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Rules\Version_Validation_Rule
*
* @since n.e.x.t
* @package wp-oop-plugin-lib
*/

namespace Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Rules;

use Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Contracts\Types;
use Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Contracts\Validation_Rule;
use Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Contracts\With_Type_Support;
use Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Exception\Validation_Exception;
use Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Traits\Type_Support;

/**
* Class for a validation rule that ensures values are valid version numbers.
*
* @since n.e.x.t
*/
class Version_Validation_Rule implements Validation_Rule, With_Type_Support {
use Type_Support;

/**
* Validates the given value.
*
* Validation will be strict and throw an exception for any unmet requirements.
*
* @since n.e.x.t
*
* @param mixed $value Value to validate.
*
* @throws Validation_Exception Thrown when validation fails.
*/
public function validate( $value ): void {
if ( ! preg_match( '/^(\d+\.\d+(?:\.\d+)?)(-[A-Za-z0-9.]+)?$/', (string) $value ) ) {
throw Validation_Exception::create(
'invalid_version',
sprintf(
/* translators: %s: value */
esc_html__( '%s is not a valid version number.', 'wp-oop-plugin-lib' ),
esc_html( (string) $value )
)
);
}
}

/**
* Sanitizes the given value.
*
* This should be called before storing the value in the persistency layer (e.g. the database).
* If the value does not satisfy validation requirements, it will be sanitized to a value that does, e.g. a default.
*
* @since n.e.x.t
*
* @param mixed $value Value to sanitize.
* @return mixed Sanitized value.
*/
public function sanitize( $value ) {
try {
$this->validate( $value );
} catch ( Validation_Exception $e ) {
// If a valid string is contained within the overall string, extract it.
if ( is_string( $value ) && preg_match( '/^(\d+\.\d+(?:\.\d+)?)(-[A-Za-z0-9.]+)?$/', $value, $matches ) ) {
return $matches[0];
}
return '';
}

return sanitize_url( $value );
}

/**
* Gets the supported types for the validation rule.
*
* @since n.e.x.t
*
* @return int One or more of the type constants from the Types interface, combined with a bitwise OR.
*/
protected function get_supported_types(): int {
return Types::TYPE_STRING;
}
}
12 changes: 12 additions & 0 deletions src/Validation/String_Validation_Rule_Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Rules\Regexp_Validation_Rule;
use Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Rules\String_Validation_Rule;
use Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Rules\URL_Validation_Rule;
use Felix_Arntz\WP_OOP_Plugin_Lib\Validation\Rules\Version_Validation_Rule;

/**
* Class for a string validation rule builder.
Expand Down Expand Up @@ -77,6 +78,17 @@ public function format_email(): static {
return $this->with_rule( new Email_Validation_Rule() );
}

/**
* Adds a version validation rule.
*
* @since n.e.x.t
*
* @return static Builder instance for chaining.
*/
public function format_version(): static {
return $this->with_rule( new Version_Validation_Rule() );
}

/**
* Adds a hex color validation rule.
*
Expand Down

0 comments on commit 830830b

Please sign in to comment.