mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-05 22:15:56 +02:00
44 lines
799 B
PHP
44 lines
799 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 = [])
|
|
{
|
|
$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;
|
|
}
|
|
}
|