Skip to content

Commit

Permalink
Merge pull request #33 from kfreiman/master
Browse files Browse the repository at this point in the history
Added LikeValue::starts() and LikeValue::ends()
  • Loading branch information
shadowhand committed Sep 30, 2017
2 parents 40a3515 + 60814ff commit e6533f8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions src/LikeValue.php
Expand Up @@ -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";
}
}
16 changes: 16 additions & 0 deletions tests/LikeValueTest.php
Expand Up @@ -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.