Skip to content
This repository has been archived by the owner on Sep 19, 2022. It is now read-only.

Commit

Permalink
RoutesCollection refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-magosa committed Oct 18, 2014
1 parent 66dcae4 commit 539caec
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 50 deletions.
88 changes: 88 additions & 0 deletions src/KM/Saffron/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Copyright 2014 Krzysztof Magosa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace KM\Saffron;

use KM\Saffron\Exception\EmptyCollection;

abstract class Collection extends \ArrayIterator
{
/**
* Returns first element in Collection
* @return mixed
*/
public function first()
{
if (null === ($key = $this->getFirstKey())) {
throw new EmptyCollection('You cannot fetch first element of empty collection.');
}

return $this[$key];
}

/**
* Returns first key in Collection
* @return mixed
*/
protected function getFirstKey()
{
foreach ($this as $key => $value) {
return $key;
}

return null;
}

/**
* Creates nested Collection for each group.
* $func closure needs to return unique value for each group.
* @param \Closure $func
* @return Collection
*/
protected function groupBy(\Closure $func)
{
$class = get_called_class();

$result = new $class;
foreach ($this as $item) {
$key = $func($item);

if (!isset($result[$key])) {
$result[$key] = new $class;
}

$result[$key]->append($item);
}

return $result;
}

/**
* Checks whether Collection has something.
* Based on value returned by $func Closure.
* @param \Closure $func
* @return bool
*/
protected function has(\Closure $func)
{
foreach ($this as $item) {
if ($func($item)) {
return true;
}
}

return false;
}
}
20 changes: 20 additions & 0 deletions src/KM/Saffron/Exception/EmptyCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* Copyright 2014 Krzysztof Magosa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace KM\Saffron\Exception;

class EmptyCollection extends \LogicException
{
}
73 changes: 23 additions & 50 deletions src/KM/Saffron/RoutesCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@
namespace KM\Saffron;

use KM\Saffron\Exception\RouteAlreadyRegistered;
use KM\Saffron\Exception\EmptyCollection;
use KM\Saffron\Collection;

class RoutesCollection extends \ArrayIterator
class RoutesCollection extends Collection
{
/**
* Create instance of Route and returns it.
* Also looks for duplicated names.
*
* @param string $name Name of route
* @return RoutesCollection
*/
Expand All @@ -34,63 +39,23 @@ public function route($name)
return $route;
}

public function first()
{
return $this[$this->getFirstKey()];
}

protected function getFirstKey()
{
foreach ($this as $key => $value) {
return $key;
}

return null;
}

protected function groupBy(\Closure $func)
{
$index = 0;
$lastValue = $func($this->first());

$result = new self();

foreach ($this as $route) {
if ($lastValue != ($value = $func($route))) {
$lastValue = $value;
$index++;
}

if (!isset($result[$index])) {
$result[$index] = new self();
}

$result[$index]->append($route);
}

return $result;
}

/**
* Groups routes by domain
* @mixed RoutesCollection
*/
public function groupByDomain()
{
return $this->groupBy(
function ($route) {
return $route->getDomain();
return sha1($route->getDomain());
}
);
}

protected function has(\Closure $func)
{
foreach ($this as $route) {
if ($func($route)) {
return true;
}
}

return false;
}

/**
* Checks whether routes in collection has domain condition.
* @return bool
*/
public function hasDomain()
{
return $this->has(
Expand All @@ -100,6 +65,10 @@ function ($route) {
);
}

/**
* Checks whether routes in collection has method condition.
* @return bool
*/
public function hasMethod()
{
return $this->has(
Expand All @@ -109,6 +78,10 @@ function ($route) {
);
}

/**
* Checks whether routes in collection has https condition.
* @return bool
*/
public function hasHttps()
{
return $this->has(
Expand Down
52 changes: 52 additions & 0 deletions tests/RoutesCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Copyright 2014 Krzysztof Magosa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use KM\Saffron\RoutesCollection;

class RoutesCollectionTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException \KM\Saffron\Exception\RouteAlreadyRegistered
* @expectedExceptionMessage Route with name home is already registered
*/
public function testDuplicateOfNamedRoute()
{
$collection = new RoutesCollection();
$collection->route('home')
->setUri('/');

$collection->route('home')
->setUri('/home');
}

/**
* @expectedException \KM\Saffron\Exception\EmptyCollection
* @expectedExceptionMessage You cannot fetch first element of empty collection.
*/
public function testFirstOnEmptyCollection()
{
$collection = new RoutesCollection();
$collection->first();
}

public function testFirstOnFullCollection()
{
$collection = new RoutesCollection();
$route1 = $collection->route('home');
$route2 = $collection->route('team');

$this->assertEquals($route1, $collection->first());
}
}

0 comments on commit 539caec

Please sign in to comment.