Last active
December 26, 2015 10:49
-
-
Save mlebkowski/7139877 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = [ | |
["calendar_id" => 1, "doctor_id" => 1, "date" => "2013-01-01"], | |
["calendar_id" => 1, "doctor_id" => 2, "date" => "2013-01-03"], | |
["calendar_id" => 1, "doctor_id" => 1, "date" => "2013-01-03"], | |
["calendar_id" => 2, "doctor_id" => 4, "date" => "2013-01-01"], | |
["calendar_id" => 2, "doctor_id" => 4, "date" => "2013-01-01"], | |
["calendar_id" => 2, "doctor_id" => 5, "date" => "2013-01-01"], | |
["calendar_id" => 3, "doctor_id" => 1, "date" => "2013-01-01"], | |
["calendar_id" => 3, "doctor_id" => 1, "date" => "2013-01-02"], | |
]; | |
class ArrayGrouper | |
{ | |
public function groupByMany(array $items, array $definitions) | |
{ | |
$property = array_shift($definitions); | |
$items = $this->group($items, $property); | |
if (0 !== sizeof($definitions)) | |
{ | |
foreach ($items as &$item) | |
{ | |
$item = $this->groupByMany($item, $definitions); | |
} | |
} | |
return $items; | |
} | |
private function group(array $items, $property) | |
{ | |
$results = []; | |
foreach ($items as $item) | |
{ | |
$key = (new PropertyAccess($property))->resolve($item); | |
// $key = $item[$property]; | |
$results[$key][] = $item; | |
} | |
return $results; | |
} | |
} | |
$grouper = new ArrayGrouper; | |
print_r($grouper->groupByMany($data, ['calendar_id', 'doctor_id', 'date'])); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class VisitGroup extends ArrayObject { | |
protected $type; | |
public function getType() { | |
return $type; | |
} | |
public function setType($type) { | |
$this->type = $type; | |
} | |
public function isType($type) { | |
return $type === $this->type; | |
} | |
public function getRange() { | |
if ($this->count() === 0) { | |
return []; | |
} | |
if ($this->count() === 1) { | |
return [$this->offsetGet(0)['start']]; | |
} | |
return [$this->offsetGet(0)['start'], $this->offsetGet($this->count()-1)['start']]; | |
} | |
} | |
class GroupCreator { | |
public function createGroupList(array $visits) { | |
$groups = []; | |
$current = null; | |
foreach ($visits as $visit) { | |
if (null === $current) { | |
$current = new VisitGroup; | |
$current->setType($visit['type']); | |
} | |
if (false === $current->isType($visit['type'])) { | |
$groups[] = $current; | |
$current = new VisitGroup; | |
$current->setType($visit['type']); | |
} | |
$current->append($visit); | |
} | |
if (null !== $current) { | |
$groups[] = $current; | |
} | |
return $groups; | |
} | |
} | |
$visits = [ | |
["type" => "open", "start" => "8:00"], | |
["type" => "closed", "start" => "9:00"], | |
["type" => "closed", "start" => "10:00"], | |
["type" => "open", "start" => "11:00"], | |
["type" => "open", "start" => "12:00"], | |
["type" => "closed", "start" => "13:00"], | |
]; | |
$creator = new GroupCreator; | |
$groupList = $creator->createGroupList($visits); | |
foreach ($groupList as $group) { | |
if ($group->isType('open')) echo "Open group: " , $group->count(), "\n"; | |
else echo "Closed group: ", implode(" - ", $group->getRange()), "\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment