Skip to content

Commit

Permalink
inline startsWith end endsWith using callables
Browse files Browse the repository at this point in the history
  • Loading branch information
mikey179 committed Dec 29, 2015
1 parent 2ffa7e8 commit 4c17708
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 143 deletions.
69 changes: 0 additions & 69 deletions src/main/php/predicate/StringEndsWith.php

This file was deleted.

70 changes: 0 additions & 70 deletions src/main/php/predicate/StringStartsWith.php

This file was deleted.

57 changes: 53 additions & 4 deletions src/main/php/predicate/predicates.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,37 @@ function isNonExistingDirectory($basePath = null)
*
* @api
* @param string $prefix
* @return \bovigo\assert\predicate\StringStartsWith
* @return \bovigo\assert\predicate\CallablePredicate
* @throws \InvalidArgumentException
* @since 1.1.0
*/
function startsWith($prefix)
{
return new StringStartsWith($prefix);
if (!is_string($prefix)) {
throw new \InvalidArgumentException(
'Prefix must be a string, "' . gettype($prefix) . '" given.'
);
}

return new CallablePredicate(
function($value) use ($prefix)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(
'Given value is not a string, but of type "'
. gettype($value) . '"'
);
}

return 0 === substr_compare(
$value,
$prefix,
0,
strlen($prefix)
);
},
'starts with \'' . $prefix . '\''
);
}

/**
Expand All @@ -447,12 +472,36 @@ function doesNotStartWith($prefix)
*
* @api
* @param string $suffix
* @return \bovigo\assert\predicate\StringEndsWith
* @return \bovigo\assert\predicate\CallablePredicate
* @throws \InvalidArgumentException
* @since 1.1.0
*/
function endsWith($suffix)
{
return new StringEndsWith($suffix);
if (!is_string($suffix)) {
throw new \InvalidArgumentException(
'Suffix must be a string, "' . gettype($suffix) . '" given.'
);
}

return new CallablePredicate(
function($value) use ($suffix)
{
if (!is_string($value)) {
throw new \InvalidArgumentException(
'Given value is not a string, but of type "'
. gettype($value) . '"'
);
}

return 0 === substr_compare(
$value,
$suffix,
-strlen($suffix)
);
},
'ends with \'' . $suffix . '\''
);
}

/**
Expand Down

0 comments on commit 4c17708

Please sign in to comment.