diff --git a/src/Assert/Assert.php b/src/Assert/Assert.php index adae7b4..1d62111 100644 --- a/src/Assert/Assert.php +++ b/src/Assert/Assert.php @@ -49,6 +49,7 @@ * @method static void validUnsignedInt(mixed $value, string $message = '', string $exception = '') * @method static void validUnsignedLong(mixed $value, string $message = '', string $exception = '') * @method static void validUnsignedShort(mixed $value, string $message = '', string $exception = '') + * @method static void validYear(mixed $value, string $message = '', string $exception = '') * @method static void validYearMonth(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidAnyURI(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidBase64Binary(mixed $value, string $message = '', string $exception = '') @@ -90,6 +91,7 @@ * @method static void nullOrValidUnsignedInt(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidUnsignedLong(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidUnsignedShort(mixed $value, string $message = '', string $exception = '') + * @method static void nullOrValidYear(mixed $value, string $message = '', string $exception = '') * @method static void nullOrValidYearMonth(mixed $value, string $message = '', string $exception = '') * @method static void allValidAnyURI(mixed $value, string $message = '', string $exception = '') * @method static void allValidBase64Binary(mixed $value, string $message = '', string $exception = '') @@ -131,6 +133,7 @@ * @method static void allValidUnsignedInt(mixed $value, string $message = '', string $exception = '') * @method static void allValidUnsignedLong(mixed $value, string $message = '', string $exception = '') * @method static void allValidUnsignedShort(mixed $value, string $message = '', string $exception = '') + * @method static void allValidYear(mixed $value, string $message = '', string $exception = '') * @method static void allValidYearMonth(mixed $value, string $message = '', string $exception = '') */ class Assert extends BaseAssert @@ -175,5 +178,6 @@ class Assert extends BaseAssert use UnsignedIntTrait; use UnsignedLongTrait; use UnsignedShortTrait; + use YearTrait; use YearMonthTrait; } diff --git a/src/Assert/YearTrait.php b/src/Assert/YearTrait.php new file mode 100644 index 0000000..aedacd9 --- /dev/null +++ b/src/Assert/YearTrait.php @@ -0,0 +1,31 @@ +sanitizeValue($value), SchemaViolationException::class); + } +} diff --git a/tests/Assert/YearTest.php b/tests/Assert/YearTest.php new file mode 100644 index 0000000..caf1deb --- /dev/null +++ b/tests/Assert/YearTest.php @@ -0,0 +1,53 @@ +assertTrue($shouldPass); + } catch (AssertionFailedException $e) { + $this->assertFalse($shouldPass); + } + } + + + /** + * @return array + */ + public static function provideYear(): array + { + return [ + 'empty' => [false, ''], + 'valid' => [true, '2001'], + 'whitespace' => [false, ' 2001 '], + 'valid numeric timezone' => [true, '2001+02:00'], + 'valid Zulu timezone' => [true, '2001Z'], + 'valid 00:00 timezone' => [true, '2001+00:00'], + '2001 BC' => [true, '-2001'], + '20000 BC' => [true, '-20000'], + ]; + } +} diff --git a/tests/Type/YearValueTest.php b/tests/Type/YearValueTest.php new file mode 100644 index 0000000..c853fa6 --- /dev/null +++ b/tests/Type/YearValueTest.php @@ -0,0 +1,54 @@ +assertTrue($shouldPass); + } catch (SchemaViolationException $e) { + $this->assertFalse($shouldPass); + } + } + + + /** + * @return array + */ + public static function provideYear(): array + { + return [ + 'empty' => [false, ''], + 'whitespace collapse' => [false, ' 2001 '], + 'valid' => [true, '2001'], + 'valid numeric timezone' => [true, '2001+02:00'], + 'valid Zulu timezone' => [true, '2001Z'], + 'whitespace collapse' => [true, ' 2001Z '], + 'valid 00:00 timezone' => [true, '2001+00:00'], + '2001 BC' => [true, '-2001'], + '20000 BC' => [true, '-20000'], + ]; + } +}