Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

Commit

Permalink
Added whereIn
Browse files Browse the repository at this point in the history
  • Loading branch information
msarca committed Mar 13, 2018
1 parent 69a26ba commit b551b4c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace Opis\Routing;

use Closure;
use Opis\Pattern\Builder;
use Serializable;
use Opis\Closure\SerializableClosure;

Expand Down Expand Up @@ -218,6 +219,32 @@ public function where(string $name, string $value): self
return $this->placeholder($name, $value);
}

/**
* @param string $name
* @param string[] $values
* @return Route
*/
public function whereIn(string $name, array $values): self
{
if (empty($values)) {
return $this;
}

// TODO: Modify this

if ($this->collection !== null) {
$delimiter = $this->collection->getRegexBuilder()->getOptions()[Builder::REGEX_DELIMITER];
} else {
$delimiter = '`';
}

$value = implode('|', array_map(function ($value) use ($delimiter) {
return preg_quote($value, $delimiter);
}, $values));

return $this->placeholder($name, $value);
}

/**
* Define a new implicit value
*
Expand Down Expand Up @@ -369,5 +396,4 @@ public function unserialize($data)
$this->properties = array_map($map, $object['properties']);
$this->collection = $object['collection'];
}

}
15 changes: 15 additions & 0 deletions tests/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,19 @@ public function testSerialization()
$this->assertEquals('BAR', $router->route(new Context('/foo/bar')));
}

public function testWhereIn()
{
$route = (new Route('/foo/{bar}', function ($bar) {
return $bar;
}))->whereIn('bar', ['a', 'b', 'car']);

$this->routes->addRoute($route);

$this->assertEquals(null, $this->router->route(new Context('/foo/bar')));
$this->assertEquals('a', $this->router->route(new Context('/foo/a')));
$this->assertEquals('b', $this->router->route(new Context('/foo/b')));
$this->assertEquals(null, $this->router->route(new Context('/foo/ab')));
$this->assertEquals('car', $this->router->route(new Context('/foo/car')));
}

}

0 comments on commit b551b4c

Please sign in to comment.