From 4c17708ff8cc2a566ca0777d78b394deddfbf70d Mon Sep 17 00:00:00 2001 From: mikey179 Date: Tue, 29 Dec 2015 11:45:16 +0100 Subject: [PATCH] inline startsWith end endsWith using callables --- src/main/php/predicate/StringEndsWith.php | 69 -------------------- src/main/php/predicate/StringStartsWith.php | 70 --------------------- src/main/php/predicate/predicates.php | 57 +++++++++++++++-- 3 files changed, 53 insertions(+), 143 deletions(-) delete mode 100644 src/main/php/predicate/StringEndsWith.php delete mode 100644 src/main/php/predicate/StringStartsWith.php diff --git a/src/main/php/predicate/StringEndsWith.php b/src/main/php/predicate/StringEndsWith.php deleted file mode 100644 index c7e9f7f..0000000 --- a/src/main/php/predicate/StringEndsWith.php +++ /dev/null @@ -1,69 +0,0 @@ -suffix = $suffix; - } - - /** - * evaluates predicate against given value - * - * @param mixed $value - * @return bool - */ - public function test($value) - { - if (!is_string($value)) { - throw new \InvalidArgumentException( - 'Given value is not a string, but of type "' - . gettype($value) . '"' - ); - } - - return 0 === substr_compare( - $value, - $this->suffix, - -strlen($this->suffix) - ); - } - - /** - * returns string representation of predicate - * - * @return string - */ - public function __toString() - { - return 'ends with \'' . $this->suffix . '\''; - } -} diff --git a/src/main/php/predicate/StringStartsWith.php b/src/main/php/predicate/StringStartsWith.php deleted file mode 100644 index 57f3cac..0000000 --- a/src/main/php/predicate/StringStartsWith.php +++ /dev/null @@ -1,70 +0,0 @@ -prefix = $prefix; - } - - /** - * evaluates predicate against given value - * - * @param mixed $value - * @return bool - */ - public function test($value) - { - if (!is_string($value)) { - throw new \InvalidArgumentException( - 'Given value is not a string, but of type "' - . gettype($value) . '"' - ); - } - - return 0 === substr_compare( - $value, - $this->prefix, - 0, - strlen($this->prefix) - ); - } - - /** - * returns string representation of predicate - * - * @return string - */ - public function __toString() - { - return 'starts with \'' . $this->prefix . '\''; - } -} diff --git a/src/main/php/predicate/predicates.php b/src/main/php/predicate/predicates.php index 37a28e1..3d4280b 100644 --- a/src/main/php/predicate/predicates.php +++ b/src/main/php/predicate/predicates.php @@ -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 . '\'' + ); } /** @@ -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 . '\'' + ); } /**