Skip to content

Commit

Permalink
Merge pull request #6 from phonetworks/to_lax
Browse files Browse the repository at this point in the history
Edge ready to serialize
  • Loading branch information
esokullu authored May 18, 2017
2 parents fee0ac9 + 43f695c commit 6aa0c54
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 13 deletions.
69 changes: 56 additions & 13 deletions src/Pho/Framework/AbstractEdge.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,50 @@

namespace Pho\Framework;

use Pho\Lib\Graph;

/**
* Framework Edge Foundation
*
*
* This abstract class extends {@link \Pho\Lib\Graph\Edge}
* and acts as a placeholder that defines that its subclasses
* must implement HEAD_LABELS, TAIL_LABEL,TAIL_LABELS
* and acts as a placeholder that defines that its subclasses
* must implement HEAD_LABELS, TAIL_LABEL,TAIL_LABELS
* and SETTABLES constants.
*
*
* @author Emre Sokullu <[email protected]>
*/
abstract class AbstractEdge extends \Pho\Lib\Graph\Edge {
abstract class AbstractEdge extends \Pho\Lib\Graph\Edge
{

/**
* Head Node Label in Singular Form
*
*
* This is what the head node will be called.
* For example; for {@link ActorOut/Subscribes}
* For example; for {@link ActorOut/Subscribes}
* edge, it will be "Subscription".
*/
const HEAD_LABEL = "";

/**
* Head Node Label in Plural Form
*
*
* Same as above, except written in plural
* form.
*/
const HEAD_LABELS = "";

/**
* Tail Node Label in Singular Form
*
*
* This is what the tail node will be called.
* For example; for {@link ActorOut/Subscribes}
* For example; for {@link ActorOut/Subscribes}
* edge, it will be "Subscriber".
*/
const TAIL_LABEL = "";

/**
* Tail Node Label in Plural Form
*
*
* Same as above, except written in plural
* form.
*/
Expand All @@ -71,5 +74,45 @@ public function __invoke(): ParticleInterface
{
return $this->head()->node();
}

}


/**
* @internal
*
* Used for serialization. Nothing special here. Declared for
* subclasses.
*
* @return string in PHP serialized format.
*/
public function serialize(): string
{
return serialize($this->toArray());
}


/**
* @internal
*
* Used for deserialization. Nothing special here. Declared for
* subclasses.
*
* @param string $data
*
* @return void
*
* @throws Exceptions\PredicateClassDoesNotExistException when the predicate class does not exist.
*/
public function unserialize(/* mixed */ $data): void
{
$data = unserialize($data);
$this->id = Graph\ID::fromString($data["id"]);
$this->tail_id = $data["tail"];
$this->head_id = $data["head"];
if (class_exists($data["predicate"])) {
$this->predicate_label = new $data["predicate"];
} else {
throw new PredicateClassDoesNotExistException((string)$this->id(), $data["predicate"]);
}
$this->attributes = new Graph\AttributeBag($this, $data["attributes"]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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\Exceptions;

use Pho\Framework\ParticleInterface;

/**
* Thrown when the given predicate class does not exist.
*
* This is called during serialization of edges. If the predicate does not
* exist, this is thrown and it means some libraries are not installed.
*
* @author Emre Sokullu <[email protected]>
*/
class PredicateClassDoesNotExistException extends \Exception {

/**
* Constructor.
*
* @param string $edge_id The ID of the edge in pure string format
* @param string $predicate_class_name Full class name of the predicate
*/
public function __construct(string $edge_id, string $predicate_class_name)
{
parent::__construct();
$this->message = sprintf(
"The edge (%s) predicate \"%s\" cannot be found.",
$edge_id,
$predicate_class_name
);
}

}

0 comments on commit 6aa0c54

Please sign in to comment.