forked from graphp/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This relates to graphp#39. I decided to break BC and introduce a new entity to represent a Group. Design choices: - A group is unique in the entire graph. - An entity doesn't have to be in a group. - A group can use its Graph to find out which vertices are also in it. - A group has to be created before it can be used (same for vertices).
- Loading branch information
1 parent
a5455ed
commit 7cc72c8
Showing
8 changed files
with
271 additions
and
45 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Fhaculty\Graph; | ||
|
||
use Fhaculty\Graph\Attribute\AttributeAware; | ||
use Fhaculty\Graph\Attribute\AttributeBagReference; | ||
use Fhaculty\Graph\Exception\InvalidArgumentException; | ||
|
||
class Group implements AttributeAware | ||
{ | ||
/** | ||
* @var int|string | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var Graph | ||
*/ | ||
private $graph; | ||
|
||
private $attributes = array(); | ||
|
||
public function __construct(Graph $graph, $id) | ||
{ | ||
if (!is_int($id) && !is_string($id)) { | ||
throw new InvalidArgumentException('Group ID has to be of type integer or string'); | ||
} | ||
|
||
$this->id = $id; | ||
$this->graph = $graph; | ||
} | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getVerticesInGroup() | ||
{ | ||
$thisGroup = $this; | ||
|
||
return array_values(array_filter( | ||
$this->graph->getVertices()->getMap(), | ||
function (Vertex $vertex) use ($thisGroup) { | ||
return $vertex->getGroup() instanceof Group && $vertex->getGroup()->equals($thisGroup); | ||
} | ||
)); | ||
} | ||
|
||
public function getAttribute($name, $default = null) | ||
{ | ||
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default; | ||
} | ||
|
||
public function setAttribute($name, $value) | ||
{ | ||
$this->attributes[$name] = $value; | ||
} | ||
|
||
public function removeAttribute($name) | ||
{ | ||
unset($this->attributes[$name]); | ||
} | ||
|
||
public function getAttributeBag() | ||
{ | ||
return new AttributeBagReference($this->attributes); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return (string)$this->id; | ||
} | ||
|
||
public function equals(Group $other) | ||
{ | ||
return $this->id === $other->id; | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Fhaculty\Graph\Tests; | ||
|
||
use Fhaculty\Graph\Exception\InvalidArgumentException; | ||
use Fhaculty\Graph\Graph; | ||
use Fhaculty\Graph\Group; | ||
use Fhaculty\Graph\Tests\Attribute\AbstractAttributeAwareTest; | ||
|
||
final class GroupTest extends AbstractAttributeAwareTest | ||
{ | ||
public function testGroupIdCanBeAnInteger() | ||
{ | ||
$group = new Group(new Graph(), 1); | ||
|
||
$this->assertSame(1, $group->getId()); | ||
} | ||
|
||
public function testGroupIdCanBeAString() | ||
{ | ||
$group = new Group(new Graph(), 'string'); | ||
|
||
$this->assertSame('string', $group->getId()); | ||
} | ||
|
||
/** | ||
* @expectedException InvalidArgumentException | ||
*/ | ||
public function testGroupIdCanNotBeSomethingElse() | ||
{ | ||
new Group(new Graph(), null); | ||
} | ||
|
||
public function testGroupEquals() | ||
{ | ||
$group = new Group(new Graph(), 1); | ||
|
||
$this->assertTrue($group->equals(new Group(new Graph(), 1))); | ||
} | ||
|
||
public function testGroupIsNotEqual() | ||
{ | ||
$group = new Group(new Graph(), 1); | ||
|
||
$this->assertFalse($group->equals(new Group(new Graph(), 2))); | ||
} | ||
|
||
public function testFindAllVerticesInGroup() | ||
{ | ||
$graph = new Graph(); | ||
$group1 = $graph->createGroup(1); | ||
$vertex1 = $graph->createVertex(1)->setGroup($group1); | ||
$vertex2 = $graph->createVertex(2)->setGroup($group1); | ||
$group2 = $graph->createGroup(2); | ||
$vertex3 = $graph->createVertex(3)->setGroup($group2); | ||
|
||
$this->assertEquals( | ||
array( | ||
$vertex1, | ||
$vertex2 | ||
), | ||
$group1->getVerticesInGroup() | ||
); | ||
|
||
$this->assertEquals( | ||
array( | ||
$vertex3 | ||
), | ||
$group2->getVerticesInGroup() | ||
); | ||
} | ||
|
||
protected function createAttributeAware() | ||
{ | ||
return new Group(new Graph(), 1); | ||
} | ||
} |
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
Oops, something went wrong.