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; $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 public function getStatus() : int
{ {
return $this->status; 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) public function setStatus(int $status)
{ {
// todo: check valid
$this->status = $status; $this->status = $status;
} }

View File

@ -69,6 +69,12 @@ abstract class GrammarAbstract
*/ */
protected $or = 'OR'; protected $or = 'OR';
/**
* Table prefix.
*
* @var string
* @since 1.0.0
*/
protected $tablePrefix = ''; 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; 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 public function getDateFormat() : string
{ {
return 'Y-m-d H:i:s'; 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 public function getTablePrefix() : string
{ {
return $this->tablePrefix; 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) public function setTablePrefix(string $prefix)
{ {
$this->tablePrefix = $prefix; $this->tablePrefix = $prefix;

View File

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

View File

@ -214,6 +214,18 @@ class Grammar extends GrammarAbstract
return 'WHERE ' . $expression; 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 protected function compileWhereElement(array $element, Builder $query, bool $first = true) : string
{ {
$expression = ''; $expression = '';
@ -265,6 +277,9 @@ class Grammar extends GrammarAbstract
return $value; return $value;
} }
// todo: fix for injection
// todo: implement binding
return $this->valueQuotes . $value . $this->valueQuotes; return $this->valueQuotes . $value . $this->valueQuotes;
} elseif (is_int($value)) { } elseif (is_int($value)) {
return $value; return $value;

View File

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

View File

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

View File

@ -206,6 +206,17 @@ class Functions
return $t; 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) public static function mod($a, $b)
{ {
if ($a < 0) { if ($a < 0) {
@ -215,6 +226,16 @@ class Functions
return $a % $b; 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 public static function isOdd($a) : bool
{ {
if ($a & 1) { if ($a & 1) {
@ -224,6 +245,16 @@ class Functions
return false; 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 public static function isEven($a) : bool
{ {
if ($a & 1) { if ($a & 1) {
@ -233,6 +264,19 @@ class Functions
return true; 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) public static function getRelativeDegree($value, $length, $start = 0)
{ {
return abs(self::mod($value - $start, $length)); return abs(self::mod($value - $start, $length));

View File

@ -18,7 +18,7 @@ namespace phpOMS\Stdlib\Collection;
use phpOMS\Utils\ArrayUtils; use phpOMS\Utils\ArrayUtils;
/** /**
* Multimap utils. * Collection.
* *
* @category Framework * @category Framework
* @package phpOMS\Stdlib * @package phpOMS\Stdlib
@ -30,28 +30,78 @@ use phpOMS\Utils\ArrayUtils;
*/ */
class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializable class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializable
{ {
/**
* Collection.
*
* @var array
* @since 1.0.0
*/
private $collection = []; 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) public function __construct(array $data)
{ {
$this->collection = $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 public function toArray() : array
{ {
return $this->collection; return $this->collection;
} }
/**
* Json serialize.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function jsonSerialize() public function jsonSerialize()
{ {
return json_encode($this->collection); 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) public function avg($filter = null)
{ {
return $this->sum($filter) / $this->count(); 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) public function sum($filter = null)
{ {
$sum = 0; $sum = 0;
@ -77,16 +127,51 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab
return $sum; return $sum;
} }
/**
* Get collection count.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function count() public function count()
{ {
return count($this->collection); 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 public function collapse() : Collection
{ {
$return = []; $return = [];
@ -116,12 +201,22 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab
return $this; 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 public function contains($find) : bool
{ {
foreach ($this->collection as $key => $value) { foreach ($this->collection as $key => $value) {
if (is_string($find) && ((is_string($value) && $find === $value) || (is_array($value) && in_array($find, $value)))) { if (is_string($find) && ((is_string($value) && $find === $value) || (is_array($value) && in_array($find, $value)))) {
return true; return true;
} elseif ($find instanceof Collection) { } elseif ($find instanceof \Closure) {
$result = $find($value, $key); $result = $find($value, $key);
if ($result) { if ($result) {
@ -133,13 +228,23 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab
return false; 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 = []; $diff = [];
foreach ($this->collection as $key => $value) { foreach ($this->collection as $key => $value) {
if ($value !== current($compare)) { if ($value !== current($compare)) {
$diff = $value; $diff[] = $value;
} }
next($compare); next($compare);
@ -163,6 +268,16 @@ class Collection implements \Countable, \ArrayAccess, \Iterator, \JsonSerializab
return $diff; 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 public function every(int $n) : Collection
{ {
$values = array_values($this->collection); $values = array_values($this->collection);

View File

@ -43,6 +43,16 @@ class BinaryTree extends Tree
return $list; 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) public function getLeft(Node $base)
{ {
$neighbors = $base->getNeighbors($base); $neighbors = $base->getNeighbors($base);
@ -51,6 +61,16 @@ class BinaryTree extends Tree
return $neighbors[0] ?? null; 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) public function getRight(Node $base)
{ {
$neighbors = $base->getNeighbors($base); $neighbors = $base->getNeighbors($base);
@ -59,7 +79,18 @@ class BinaryTree extends Tree
return $neighbors[1] ?? null; 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) { if($this->getLeft($base) === null) {
$this->addNode($base, $left); $this->addNode($base, $left);
@ -68,8 +99,21 @@ class BinaryTree extends Tree
} else { } else {
// todo: replace node // 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) public function setRight(Node $base, Node $right)
{ {
if($this->getRight($base) === null) { 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) public function inOrder(Node $node, \Closure $callback)
{ {
if(count($this->nodes) === 0) {
return;
}
$this->inOrder($this->getLeft($node), $callback); $this->inOrder($this->getLeft($node), $callback);
$callback($node); $callback($node);
$this->inOrder($this->getRight($node), $callback); $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) private function getVerticalOrder(Node $node, int $horizontalDistance = 0, array &$order)
{ {
if(!isset($order[$horizontalDistance])) { 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) public function verticalOrder(Node $node, \Closure $callback)
{ {
$order = []; $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 public function isSymmetric(Node $node1 = null, Node $node2 = null) : bool
{ {
if(!isset($node1) && !isset($node2)) { if(!isset($node1) && !isset($node2)) {

View File

@ -28,31 +28,85 @@ namespace phpOMS\Stdlib\Graph;
*/ */
class Graph class Graph
{ {
/**
* Nodes.
*
* @var array
* @since 1.0.0
*/
protected $nodes = []; protected $nodes = [];
/**
* Edges.
*
* @var array
* @since 1.0.0
*/
protected $edges = []; 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; $this->nodes[] = $node;
return $this; 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; $this->nodes[$key] = $node;
return $this; 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; $this->edges[] = $edge;
return $this; 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) public function setEdge($key, Edge $edge)
{ {
$this->edges[$key] = $edge; $this->edges[$key] = $edge;
@ -60,16 +114,46 @@ class Graph
return $this; 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 public function getNode($key) : Node
{ {
return $this->nodes[$key]; 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 public function getEdge($key) : Edge
{ {
return $this->edges[$key]; 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 public function getEdgesOfNode($node) : array
{ {
if(!($node instanceof Node)) { if(!($node instanceof Node)) {
@ -88,6 +172,16 @@ class Graph
return $edges; 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 public function getNeighbors($node) : array
{ {
if(!($node instanceof Node)) { if(!($node instanceof Node)) {
@ -110,71 +204,196 @@ class Graph
return $neighbors; return $neighbors;
} }
/**
* Get graph dimension.
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getDimension() : int public function getDimension() : int
{ {
// todo: implement
return 0; return 0;
} }
/**
* Get all bridges.
*
* @return Edge[]
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getBridges() : array public function getBridges() : array
{ {
// todo: implement
return []; 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 public function getKruskalMinimalSpanningTree() : Tree
{ {
// todo: implement
return new Tree(); 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 public function getPrimMinimalSpanningTree() : Tree
{ {
// todo: implement
return new Tree(); return new Tree();
} }
/**
* Get circles in graph.
*
* @return array
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getCircle() : array 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 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 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 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 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 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 public function getOrder() : int
{ {
return count($this->nodes); 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 public function getSize() : int
{ {
return count($this->edges); 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 public function getDiameter() : int
{ {
$diameter = 0; $diameter = 0;
@ -194,46 +413,51 @@ class Graph
public function getGirth() : int public function getGirth() : int
{ {
// todo: implement
} }
public function getCircuitRank() : int public function getCircuitRank() : int
{ {
// todo: implement
} }
public function getNodeConnectivity() : int public function getNodeConnectivity() : int
{ {
// todo: implement
} }
public function getEdgeConnectivity() : int public function getEdgeConnectivity() : int
{ {
// todo: implement
} }
public function isConnected() : bool public function isConnected() : bool
{ {
// todo: implement
return true; return true;
} }
public function getUnconnected() : array public function getUnconnected() : array
{ {
// todo: implement
// get all unconnected sub graphs // get all unconnected sub graphs
} }
public function isBipartite() : bool public function isBipartite() : bool
{ {
// todo: implement
return true; return true;
} }
public function isTriangleFree() : bool public function isTriangleFree() : bool
{ {
// todo: implement
return true; return true;
} }
public function isCircleFree() : bool public function isCircleFree() : bool
{ {
// todo: implement
return true; return true;
} }
} }

View File

@ -28,20 +28,55 @@ namespace phpOMS\Stdlib\Graph;
*/ */
class Tree extends Graph class Tree extends Graph
{ {
/**
* Root node.
*
* @var Node
* @since 1.0.0
*/
private $root = null; private $root = null;
/**
* Constructor.
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function __construct() public function __construct()
{ {
$root = new Node(); $root = new Node();
parent::addNode($root); 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::addNode($node);
parent::addEdge(new Edge($base, $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 public function getMaxDepth(Node $node = null) : int
{ {
$currentNode = $node ?? $this->root; $currentNode = $node ?? $this->root;
@ -60,6 +95,16 @@ class Tree extends Graph
return $depth; 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 public function getMinDepth(Node $node = null) : int
{ {
$currentNode = $node ?? $this->root; $currentNode = $node ?? $this->root;
@ -80,7 +125,18 @@ class Tree extends Graph
return min($depth) + 1; 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(); $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 public function isLeaf(Node $node) : bool
{ {
return count($this->getEdgesOfNode($node)) === 1; 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 public function getLevelNodes(int $level, Node $node) : array
{ {
--$level; --$level;
@ -112,7 +189,18 @@ class Tree extends Graph
return $nodes; 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) { if(count($this->edges) % $type !== 0) {
return false; return false;
} }
@ -128,6 +216,15 @@ class Tree extends Graph
return true; 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) { public function preOrder(Node $node, \Closure $callback) {
if(count($this->nodes) === 0) { if(count($this->nodes) === 0) {
return; 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) { public function postOrder(Node $node, \Closure $callback) {
if(count($this->nodes) === 0) { if(count($this->nodes) === 0) {
return; return;

View File

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

View File

@ -30,6 +30,9 @@ use phpOMS\Validation\Base\DateTime;
*/ */
class TaskScheduler extends SchedulerAbstract class TaskScheduler extends SchedulerAbstract
{ {
/**
* {@inheritdoc}
*/
public function save() public function save()
{ {
@ -74,12 +77,33 @@ class TaskScheduler extends SchedulerAbstract
return trim($stdout); 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 private function normalize(string $raw) : string
{ {
return str_replace("\r\n", "\n", $raw); 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 = TaskFactory::create($jobData[1], '');
$job->setRun($jobData[8]); $job->setRun($jobData[8]);
@ -109,6 +133,9 @@ class TaskScheduler extends SchedulerAbstract
return $job; return $job;
} }
/**
* {@inheritdoc}
*/
public function getAll() : array public function getAll() : array
{ {
$lines = explode("\n", $this->normalize($this->run('/query /v /fo CSV'))); $lines = explode("\n", $this->normalize($this->run('/query /v /fo CSV')));
@ -122,16 +149,25 @@ class TaskScheduler extends SchedulerAbstract
return $jobs; return $jobs;
} }
/**
* {@inheritdoc}
*/
public function get(string $id) public function get(string $id)
{ {
// todo: implement
} }
/**
* {@inheritdoc}
*/
public function getByName(string $name) : Schedule public function getByName(string $name) : Schedule
{ {
// todo: implement
} }
/**
* {@inheritdoc}
*/
public function getAllByName(string $name, bool $exact = true) : array public function getAllByName(string $name, bool $exact = true) : array
{ {
if($exact) { if($exact) {
@ -160,8 +196,11 @@ class TaskScheduler extends SchedulerAbstract
return $jobs; return $jobs;
} }
/**
* {@inheritdoc}
*/
public function create(Schedule $task) public function create(Schedule $task)
{ {
// todo: implement
} }
} }