Skip to content
Permalink
Browse files

Merge pull request #33 from kfreiman/master

Added LikeValue::starts() and LikeValue::ends()
  • Loading branch information...
shadowhand committed Sep 30, 2017
2 parents 40a3515 + 60814ff commit e6533f81cdd02f126c2f9ca1181198e1ee981d4b
Showing with 38 additions and 4 deletions.
  1. +4 −4 README.md
  2. +18 −0 src/LikeValue.php
  3. +16 −0 tests/LikeValueTest.php
@@ -349,12 +349,12 @@ print_r($statement->params());
// ["\%\%hijack"];
```

The `LikeValue` helper also supports adding wildcards before and after the
value automatically:
The `LikeValue` helper also supports adding wildcards value automatically:

```php
echo like::any('John');
// "%John%"
echo like::any('John'); // "%John%"
echo like::starts('Joh'); // "Joh%"
echo like::ends('ohn'); // "%ohn"
```

There is also a MSSQL extension that will escape character ranges:
@@ -27,4 +27,22 @@ public static function any(string $value): string
$value = static::escape($value);
return "%$value%";
}
/**
* Escape input for a LIKE condition, ends with wildcards.
*/
public static function starts(string $value): string
{
$value = static::escape($value);
return "$value%";
}
/**
* Escape input for a LIKE condition, starts with wildcards.
*/
public static function ends(string $value): string
{
$value = static::escape($value);
return "%$value";
}
}
@@ -22,4 +22,20 @@ public function testLikeAny()
$this->assertSame($expected, LikeValue::any($input));
}
public function testLikeStarts()
{
$input = 'a % string';
$expected = 'a \\% string%';
$this->assertSame($expected, LikeValue::starts($input));
}
public function testLikeEnds()
{
$input = 'a % string';
$expected = '%a \\% string';
$this->assertSame($expected, LikeValue::ends($input));
}
}

0 comments on commit e6533f8

Please sign in to comment.
You can’t perform that action at this time.