diff --git a/src/Pho/Framework/Acl.php b/src/Pho/Framework/Acl.php new file mode 100644 index 0000000..ea7ce87 --- /dev/null +++ b/src/Pho/Framework/Acl.php @@ -0,0 +1,26 @@ +creator = $creator; + $this->context = $context; + } + + public function toArray(): array + { + //eval(\Psy\sh()); + return [ + "creator" => (string) $this->creator->id(), + "context" => ($this->context instanceof Graph\Graph) ? Graph\Graph::class : (string) $this->context->id() + ]; + } + +} \ No newline at end of file diff --git a/src/Pho/Framework/Actor.php b/src/Pho/Framework/Actor.php index 5cbb959..e8f60e7 100644 --- a/src/Pho/Framework/Actor.php +++ b/src/Pho/Framework/Actor.php @@ -11,6 +11,8 @@ namespace Pho\Framework; +use Pho\Lib\Graph; + /** * The Actor Particle * @@ -23,7 +25,7 @@ * * @author Emre Sokullu */ -class Actor extends \Pho\Lib\Graph\Node implements ParticleInterface { +class Actor extends Graph\Node implements ParticleInterface { use ParticleTrait; @@ -36,4 +38,10 @@ class Actor extends \Pho\Lib\Graph\Node implements ParticleInterface { */ const EDGES_IN = [ActorOut\Reads::class, ActorOut\Subscribes::class, ObjectOut\Transmits::class]; + public function __construct(Graph\GraphInterface $context) { + parent::__construct($context); + $this->acl = new Acl($this, $context); + $this->setupEdges(); + } + } \ No newline at end of file diff --git a/src/Pho/Framework/Frame.php b/src/Pho/Framework/Frame.php index ba0c489..df395dc 100644 --- a/src/Pho/Framework/Frame.php +++ b/src/Pho/Framework/Frame.php @@ -11,7 +11,7 @@ namespace Pho\Framework; -use Pho\Lib\Graph\SubGraph; +use Pho\Lib\Graph; /** * The Frame Particle @@ -31,7 +31,7 @@ * * @author Emre Sokullu */ -class Frame extends \Pho\Lib\Graph\SubGraph implements ParticleInterface { +class Frame extends Graph\SubGraph implements ParticleInterface { use ParticleTrait; @@ -44,4 +44,10 @@ class Frame extends \Pho\Lib\Graph\SubGraph implements ParticleInterface { */ const EDGES_IN = [ActorOut\Reads::class, ActorOut\Subscribes::class, ActorOut\Writes::class, ObjectOut\Transmits::class]; + public function __construct(Actor $creator, Graph\GraphInterface $context) { + parent::__construct($context); + $this->acl = new Acl($creator, $context); + $this->setupEdges(); + } + } \ No newline at end of file diff --git a/src/Pho/Framework/Object.php b/src/Pho/Framework/Object.php index 8be289e..bb05bf7 100644 --- a/src/Pho/Framework/Object.php +++ b/src/Pho/Framework/Object.php @@ -11,6 +11,8 @@ namespace Pho\Framework; +use Pho\Lib\Graph; + /** * The Object Particle * @@ -34,4 +36,10 @@ class Object extends \Pho\Lib\Graph\Node implements ParticleInterface { */ const EDGES_IN = [ActorOut\Reads::class, ActorOut\Subscribes::class, ActorOut\Writes::class, ObjectOut\Transmits::class]; + public function __construct(Actor $creator, Graph\GraphInterface $context) { + parent::__construct($context); + $this->acl = new Acl($creator, $context); + $this->setupEdges(); + } + } \ No newline at end of file diff --git a/src/Pho/Framework/ParticleTrait.php b/src/Pho/Framework/ParticleTrait.php index b56dc26..e040897 100644 --- a/src/Pho/Framework/ParticleTrait.php +++ b/src/Pho/Framework/ParticleTrait.php @@ -11,7 +11,6 @@ namespace Pho\Framework; -use Pho\Lib\Graph; use Pho\Framework\Exceptions\InvalidEdgeHeadTypeException; use Zend\File\ClassFileLocator; @@ -112,12 +111,16 @@ trait ParticleTrait { protected $edge_out_getter_classes = []; /** - * Constructor. - * - * @param Pho\Lib\Graph\GraphInterface $graph The graph that this particle belongs to. + * Access Control List object + * + * @var Acl + */ + protected $acl; + + /** + * Trait constructor. */ - public function __construct(Graph\GraphInterface $graph) { - parent::__construct($graph); + protected function setupEdges() { $this->_setupEdgesIn(); $this->_setupEdgesOut(); } @@ -260,4 +263,11 @@ protected function _callGetter(string $name, array $args): array } + public function toArray(): array + { + $array = parent::toArray(); + $array["acl"] = $this->acl->toArray(); + return $array; + } + } \ No newline at end of file diff --git a/tests/Pho/Framework/SimpleTest.php b/tests/Pho/Framework/SimpleTest.php new file mode 100644 index 0000000..e65a914 --- /dev/null +++ b/tests/Pho/Framework/SimpleTest.php @@ -0,0 +1,131 @@ + + * + * 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; + +class SimpleTest extends \PHPUnit\Framework\TestCase +{ + private $graph; + + public function setUp() { + $this->graph = new Graph\Graph(); + } + + public function tearDown() { + unset($this->graph); + } + + public function testActor() { + $node = new Actor($this->graph); + $node_expected_to_be_identical = $this->graph->get($node->id()); + $this->assertEquals($node->id(), $node_expected_to_be_identical->id()); + } + + public function testActorEdge() { + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); + $edge = $actor->writes($object); + $this->assertInstanceOf(ActorOut\Writes::class, $edge); + $this->assertInstanceOf(Graph\Predicate::class, $edge->predicate()); + } + + public function testActorPredicate() { + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); + $edge = $actor->subscribes($object); + $this->assertInstanceOf(ActorOut\SubscribesPredicate::class, $edge->predicate()); + } + + public function testObjectGetter() { + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); + $edge = $actor->writes($object); + $this->assertInstanceOf(ActorOut\Writes::class, $object->getWriters()[0]); + $this->assertCount(1, $object->getWriters()); + $this->assertCount(1, $actor->getWrites()); + $this->assertInstanceOf(ActorOut\Writes::class, $actor->getWrites()[0]); + } + + public function testFiltering() { + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); + $edge = $actor->writes($object); + $edge = $actor->reads($object); + $this->assertCount(1, $actor->getWrites()); + } + + /** + * Since write extends subscribes + */ + public function testEdgeInheritance() { + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); + $edge = $actor->writes($object); + $this->assertCount(1, $actor->getSubscriptions()); + $this->assertCount(0, $actor->getReads()); + } + + /** + * @expectedException Pho\Framework\Exceptions\InvalidEdgeHeadTypeException + */ + public function testImpossibleEdge() { + $actor1 = new Actor($this->graph); + $actor2 = new Actor($this->graph); + $edge = $actor1->writes($actor2); + } + + public function testEdgeInvoke() { + $actor = new Actor($this->graph); + $object = new Object($actor, $this->graph); + $edge = $actor->writes($object); + $this->assertInstanceOf(Object::class, $edge()); + $this->assertEquals($object->id(), $edge()->id()); + } + + + public function testActorToArray() { + $actor = new Actor($this->graph); + $array = $actor->toArray(); + $faker = \Faker\Factory::create(); + $this->assertArrayHasKey("id", $array); + $this->assertArrayHasKey("attributes", $array); + $this->assertCount(0, $array["attributes"]); + $actor->attributes()->username = $faker->username; + $this->assertCount(1, $actor->toArray()["attributes"]); + $this->assertArrayHasKey("edge_list", $array); + $this->assertArrayHasKey("acl", $array); + $this->assertCount(2, $array["acl"]); + $this->assertArrayHasKey("context", $array["acl"]); + $this->assertArrayHasKey("creator", $array["acl"]); + } + + public function testFrameToArray() { + $faker = \Faker\Factory::create(); + $actor = new Actor($this->graph); + $frame = new Frame($actor, $this->graph); + $edge = $actor->writes($frame); + $array = $frame->toArray(); + $this->assertArrayHasKey("id", $array); + $this->assertArrayHasKey("attributes", $array); + $this->assertCount(0, $array["attributes"]); + $actor->attributes()->username = $faker->username; + $this->assertCount(1, $actor->toArray()["attributes"]); + $this->assertArrayHasKey("edge_list", $array); + $this->assertArrayHasKey("acl", $array); + $this->assertCount(2, $array["acl"]); + $this->assertArrayHasKey("context", $array["acl"]); + $this->assertArrayHasKey("creator", $array["acl"]); + $this->assertEquals($actor->id(), $array["acl"]["creator"]); + } + +} \ No newline at end of file diff --git a/tests/SimpleTest.php b/tests/SimpleTest.php deleted file mode 100644 index d0482bf..0000000 --- a/tests/SimpleTest.php +++ /dev/null @@ -1,99 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -// namespace Pho\Framework\Tests; - -use Pho\Framework; -use Pho\Lib\Graph; - -class SimpleTest extends \PHPUnit\Framework\TestCase -{ - private $graph; - - public function setUp() { - $this->graph = new Graph\Graph(); - } - - public function tearDown() { - unset($this->graph); - } - - public function testActor() { - $node = new Framework\Actor($this->graph); - $node_expected_to_be_identical = $this->graph->get($node->id()); - $this->assertEquals($node->id(), $node_expected_to_be_identical->id()); - } - - public function testActorEdge() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); - $edge = $actor->writes($object); - $this->assertInstanceOf(Framework\ActorOut\Writes::class, $edge); - $this->assertInstanceOf(Graph\Predicate::class, $edge->predicate()); - } - - public function testActorPredicate() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); - $edge = $actor->subscribes($object); - $this->assertInstanceOf(Framework\ActorOut\SubscribesPredicate::class, $edge->predicate()); - } - - public function testObjectGetter() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); - $edge = $actor->writes($object); - $this->assertInstanceOf(Framework\ActorOut\Writes::class, $object->getWriters()[0]); - $this->assertCount(1, $object->getWriters()); - $this->assertCount(1, $actor->getWrites()); - $this->assertInstanceOf(Framework\ActorOut\Writes::class, $actor->getWrites()[0]); - } - - public function testFiltering() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); - $edge = $actor->writes($object); - $edge = $actor->reads($object); - $this->assertCount(1, $actor->getWrites()); - } - - /** - * Since write extends subscribes - */ - public function testEdgeInheritance() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); - $edge = $actor->writes($object); - $this->assertCount(1, $actor->getSubscriptions()); - $this->assertCount(0, $actor->getReads()); - } - - /** - * @expectedException Pho\Framework\Exceptions\InvalidEdgeHeadTypeException - */ - public function testImpossibleEdge() { - $actor1 = new Framework\Actor($this->graph); - $actor2 = new Framework\Actor($this->graph); - $edge = $actor1->writes($actor2); - } - - public function testEdgeInvoke() { - $actor = new Framework\Actor($this->graph); - $object = new Framework\Object($this->graph); - $edge = $actor->writes($object); - $this->assertInstanceOf(Framework\Object::class, $edge()); - $this->assertEquals($object->id(), $edge()->id()); - } - - - - -} \ No newline at end of file