mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-06-25 18:53:25 +02:00
EduardoGR adding strategy context in order to complete the pattern
This commit is contained in:
23
Behavioral/Strategy/Context.php
Normal file
23
Behavioral/Strategy/Context.php
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\Strategy;
|
||||||
|
|
||||||
|
class Context
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var ComparatorInterface
|
||||||
|
*/
|
||||||
|
private $comparator;
|
||||||
|
|
||||||
|
public function __construct(ComparatorInterface $comparator)
|
||||||
|
{
|
||||||
|
$this->comparator = $comparator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function executeStrategy(array $elements) : array
|
||||||
|
{
|
||||||
|
uasort($elements, [$this->comparator, 'compare']);
|
||||||
|
|
||||||
|
return $elements;
|
||||||
|
}
|
||||||
|
}
|
@ -1,43 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Strategy;
|
|
||||||
|
|
||||||
class ObjectCollection
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private $elements;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ComparatorInterface
|
|
||||||
*/
|
|
||||||
private $comparator;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $elements
|
|
||||||
*/
|
|
||||||
public function __construct(array $elements = [])
|
|
||||||
{
|
|
||||||
$this->elements = $elements;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function sort(): array
|
|
||||||
{
|
|
||||||
if (!$this->comparator) {
|
|
||||||
throw new \LogicException('Comparator is not set');
|
|
||||||
}
|
|
||||||
|
|
||||||
uasort($this->elements, [$this->comparator, 'compare']);
|
|
||||||
|
|
||||||
return $this->elements;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ComparatorInterface $comparator
|
|
||||||
*/
|
|
||||||
public function setComparator(ComparatorInterface $comparator)
|
|
||||||
{
|
|
||||||
$this->comparator = $comparator;
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Strategy\Tests;
|
namespace DesignPatterns\Behavioral\Strategy\Tests;
|
||||||
|
|
||||||
|
use DesignPatterns\Behavioral\Strategy\Context;
|
||||||
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 PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
class StrategyTest extends TestCase
|
class StrategyTest extends TestCase
|
||||||
@ -45,9 +45,8 @@ class StrategyTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testIdComparator($collection, $expected)
|
public function testIdComparator($collection, $expected)
|
||||||
{
|
{
|
||||||
$obj = new ObjectCollection($collection);
|
$obj = new Context(new IdComparator());
|
||||||
$obj->setComparator(new IdComparator());
|
$elements = $obj->executeStrategy($collection);
|
||||||
$elements = $obj->sort();
|
|
||||||
|
|
||||||
$firstElement = array_shift($elements);
|
$firstElement = array_shift($elements);
|
||||||
$this->assertEquals($expected, $firstElement);
|
$this->assertEquals($expected, $firstElement);
|
||||||
@ -61,9 +60,8 @@ class StrategyTest extends TestCase
|
|||||||
*/
|
*/
|
||||||
public function testDateComparator($collection, $expected)
|
public function testDateComparator($collection, $expected)
|
||||||
{
|
{
|
||||||
$obj = new ObjectCollection($collection);
|
$obj = new Context(new DateComparator());
|
||||||
$obj->setComparator(new DateComparator());
|
$elements = $obj->executeStrategy($collection);
|
||||||
$elements = $obj->sort();
|
|
||||||
|
|
||||||
$firstElement = array_shift($elements);
|
$firstElement = array_shift($elements);
|
||||||
$this->assertEquals($expected, $firstElement);
|
$this->assertEquals($expected, $firstElement);
|
||||||
|
Reference in New Issue
Block a user