Skip to content

Commit

Permalink
Move literal up
Browse files Browse the repository at this point in the history
  • Loading branch information
jm42 committed Feb 16, 2019
1 parent 5437493 commit aeb41cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 33 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
58 changes: 29 additions & 29 deletions library/assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit aeb41cb

Please sign in to comment.