-
-
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.
Merge pull request #6 from phonetworks/to_lax
Edge ready to serialize
- Loading branch information
Showing
2 changed files
with
98 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
*/ | ||
|
@@ -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"]); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Pho/Framework/Exceptions/PredicateClassDoesNotExistException.php
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,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 | ||
); | ||
} | ||
|
||
} |