mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
start a restructure
This commit is contained in:
52
Behavioral/Strategy/ObjectCollection.php
Normal file
52
Behavioral/Strategy/ObjectCollection.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user