From aeb41cbadca246baee5a6cd3c37e1ba46c39380e Mon Sep 17 00:00:00 2001 From: Juan Martinez Date: Sat, 16 Feb 2019 13:04:59 +0100 Subject: [PATCH] Move literal up --- README.md | 8 +++---- library/assert.php | 58 +++++++++++++++++++++++----------------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 58cb1d7..ad54dd8 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,10 @@ Validators All core _validators_ live in `\plan\assert` _namespace_. +### `literal` + +See [Literals](#literals). + ### `type` Will validate the type of data. The data type will be not casted. @@ -135,10 +139,6 @@ Wrapper around `is_scalar` function. Wrapper around `instanceof` type operator. -### `literal` - -See [Literals](#literals). - ### `iterable` Given data must be an array or implement `Iterable` interface. diff --git a/library/assert.php b/library/assert.php index 5e4bad7..b9821f2 100644 --- a/library/assert.php +++ b/library/assert.php @@ -21,6 +21,35 @@ function id() }; } +/** + * Compare `$data` with `$literal` using the identity operator. + * + * @param mixed $literal something to compare to + * + * @throws Invalid + * @return Closure + */ +function literal($literal) +{ + $type = assert\type(gettype($literal)); + + return function($data, $path = null) use($type, $literal) + { + $data = $type($data, $path); + + if ($data !== $literal) { + $ctx = [ + 'data' => util\repr($data), + 'literal' => $literal, + ]; + + throw new Invalid('{data} is not {literal}', $ctx, $path); + } + + return $data; + }; +} + /** * Check that the input data is of the given `$type`. The data type will not be * casted. @@ -128,35 +157,6 @@ function instance($class) }; } -/** - * Compare `$data` with `$literal` using the identity operator. - * - * @param mixed $literal something to compare to - * - * @throws Invalid - * @return Closure - */ -function literal($literal) -{ - $type = assert\type(gettype($literal)); - - return function($data, $path = null) use($type, $literal) - { - $data = $type($data, $path); - - if ($data !== $literal) { - $ctx = [ - 'data' => util\repr($data), - 'literal' => $literal, - ]; - - throw new Invalid('{data} is not {literal}', $ctx, $path); - } - - return $data; - }; -} - /** * Validates that `$data` is iterable. *