mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 09:42:24 +01:00
PHP7 Strategy
This commit is contained in:
parent
9b9eee5a4c
commit
36f3df8fbb
@ -8,7 +8,7 @@ interface ComparatorInterface
|
||||
* @param mixed $a
|
||||
* @param mixed $b
|
||||
*
|
||||
* @return bool
|
||||
* @return int
|
||||
*/
|
||||
public function compare($a, $b);
|
||||
public function compare($a, $b): int;
|
||||
}
|
||||
|
@ -5,17 +5,16 @@ namespace DesignPatterns\Behavioral\Strategy;
|
||||
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']);
|
||||
$bDate = new \DateTime($b['date']);
|
||||
|
||||
if ($aDate == $bDate) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $aDate < $bDate ? -1 : 1;
|
||||
return $aDate <=> $bDate;
|
||||
}
|
||||
}
|
||||
|
@ -5,14 +5,13 @@ namespace DesignPatterns\Behavioral\Strategy;
|
||||
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 0;
|
||||
} else {
|
||||
return $a['id'] < $b['id'] ? -1 : 1;
|
||||
}
|
||||
return $a['id'] <=> $b['id'];
|
||||
}
|
||||
}
|
||||
|
@ -17,30 +17,24 @@ class ObjectCollection
|
||||
/**
|
||||
* @param array $elements
|
||||
*/
|
||||
public function __construct(array $elements = array())
|
||||
public function __construct(array $elements = [])
|
||||
{
|
||||
$this->elements = $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function sort()
|
||||
public function sort(): array
|
||||
{
|
||||
if (!$this->comparator) {
|
||||
throw new \LogicException('Comparator is not set');
|
||||
}
|
||||
|
||||
$callback = array($this->comparator, 'compare');
|
||||
uasort($this->elements, $callback);
|
||||
uasort($this->elements, [$this->comparator, 'compare']);
|
||||
|
||||
return $this->elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ComparatorInterface $comparator
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setComparator(ComparatorInterface $comparator)
|
||||
{
|
||||
|
@ -5,43 +5,42 @@ namespace DesignPatterns\Behavioral\Strategy\Tests;
|
||||
use DesignPatterns\Behavioral\Strategy\DateComparator;
|
||||
use DesignPatterns\Behavioral\Strategy\IdComparator;
|
||||
use DesignPatterns\Behavioral\Strategy\ObjectCollection;
|
||||
use DesignPatterns\Behavioral\Strategy\Strategy;
|
||||
|
||||
/**
|
||||
* Tests for Strategy pattern.
|
||||
*/
|
||||
class StrategyTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function getIdCollection()
|
||||
public function provideIntegers()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(array('id' => 2), array('id' => 1), array('id' => 3)),
|
||||
array('id' => 1),
|
||||
),
|
||||
array(
|
||||
array(array('id' => 3), array('id' => 2), array('id' => 1)),
|
||||
array('id' => 1),
|
||||
),
|
||||
);
|
||||
return [
|
||||
[
|
||||
[['id' => 2], ['id' => 1], ['id' => 3]],
|
||||
['id' => 1],
|
||||
],
|
||||
[
|
||||
[['id' => 3], ['id' => 2], ['id' => 1]],
|
||||
['id' => 1],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function getDateCollection()
|
||||
public function providateDates()
|
||||
{
|
||||
return array(
|
||||
array(
|
||||
array(array('date' => '2014-03-03'), array('date' => '2015-03-02'), array('date' => '2013-03-01')),
|
||||
array('date' => '2013-03-01'),
|
||||
),
|
||||
array(
|
||||
array(array('date' => '2014-02-03'), array('date' => '2013-02-01'), array('date' => '2015-02-02')),
|
||||
array('date' => '2013-02-01'),
|
||||
),
|
||||
);
|
||||
return [
|
||||
[
|
||||
[['date' => '2014-03-03'], ['date' => '2015-03-02'], ['date' => '2013-03-01']],
|
||||
['date' => '2013-03-01'],
|
||||
],
|
||||
[
|
||||
[['date' => '2014-02-03'], ['date' => '2013-02-01'], ['date' => '2015-02-02']],
|
||||
['date' => '2013-02-01'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getIdCollection
|
||||
* @dataProvider provideIntegers
|
||||
*
|
||||
* @param array $collection
|
||||
* @param array $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)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user