Group hosts for multiple events
This commit is contained in:
parent
ab9980c92d
commit
4e551b1347
51
src/Service/Picker.php
Normal file
51
src/Service/Picker.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class Picker
|
||||
{
|
||||
/**
|
||||
* The combined hosts for the retrieved events.
|
||||
*
|
||||
* @var \Tightenco\Collect\Support\Collection
|
||||
*/
|
||||
private $hosts;
|
||||
|
||||
/**
|
||||
* Picker constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->hosts = collect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the event hosts.
|
||||
*
|
||||
* @return \Tightenco\Collect\Support\Collection
|
||||
*/
|
||||
public function getHosts(): Collection
|
||||
{
|
||||
return $this->hosts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hosts for the retrieved events.
|
||||
*
|
||||
* @param \Tightenco\Collect\Support\Collection $data
|
||||
* The event data.
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setHosts(Collection $data): self
|
||||
{
|
||||
$this->hosts = $data->pluck('hosts.*.host_name')
|
||||
->flatten(1)
|
||||
->unique()
|
||||
->sort();
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
39
tests/Service/PickerTest.php
Normal file
39
tests/Service/PickerTest.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
|
||||
namespace App\Tests\Service;
|
||||
|
||||
use App\Service\Picker;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class PickerTest extends TestCase
|
||||
{
|
||||
/** @test */
|
||||
public function hosts_for_multiple_events_are_grouped_and_unique()
|
||||
{
|
||||
$data = [
|
||||
[
|
||||
'hosts' => [
|
||||
['host_name' => 'Lee Stone'],
|
||||
['host_name' => 'Dave Liddament'],
|
||||
['host_name' => 'Kat Zien'],
|
||||
],
|
||||
],
|
||||
[
|
||||
'hosts' => [
|
||||
['host_name' => 'Oliver Davies'],
|
||||
['host_name' => 'Lee Stone'],
|
||||
['host_name' => 'Lucia Velasco'],
|
||||
['host_name' => 'Dave Liddament'],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$picker = new Picker();
|
||||
$picker->setHosts(collect($data));
|
||||
|
||||
$hosts = $picker->getHosts();
|
||||
$this->assertInstanceOf(Collection::class, $hosts);
|
||||
$this->assertCount(5, $hosts);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue