From 671d819138b391a59a2c969495ffcfcefed98ec3 Mon Sep 17 00:00:00 2001 From: Emre Sokullu Date: Tue, 2 May 2017 14:13:08 +0300 Subject: [PATCH] Support for disparate graphs and the new belongsOrEquals method for ContextInterface classes --- src/Pho/Framework/ContextInterface.php | 8 +++ src/Pho/Framework/Frame.php | 23 ++++++++ src/Pho/Framework/Graph.php | 41 +++++++++++++ tests/Pho/Framework/RecursiveContextsTest.php | 57 +++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 tests/Pho/Framework/RecursiveContextsTest.php diff --git a/src/Pho/Framework/ContextInterface.php b/src/Pho/Framework/ContextInterface.php index 4a98aa0..167fa33 100644 --- a/src/Pho/Framework/ContextInterface.php +++ b/src/Pho/Framework/ContextInterface.php @@ -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; + } \ No newline at end of file diff --git a/src/Pho/Framework/Frame.php b/src/Pho/Framework/Frame.php index d43f1f0..f0d0766 100644 --- a/src/Pho/Framework/Frame.php +++ b/src/Pho/Framework/Frame.php @@ -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; + } + } \ No newline at end of file diff --git a/src/Pho/Framework/Graph.php b/src/Pho/Framework/Graph.php index d382b9a..0df8e5f 100644 --- a/src/Pho/Framework/Graph.php +++ b/src/Pho/Framework/Graph.php @@ -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(); + } + } \ No newline at end of file diff --git a/tests/Pho/Framework/RecursiveContextsTest.php b/tests/Pho/Framework/RecursiveContextsTest.php new file mode 100644 index 0000000..6df2d5e --- /dev/null +++ b/tests/Pho/Framework/RecursiveContextsTest.php @@ -0,0 +1,57 @@ + + * + * 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)); + } + +} \ No newline at end of file