PHP7 Strategy

This commit is contained in:
Dominik Liebler 2016-09-22 11:02:03 +02:00
parent 9b9eee5a4c
commit 36f3df8fbb
5 changed files with 47 additions and 53 deletions

View File

@ -8,7 +8,7 @@ interface ComparatorInterface
* @param mixed $a * @param mixed $a
* @param mixed $b * @param mixed $b
* *
* @return bool * @return int
*/ */
public function compare($a, $b); public function compare($a, $b): int;
} }

View File

@ -5,17 +5,16 @@ namespace DesignPatterns\Behavioral\Strategy;
class DateComparator implements ComparatorInterface class DateComparator implements ComparatorInterface
{ {
/** /**
* {@inheritdoc} * @param mixed $a
* @param mixed $b
*
* @return int
*/ */
public function compare($a, $b) public function compare($a, $b): int
{ {
$aDate = new \DateTime($a['date']); $aDate = new \DateTime($a['date']);
$bDate = new \DateTime($b['date']); $bDate = new \DateTime($b['date']);
if ($aDate == $bDate) { return $aDate <=> $bDate;
return 0;
}
return $aDate < $bDate ? -1 : 1;
} }
} }

View File

@ -5,14 +5,13 @@ namespace DesignPatterns\Behavioral\Strategy;
class IdComparator implements ComparatorInterface class IdComparator implements ComparatorInterface
{ {
/** /**
* {@inheritdoc} * @param mixed $a
* @param mixed $b
*
* @return int
*/ */
public function compare($a, $b) public function compare($a, $b): int
{ {
if ($a['id'] == $b['id']) { return $a['id'] <=> $b['id'];
return 0;
} else {
return $a['id'] < $b['id'] ? -1 : 1;
}
} }
} }

View File

@ -17,30 +17,24 @@ class ObjectCollection
/** /**
* @param array $elements * @param array $elements
*/ */
public function __construct(array $elements = array()) public function __construct(array $elements = [])
{ {
$this->elements = $elements; $this->elements = $elements;
} }
/** public function sort(): array
* @return array
*/
public function sort()
{ {
if (!$this->comparator) { if (!$this->comparator) {
throw new \LogicException('Comparator is not set'); throw new \LogicException('Comparator is not set');
} }
$callback = array($this->comparator, 'compare'); uasort($this->elements, [$this->comparator, 'compare']);
uasort($this->elements, $callback);
return $this->elements; return $this->elements;
} }
/** /**
* @param ComparatorInterface $comparator * @param ComparatorInterface $comparator
*
* @return void
*/ */
public function setComparator(ComparatorInterface $comparator) public function setComparator(ComparatorInterface $comparator)
{ {

View File

@ -5,43 +5,42 @@ namespace DesignPatterns\Behavioral\Strategy\Tests;
use DesignPatterns\Behavioral\Strategy\DateComparator; use DesignPatterns\Behavioral\Strategy\DateComparator;
use DesignPatterns\Behavioral\Strategy\IdComparator; use DesignPatterns\Behavioral\Strategy\IdComparator;
use DesignPatterns\Behavioral\Strategy\ObjectCollection; use DesignPatterns\Behavioral\Strategy\ObjectCollection;
use DesignPatterns\Behavioral\Strategy\Strategy;
/**
* Tests for Strategy pattern.
*/
class StrategyTest extends \PHPUnit_Framework_TestCase class StrategyTest extends \PHPUnit_Framework_TestCase
{ {
public function getIdCollection() public function provideIntegers()
{ {
return array( return [
array( [
array(array('id' => 2), array('id' => 1), array('id' => 3)), [['id' => 2], ['id' => 1], ['id' => 3]],
array('id' => 1), ['id' => 1],
), ],
array( [
array(array('id' => 3), array('id' => 2), array('id' => 1)), [['id' => 3], ['id' => 2], ['id' => 1]],
array('id' => 1), ['id' => 1],
), ],
); ];
} }
public function getDateCollection() public function providateDates()
{ {
return array( return [
array( [
array(array('date' => '2014-03-03'), array('date' => '2015-03-02'), array('date' => '2013-03-01')), [['date' => '2014-03-03'], ['date' => '2015-03-02'], ['date' => '2013-03-01']],
array('date' => '2013-03-01'), ['date' => '2013-03-01'],
), ],
array( [
array(array('date' => '2014-02-03'), array('date' => '2013-02-01'), array('date' => '2015-02-02')), [['date' => '2014-02-03'], ['date' => '2013-02-01'], ['date' => '2015-02-02']],
array('date' => '2013-02-01'), ['date' => '2013-02-01'],
), ],
); ];
} }
/** /**
* @dataProvider getIdCollection * @dataProvider provideIntegers
*
* @param array $collection
* @param array $expected
*/ */
public function testIdComparator($collection, $expected) public function testIdComparator($collection, $expected)
{ {
@ -54,7 +53,10 @@ class StrategyTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @dataProvider getDateCollection * @dataProvider providateDates
*
* @param array $collection
* @param array $expected
*/ */
public function testDateComparator($collection, $expected) public function testDateComparator($collection, $expected)
{ {