From 830830b13c5dcc9f66047b91a5264c21bff1aec0 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 18 Jun 2024 14:13:47 -0700 Subject: [PATCH] Add version validation rule. --- src/Validation/Rules/Date_Validation_Rule.php | 4 + .../Rules/Datetime_Validation_Rule.php | 11 +++ .../Rules/Version_Validation_Rule.php | 84 +++++++++++++++++++ .../String_Validation_Rule_Builder.php | 12 +++ 4 files changed, 111 insertions(+) create mode 100644 src/Validation/Rules/Version_Validation_Rule.php diff --git a/src/Validation/Rules/Date_Validation_Rule.php b/src/Validation/Rules/Date_Validation_Rule.php index 3e35a77..13c7de2 100644 --- a/src/Validation/Rules/Date_Validation_Rule.php +++ b/src/Validation/Rules/Date_Validation_Rule.php @@ -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 ''; } diff --git a/src/Validation/Rules/Datetime_Validation_Rule.php b/src/Validation/Rules/Datetime_Validation_Rule.php index 9ed6aa0..70ace86 100644 --- a/src/Validation/Rules/Datetime_Validation_Rule.php +++ b/src/Validation/Rules/Datetime_Validation_Rule.php @@ -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 ''; } diff --git a/src/Validation/Rules/Version_Validation_Rule.php b/src/Validation/Rules/Version_Validation_Rule.php new file mode 100644 index 0000000..60a3b3e --- /dev/null +++ b/src/Validation/Rules/Version_Validation_Rule.php @@ -0,0 +1,84 @@ +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; + } +} diff --git a/src/Validation/String_Validation_Rule_Builder.php b/src/Validation/String_Validation_Rule_Builder.php index 2b2372a..13b2139 100644 --- a/src/Validation/String_Validation_Rule_Builder.php +++ b/src/Validation/String_Validation_Rule_Builder.php @@ -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. @@ -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. *