Group hosts for multiple events

This commit is contained in:
Oliver Davies 2019-02-13 22:47:34 +00:00
parent ab9980c92d
commit 4e551b1347
2 changed files with 90 additions and 0 deletions

51
src/Service/Picker.php Normal file
View 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;
}
}

View 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);
}
}