This commit is contained in:
Dennis Eichhorn 2016-11-15 21:25:39 +01:00
parent 8212751b2e
commit 3ec569fa9e
13 changed files with 890 additions and 37 deletions

View File

@ -179,13 +179,30 @@ class Group implements ArrayableInterface, \JsonSerializable
$this->description = $description;
}
/**
* Get group status.
*
* @return int Group status
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getStatus() : int
{
return $this->status;
}
/**
* Set group status.
*
* @param int $status Group status
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setStatus(int $status)
{
// todo: check valid
$this->status = $status;
}

View File

@ -69,6 +69,12 @@ abstract class GrammarAbstract
*/
protected $or = 'OR';
/**
* Table prefix.
*
* @var string
* @since 1.0.0
*/
protected $tablePrefix = '';
/**
@ -95,18 +101,52 @@ abstract class GrammarAbstract
) . ';';
}
/**
* Compile query components.
*
* @param BuilderAbstract $query Builder
*
* @return array Parsed query components
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
abstract protected function compileComponents(BuilderAbstract $query) : array;
/**
* Get date format.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getDateFormat() : string
{
return 'Y-m-d H:i:s';
}
/**
* Get table prefix.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getTablePrefix() : string
{
return $this->tablePrefix;
}
/**
* Set table prefix.
*
* @param string $prefix Table prefix
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setTablePrefix(string $prefix)
{
$this->tablePrefix = $prefix;

View File

@ -704,43 +704,103 @@ class Builder extends BuilderAbstract
return $this;
}
/**
* Lock query.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function lock()
{
}
/**
* Lock for update query.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function lockUpdate()
{
}
/**
* Create query string.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __toString()
{
return $this->grammar->compileQuery($this);
}
/**
* Find query.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function find()
{
}
/**
* Count results.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function count()
{
}
/**
* Check if exists.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function exists()
{
}
/**
* Select minimum.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function min()
{
}
/**
* Select maximum.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function max()
{
}
/**
* Select sum.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function sum()
{
}
/**
* Select average.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function avg()
{
}
@ -821,6 +881,16 @@ class Builder extends BuilderAbstract
return $this;
}
/**
* Update columns.
*
* @param array $columns Column names to update
*
* @return Builder
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function update(...$columns) : Builder
{
$this->type = QueryType::UPDATE;
@ -832,44 +902,106 @@ class Builder extends BuilderAbstract
return $this;
}
/**
* Increment value.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function increment()
{
}
/**
* Decrement value.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function decrement()
{
}
/**
* Join.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function join($table1, $table2, $column1, $opperator, $column2)
{
return $this;
}
/**
* Join where.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function joinWhere()
{
}
/**
* Left join.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function leftJoin()
{
}
/**
* Left join where.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function leftJoinWhere()
{
}
/**
* Right join.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function rightJoin()
{
}
/**
* Right join where.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function rightJoinWhere()
{
}
/**
* Rollback.
*
* @return Builder
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function rollback()
{
return $this;
}
/**
* On.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function on()
{
@ -892,6 +1024,14 @@ class Builder extends BuilderAbstract
return clone($this);
}
/**
* Execute query.
*
* @return mixed
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function execute()
{
$sth = $this->connection->con->prepare($this->toSql());

View File

@ -214,6 +214,18 @@ class Grammar extends GrammarAbstract
return 'WHERE ' . $expression;
}
/**
* Compile where element.
*
* @param array $element Element data
* @param Builder $query Query builder
* @param bool $first Is first element (usefull for nesting)
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
protected function compileWhereElement(array $element, Builder $query, bool $first = true) : string
{
$expression = '';
@ -265,6 +277,9 @@ class Grammar extends GrammarAbstract
return $value;
}
// todo: fix for injection
// todo: implement binding
return $this->valueQuotes . $value . $this->valueQuotes;
} elseif (is_int($value)) {
return $value;

View File

@ -129,12 +129,26 @@ class HttpSession implements SessionInterface
return $this->sessionData[$key] ?? null;
}
/**
* Lock session from further adjustments.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function lock()
{
self::$isLocked = true;
}
public static function isLocked()
/**
* Check if session is locked.
*
* @return bool Lock status
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function isLocked() : bool
{
return self::$isLocked;
}
@ -177,10 +191,18 @@ class HttpSession implements SessionInterface
$this->sid = $sid;
}
/**
* Destruct session.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __destruct()
{
$_SESSION = $this->sessionData;
session_write_close();
if(!self::$isLocked) {
$_SESSION = $this->sessionData;
session_write_close();
}
}
}

View File

@ -119,9 +119,17 @@ class FileLogger implements LoggerInterface
$this->path = $path;
}
/**
* Create logging file.
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function createFile()
{
if (!file_exists($this->path) && !$this->created) {
if (!$this->created && !file_exists($this->path)) {
File::create($this->path);
$this->created = true;
}

View File

@ -206,6 +206,17 @@ class Functions
return $t;
}
/**
* Modular implementation for negative values.
*
* @param int $a
* @param int $b
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function mod($a, $b)
{
if ($a < 0) {
@ -215,6 +226,16 @@ class Functions
return $a % $b;
}
/**
* Check if value is odd.
*
* @param int $a Value to test
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function isOdd($a) : bool
{
if ($a & 1) {
@ -224,6 +245,16 @@ class Functions
return false;
}
/**
* Check if value is even.
*
* @param int $a Value to test
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function isEven($a) : bool
{
if ($a & 1) {
@ -233,6 +264,19 @@ class Functions
return true;
}
/**
* Gets the relative position on a circular construct.
*
* @example The relative fiscal month (August) in a company where the fiscal year starts in July.
* @example 2 = getRelativeDegree(8, 12, 7);
*
* @param int $a Value to test
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function getRelativeDegree($value, $length, $start = 0)
{
return abs(self::mod($value - $start, $length));

View File

@ -18,7 +18,7 @@ namespace phpOMS\Stdlib\Collection;
use phpOMS\Utils\ArrayUtils;
/**
* Multimap utils.
* Collection.
*
* @category Framework
* @package phpOMS\Stdlib
@ -30,28 +30,78 @@ use phpOMS\Utils\ArrayUtils;
*/
class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializable
{
/**
* Collection.
*
* @var array
* @since 1.0.0
*/
private $collection = [];
/**
* Create collection from array.
*
* @param array $data Collection data
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct(array $data)
{
$this->collection = $data;
}
/**
* Turn collection to array.
*
* @return array Collection array representation
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function toArray() : array
{
return $this->collection;
}
/**
* Json serialize.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function jsonSerialize()
{
return json_encode($this->collection);
}
/**
* Get average of collection data.
*
* @param mixed $filter Filter for average calculation
*
* @return mixed
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function avg($filter = null)
{
return $this->sum($filter) / $this->count();
}
/**
* Get sum of collection data.
*
* @param mixed $filter Filter for sum calculation
*
* @return mixed
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function sum($filter = null)
{
$sum = 0;
@ -77,16 +127,51 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab
return $sum;
}
/**
* Get collection count.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function count()
{
return count($this->collection);
}
public function chunk(int $size) : Collection
/**
* Chunk collection.
*
* Creates new collection in the specified size.
*
* @param int $size Chunk size
*
* @return Collection[]
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function chunk(int $size) : array
{
return new self(array_chunk($this->collection, $size));
$arrays = array_chunk($this->collection, $size);
$collections = [];
foreach($arrays as $array) {
$collections[] = new self($array);
}
return $collections;
}
/**
* Collapse collection.
*
* @return Collection
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function collapse() : Collection
{
$return = [];
@ -116,12 +201,22 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab
return $this;
}
/**
* Check if collection contains a value.
*
* @param string|int|float|\Closure $find Needle
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function contains($find) : bool
{
foreach ($this->collection as $key => $value) {
if (is_string($find) && ((is_string($value) && $find === $value) || (is_array($value) && in_array($find, $value)))) {
return true;
} elseif ($find instanceof Collection) {
} elseif ($find instanceof \Closure) {
$result = $find($value, $key);
if ($result) {
@ -133,13 +228,23 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab
return false;
}
public function diff(array $compare) : array
/**
* Diff of collection.
*
* @param Collection|array $compare To compare with
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function diff($compare) : array
{
$diff = [];
foreach ($this->collection as $key => $value) {
if ($value !== current($compare)) {
$diff = $value;
$diff[] = $value;
}
next($compare);
@ -163,6 +268,16 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab
return $diff;
}
/**
* Get collection that contains every n-th element.
*
* @param $int $n Every n-th element
*
* @return Collection
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function every(int $n) : Collection
{
$values = array_values($this->collection);

View File

@ -43,6 +43,16 @@ class BinaryTree extends Tree
return $list;
}
/**
* Get left node of a node.
*
* @param Node $node Tree node
*
* @return Node Left node
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getLeft(Node $base)
{
$neighbors = $base->getNeighbors($base);
@ -51,6 +61,16 @@ class BinaryTree extends Tree
return $neighbors[0] ?? null;
}
/**
* Get right node of a node.
*
* @param Node $node Tree node
*
* @return Node Right node
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getRight(Node $base)
{
$neighbors = $base->getNeighbors($base);
@ -59,7 +79,18 @@ class BinaryTree extends Tree
return $neighbors[1] ?? null;
}
public function setLeft(Node $base, Node $left)
/**
* Set left node of node.
*
* @param Node $base Base node
* @param Node $left Left node
*
* @return BinaryTree
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setLeft(Node $base, Node $left) : BinaryTree
{
if($this->getLeft($base) === null) {
$this->addNode($base, $left);
@ -68,8 +99,21 @@ class BinaryTree extends Tree
} else {
// todo: replace node
}
return $this;
}
/**
* Set right node of node.
*
* @param Node $base Base node
* @param Node $right Right node
*
* @return BinaryTree
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setRight(Node $base, Node $right)
{
if($this->getRight($base) === null) {
@ -81,17 +125,32 @@ class BinaryTree extends Tree
}
}
/**
* Perform action on tree in in-order.
*
* @param Node $node Tree node
* @param \Closure $callback Task to perform on node
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function inOrder(Node $node, \Closure $callback)
{
if(count($this->nodes) === 0) {
return;
}
$this->inOrder($this->getLeft($node), $callback);
$callback($node);
$this->inOrder($this->getRight($node), $callback);
}
/**
* Get nodes in vertical order.
*
* @param Node $node Tree node
* @param int $horizontalDistance Horizontal distance
* @param Node[] &$order Ordered nodes by horizontal distance
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function getVerticalOrder(Node $node, int $horizontalDistance = 0, array &$order)
{
if(!isset($order[$horizontalDistance])) {
@ -111,6 +170,15 @@ class BinaryTree extends Tree
}
}
/**
* Perform action on tree in vertical-order.
*
* @param Node $node Tree node
* @param \Closure $callback Task to perform on node
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function verticalOrder(Node $node, \Closure $callback)
{
$order = [];
@ -123,6 +191,15 @@ class BinaryTree extends Tree
}
}
/**
* Check if tree is symmetric.
*
* @param Node $node1 Tree node1
* @param Node $node2 Tree node2 (optional, can be different tree)
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function isSymmetric(Node $node1 = null, Node $node2 = null) : bool
{
if(!isset($node1) && !isset($node2)) {

View File

@ -28,31 +28,85 @@ namespace phpOMS\Stdlib\Graph;
*/
class Graph
{
/**
* Nodes.
*
* @var array
* @since 1.0.0
*/
protected $nodes = [];
/**
* Edges.
*
* @var array
* @since 1.0.0
*/
protected $edges = [];
public function addNode(Node $node)
/**
* Add node to graph.
*
* @param Node $node Graph node
*
* @return Graph
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function addNode(Node $node) : Graph
{
$this->nodes[] = $node;
return $this;
}
public function setNode($key, Node $node)
/**
* Set node in graph.
*
* @param mixed $key Key of node
* @param Node $node Graph node
*
* @return Graph
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setNode($key, Node $node) : Graph
{
$this->nodes[$key] = $node;
return $this;
}
public function addEdge(Edge $edge)
/**
* Add edge to graph.
*
* @param Edge $edge Graph edge
*
* @return Graph
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function addEdge(Edge $edge) : Graph
{
$this->edges[] = $edge;
return $this;
}
/**
* Set edge in graph.
*
* @param mixed $key Edge key
* @param Edge $edge Edge to set
*
* @return Graph
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function setEdge($key, Edge $edge)
{
$this->edges[$key] = $edge;
@ -60,16 +114,46 @@ class Graph
return $this;
}
/**
* Get graph node
*
* @param mixed $key Node key
*
* @return Node
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getNode($key) : Node
{
return $this->nodes[$key];
}
/**
* Get graph edge.
*
* @param mixed $key Edge key
*
* @return Edge
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getEdge($key) : Edge
{
return $this->edges[$key];
}
/**
* Get all edges of a node
*
* @param mixed $node Node
*
* @return Edge[]
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getEdgesOfNode($node) : array
{
if(!($node instanceof Node)) {
@ -88,6 +172,16 @@ class Graph
return $edges;
}
/**
* Get all node neighbors.
*
* @param Node $node Graph node
*
* @return Node[]
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getNeighbors($node) : array
{
if(!($node instanceof Node)) {
@ -110,71 +204,196 @@ class Graph
return $neighbors;
}
/**
* Get graph dimension.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getDimension() : int
{
// todo: implement
return 0;
}
/**
* Get all bridges.
*
* @return Edge[]
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getBridges() : array
{
// todo: implement
return [];
}
/**
* Get minimal spanning tree using Kruskal's algorithm.
*
* @return Tree
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getKruskalMinimalSpanningTree() : Tree
{
// todo: implement
return new Tree();
}
/**
* Get minimal spanning tree using Prim's algorithm
*
* @return Tree
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getPrimMinimalSpanningTree() : Tree
{
// todo: implement
return new Tree();
}
/**
* Get circles in graph.
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getCircle() : array
{
// todo: implement
}
/**
* Get shortest path using Floyd Warschall algorithm.
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getFloydWarshallShortestPath() : array
{
// todo: implement
}
/**
* Get shortest path using Dijkstra algorithm.
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getDijkstraShortestPath() : array
{
// todo: implement
}
/**
* Perform depth first traversal.
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function depthFirstTraversal() : array
{
// todo: implement
}
/**
* Perform breadth first traversal.
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function breadthFirstTraversal() : array
{
// todo: implement
}
/**
* Get longest path in graph.
*
* @return Node[]
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function longestPath() : array
{
// todo: implement
}
public function longestPathBetweenNodes() : array
/**
* Get longest path between two nodes.
*
* @param Node $node1 Graph node
* @param Node $node2 Graph node
*
* @return Node[]
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function longestPathBetweenNodes(Node $node1, Node $node2) : array
{
// todo: implement
}
/**
* Get order of the graph.
*
* The order of a graph is the amount of nodes it contains.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getOrder() : int
{
return count($this->nodes);
}
/**
* Get size of the graph.
*
* The size of the graph is the amount of edges it contains.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getSize() : int
{
return count($this->edges);
}
/**
* Get diameter of graph.
*
* The diameter of a graph is the longest shortest path between two nodes.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getDiameter() : int
{
$diameter = 0;
@ -194,46 +413,51 @@ class Graph
public function getGirth() : int
{
// todo: implement
}
public function getCircuitRank() : int
{
// todo: implement
}
public function getNodeConnectivity() : int
{
// todo: implement
}
public function getEdgeConnectivity() : int
{
// todo: implement
}
public function isConnected() : bool
{
// todo: implement
return true;
}
public function getUnconnected() : array
{
// todo: implement
// get all unconnected sub graphs
}
public function isBipartite() : bool
{
// todo: implement
return true;
}
public function isTriangleFree() : bool
{
// todo: implement
return true;
}
public function isCircleFree() : bool
{
// todo: implement
return true;
}
}

View File

@ -28,20 +28,55 @@ namespace phpOMS\Stdlib\Graph;
*/
class Tree extends Graph
{
/**
* Root node.
*
* @var Node
* @since 1.0.0
*/
private $root = null;
/**
* Constructor.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct()
{
$root = new Node();
parent::addNode($root);
}
public function addRelativeNode(Node $base, Node $node)
/**
* Add a note relative to a node.
*
* @param Node $base Base node
* @param Node $node Node to add
*
* @return Tree
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function addRelativeNode(Node $base, Node $node) : Tree
{
parent::addNode($node);
parent::addEdge(new Edge($base, $node));
return $this;
}
/**
* Get maximum tree depth.
*
* @param Node $node Tree node
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getMaxDepth(Node $node = null) : int
{
$currentNode = $node ?? $this->root;
@ -60,6 +95,16 @@ class Tree extends Graph
return $depth;
}
/**
* Get minimum tree path.
*
* @param Node $node Tree node
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getMinDepth(Node $node = null) : int
{
$currentNode = $node ?? $this->root;
@ -80,7 +125,18 @@ class Tree extends Graph
return min($depth) + 1;
}
public function levelOrder(\Closure $callback)
/**
* Perform task on tree nodes in level order.
*
* @param Node $node Tree node
* @param \Closure $callback Task to perform
*
* @return void
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function levelOrder(Node $node, \Closure $callback)
{
$depth = $this->getMaxDepth();
@ -90,11 +146,32 @@ class Tree extends Graph
}
}
/**
* Check if node is leaf.
*
* @param Node $node Tree node
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function isLeaf(Node $node) : bool
{
return count($this->getEdgesOfNode($node)) === 1;
}
/**
* Get all nodes of a specific level.
*
* @param int $level Level to retrieve
* @param Node $node Tree node
*
* @return Node[]
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getLevelNodes(int $level, Node $node) : array
{
--$level;
@ -112,7 +189,18 @@ class Tree extends Graph
return $nodes;
}
public function isFull(int $type) : bool {
/**
* Check if the tree is full.
*
* @param int $type Child nodes per non-leaf node
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function isFull(int $type) : bool
{
if(count($this->edges) % $type !== 0) {
return false;
}
@ -128,6 +216,15 @@ class Tree extends Graph
return true;
}
/**
* Perform action on tree in pre-order.
*
* @param Node $node Tree node
* @param \Closure $callback Task to perform on node
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function preOrder(Node $node, \Closure $callback) {
if(count($this->nodes) === 0) {
return;
@ -142,6 +239,15 @@ class Tree extends Graph
}
}
/**
* Perform action on tree in post-order.
*
* @param Node $node Tree node
* @param \Closure $callback Task to perform on node
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function postOrder(Node $node, \Closure $callback) {
if(count($this->nodes) === 0) {
return;

View File

@ -38,6 +38,12 @@ final class Storage
*/
private static $registered = [];
/**
* Constructor.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
private function __construct()
{

View File

@ -30,6 +30,9 @@ use phpOMS\Validation\Base\DateTime;
*/
class TaskScheduler extends SchedulerAbstract
{
/**
* {@inheritdoc}
*/
public function save()
{
@ -74,12 +77,33 @@ class TaskScheduler extends SchedulerAbstract
return trim($stdout);
}
/**
* Normalize run result for easier parsing
*
* @param string $raw Raw command output
*
* @return string Normalized string for parsing
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
private function normalize(string $raw) : string
{
return str_replace("\r\n", "\n", $raw);
}
private function parseJobList(array $jobData) {
/**
* Parse a list of jobs
*
* @param array $jobData Csv data containing the job information
*
* @return TaskAbstract Parsed job
*
* @since 1.0.0
* @author Dennis Eichhorn
*/
private function parseJobList(array $jobData) : TaskAbstract
{
$job = TaskFactory::create($jobData[1], '');
$job->setRun($jobData[8]);
@ -109,6 +133,9 @@ class TaskScheduler extends SchedulerAbstract
return $job;
}
/**
* {@inheritdoc}
*/
public function getAll() : array
{
$lines = explode("\n", $this->normalize($this->run('/query /v /fo CSV')));
@ -122,16 +149,25 @@ class TaskScheduler extends SchedulerAbstract
return $jobs;
}
/**
* {@inheritdoc}
*/
public function get(string $id)
{
// todo: implement
}
/**
* {@inheritdoc}
*/
public function getByName(string $name) : Schedule
{
// todo: implement
}
/**
* {@inheritdoc}
*/
public function getAllByName(string $name, bool $exact = true) : array
{
if($exact) {
@ -160,8 +196,11 @@ class TaskScheduler extends SchedulerAbstract
return $jobs;
}
/**
* {@inheritdoc}
*/
public function create(Schedule $task)
{
// todo: implement
}
}