mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-06-10 16:04:57 +02:00
50 lines
897 B
PHP
50 lines
897 B
PHP
<?php
|
|
|
|
namespace DesignPatterns\Behavioral\Strategy;
|
|
|
|
class ObjectCollection
|
|
{
|
|
/**
|
|
* @var array
|
|
*/
|
|
private $elements;
|
|
|
|
/**
|
|
* @var ComparatorInterface
|
|
*/
|
|
private $comparator;
|
|
|
|
/**
|
|
* @param array $elements
|
|
*/
|
|
public function __construct(array $elements = array())
|
|
{
|
|
$this->elements = $elements;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function sort()
|
|
{
|
|
if (!$this->comparator) {
|
|
throw new \LogicException('Comparator is not set');
|
|
}
|
|
|
|
$callback = array($this->comparator, 'compare');
|
|
uasort($this->elements, $callback);
|
|
|
|
return $this->elements;
|
|
}
|
|
|
|
/**
|
|
* @param ComparatorInterface $comparator
|
|
*
|
|
* @return void
|
|
*/
|
|
public function setComparator(ComparatorInterface $comparator)
|
|
{
|
|
$this->comparator = $comparator;
|
|
}
|
|
}
|