Skip to content

Commit

Permalink
Support for disparate graphs and the new belongsOrEquals method for C…
Browse files Browse the repository at this point in the history
…ontextInterface classes
  • Loading branch information
Emre Sokullu committed May 2, 2017
1 parent 75e309d commit 671d819
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Pho/Framework/ContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@
*/
interface ContextInterface extends \Pho\Lib\Graph\GraphInterface {

/**
* Checks if the given context is equal to or a subelement of this context.
*
* @param ContextInterface $context
* @return bool
*/
public function belongsOrEquals(ContextInterface $context): bool;

}
23 changes: 23 additions & 0 deletions src/Pho/Framework/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,27 @@ public function __construct(Actor $creator, ContextInterface $context) {
$this->setupEdges();
}

/**
* {@inheritdoc}
*/
public function belongsOrEquals(ContextInterface $context): bool
{
/*if($context instanceof Graph)
return true;*/
$members = $context->members();
foreach($members as $member) {
if($member instanceof Frame) {
if($member->id() == $this->id()) {
return true;
}
else {
if($this->belongsOrEquals($member)) {
return true;
}
}
}
}
return false;
}

}
41 changes: 41 additions & 0 deletions src/Pho/Framework/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,45 @@
*/
class Graph extends \Pho\Lib\Graph\Graph implements ContextInterface {

/**
* The title of the graph.
*
* @var string
*/
protected $title;

/**
* Constructor.
*
* The title allows one to host multiple graphs in a single PHP instance.
*
* @param string $title Optional. Leave blank for a random title.
*/
public function __construct(string $title = "") {
if(empty($title)) {
$this->title = uniqid("graph_", true);
}
else {
$this->title = $title;
}
}

/**
* Returns the title of the graph.
*
* @return string
*/
public function title(): string
{
return $this->title;
}

/**
* {@inheritdoc}
*/
public function belongsOrEquals(ContextInterface $context): bool
{
return $context instanceof Graph && $this->title == $context->title();
}

}
57 changes: 57 additions & 0 deletions tests/Pho/Framework/RecursiveContextsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Pho package.
*
* (c) Emre Sokullu <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pho\Framework;

use Pho\Lib\Graph\Predicate;

class RecursiveContextsTest extends \PHPUnit\Framework\TestCase
{
private $graph;

public function setUp() {
$this->graph = new Graph();
}

public function tearDown() {
unset($this->graph);
}

public function testGraph() {
$this->assertTrue($this->graph->belongsOrEquals($this->graph));
}

public function testFrame() {
$actor = new Actor($this->graph);
$frame = new Frame($actor, $this->graph);
$this->assertTrue($frame->belongsOrEquals($this->graph));
}

public function testSubFrame() {
$actor = new Actor($this->graph);
$frame = new Frame($actor, $this->graph);
$subframe = new Frame($actor, $frame);
$this->assertTrue($subframe->belongsOrEquals($this->graph));
$this->assertTrue($subframe->belongsOrEquals($frame));
}

public function testDisparateGraphs() {
$new_graph = new Graph();
$actor = new Actor($this->graph);
$frame = new Frame($actor, $this->graph);
$subframe = new Frame($actor, $frame);
$this->assertFalse($this->graph->belongsOrEquals($new_graph));
$this->assertFalse($frame->belongsOrEquals($new_graph));
$this->assertFalse($subframe->belongsOrEquals($new_graph));
$this->assertTrue($subframe->belongsOrEquals($this->graph));
}

}

0 comments on commit 671d819

Please sign in to comment.