mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 17:52:25 +01:00
53 lines
932 B
PHP
53 lines
932 B
PHP
<?php
|
|
|
|
namespace DesignPatterns\Strategy;
|
|
|
|
/**
|
|
* Class ObjectCollection
|
|
*/
|
|
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;
|
|
}
|
|
}
|