From 277e360159373e2474c1fc637dd6f88049e54921 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 6 May 2016 23:39:33 +0200 Subject: [PATCH] Formatting and new implementations --- DataStorage/Cookie/CookieJar.php | 102 ++++++++++++++++--- Math/Algebra/PointPolygonIntersection.php | 47 +++++++-- Math/Algebra/Regression.php | 2 +- Math/Geometry/ConvexHull/MonotoneChain.php | 111 +++++++++++++++++++++ Math/Matrix/Cholesky.php | 0 Math/Matrix/InverseType.php | 4 +- Math/Number/Integer.php | 38 ++++++- Math/Number/Natural.php | 37 +++++++ Math/Optimization/GaussianElimination.php | 65 +++++++++++- Math/Optimization/Graph/Dijkstra.php | 3 +- Utils/TaskSchedule/SchedulerFactory.php | 35 +++++++ Utils/TaskSchedule/TaskFactory.php | 38 +++++++ 12 files changed, 455 insertions(+), 27 deletions(-) create mode 100644 Math/Geometry/ConvexHull/MonotoneChain.php create mode 100644 Math/Matrix/Cholesky.php diff --git a/DataStorage/Cookie/CookieJar.php b/DataStorage/Cookie/CookieJar.php index 6a0248584..3b05fe661 100644 --- a/DataStorage/Cookie/CookieJar.php +++ b/DataStorage/Cookie/CookieJar.php @@ -13,38 +13,76 @@ * @version 1.0.0 * @link http://orange-management.com */ -/* -NOT IN USE -Will be implemented later -*/ -/* TODO: implement */ + namespace phpOMS\DataStorage\Cookie; /** - * @since 1.0.0 - * @author Dennis Eichhorn + * CookieJar class + * + * @category Framework + * @package phpOMS\Utils + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 */ class CookieJar { + /** + * Cookie values. + * + * @var array + * @since 1.0.0 + */ private $cookies = []; + /** + * Locked. + * + * @var bool + * @since 1.0.0 + */ private static $isLocked = false; + /** + * Constructor. + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function __construct() { $this->cookies = $_COOKIE; } - public function set($id, $value, int $expiry = 86400, $path = '/', $domain = null, bool $secure = false, bool $httponly = true, bool $overwrite = true) : bool + /** + * Set pending cookie + * + * @param string $id Cookie id + * @param mixed $value Cookie value + * @param int $expire Expire time + * @param string $path Path + * @param string $domain Domain + * @param bool $secure Is secure + * @param bool $httpOnly Allow only http access + * @param bool $overwrite Overwrite if already set + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function set(string $id, $value, int $expire = 86400, string $path = '/', string $domain = null, bool $secure = false, bool $httpOnly = true, bool $overwrite = true) : bool { if ($overwrite || !isset($this->cookies[$id])) { $this->cookies[$id] = [ 'value' => $value, - 'expiry' => $expiry, + 'expiry' => $expire, 'path' => $path, 'domain' => $domain, 'secure' => $secure, - 'httponly' => $httponly, + 'httponly' => $httpOnly, ]; return true; @@ -53,7 +91,17 @@ class CookieJar return false; } - public function remove($id) : bool + /** + * Remove pending cookie + * + * @param string $id Cookie id to remove + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function remove(string $id) : bool { if (isset($this->cookies[$id])) { unset($this->cookies[$id]); @@ -64,12 +112,28 @@ class CookieJar return false; } - public function delete($id) : bool + /** + * Delete already set cookie + * + * @param string $id Cookie id to remove + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function delete(string $id) { $this->remove($id); setcookie($id, '', time() - 3600); } + /** + * Save cookie + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public function save() { if (self::$isLocked) { @@ -81,11 +145,25 @@ class CookieJar } } + /** + * Lock + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function lock() { self::$isLocked = true; } + /** + * Is locked? + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function isLocked() : bool { return self::$isLocked; diff --git a/Math/Algebra/PointPolygonIntersection.php b/Math/Algebra/PointPolygonIntersection.php index 3725b3e4a..2c7aaef36 100644 --- a/Math/Algebra/PointPolygonIntersection.php +++ b/Math/Algebra/PointPolygonIntersection.php @@ -15,14 +15,38 @@ */ namespace phpOMS\Math\Algebra; -class PointPolygonIntersection +/** + * Scheduler factory. + * + * @category Framework + * @package phpOMS\Utils\TaskSchedule + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +final class PointPolygonIntersection { + /** + * Epsilon. + * + * @var float + * @since 1.0.0 + */ const EPSILON = 1E-6; - private function __construct() - { - } - + /** + * Point polygon relative position + * + * @param array $point Point location + * @param array $vertices Vertice locations + * + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function pointInPolygon(array $point, array $vertices) : int { $length = count($vertices); @@ -70,7 +94,18 @@ class PointPolygonIntersection return 1; } - private static function isOnVertex($point, $vertices) + /** + * Is point on vertex? + * + * @param array $point Point location + * @param array $vertices Vertice locations + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private static function isOnVertex($point, $vertices) : bool { foreach ($vertices as $vertex) { if (abs($point['x'] - $vertex['x']) < self::EPSILON && abs($point['y'] - $vertex['y']) < self::EPSILON) { diff --git a/Math/Algebra/Regression.php b/Math/Algebra/Regression.php index 107b67918..c133e2450 100644 --- a/Math/Algebra/Regression.php +++ b/Math/Algebra/Regression.php @@ -39,7 +39,7 @@ class Regression * * @return array * - * @throws + * @throws \Exception * * @since 1.0.0 * @author Dennis Eichhorn diff --git a/Math/Geometry/ConvexHull/MonotoneChain.php b/Math/Geometry/ConvexHull/MonotoneChain.php new file mode 100644 index 000000000..bb569026f --- /dev/null +++ b/Math/Geometry/ConvexHull/MonotoneChain.php @@ -0,0 +1,111 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Geometry; + +/** + * Andrew's monotone chain convex hull algorithm class. + * + * @category Framework + * @package phpOMS\Utils\TaskSchedule + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + * + * @todo: implement vertice class or use vertice class used by graphs? May be usefull in order to give vertices IDs! + */ +final class MonotoneChain +{ + /** + * Counter clock wise turn? + * + * @param array $a Point b + * @param array $b Point c + * @param array $c Point d + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private static function cross(array $a, array $b, array $c) : float + { + return ($b['x'] - $a['x']) * ($c['y'] - $a['y']) - ($b['y'] - $a['y']) * ($c['x'] - $a['x']); + } + + /** + * Sort by x coordinate then by z coordinate + * + * @param array $a Point b + * @param array $b Point c + * + * @return float + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private static function sort(array $a, array $b) : float + { + return $a['x'] === $b['x'] ? $a['y'] - $b['y'] : $a['x'] - $b['x']; + } + + /** + * Create convex hull + * + * @param array $points Points (Point Cloud) + * + * @return array + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public static function createConvexHull(array $points) : array + { + if (($n = count($points)) > 1) { + $k = 0; + $h = []; + + uasort($points, [self::class, 'sort']); + + // Lower hull + for ($i = 0; $i < $n; ++$i) { + while ($k >= 2 && self::cross($h[$k - 2], $h[$k - 1], $points[$i]) <= 0) { + $k--; + } + + $h[$k++] = $points[$i]; + } + + // Upper hull + for ($i = $n - 2, $t = $k + 1; $i >= 0; $i--) { + while ($k >= $t && self::cross($h[$k - 2], $h[$k - 1], $points[$i]) <= 0) { + $k--; + } + + $h[$k++] = $points[$i]; + } + + if ($k > 1) { + $h = array_splice($h, $k - 1); + } + + return $h; + } + + return $points; + } +} \ No newline at end of file diff --git a/Math/Matrix/Cholesky.php b/Math/Matrix/Cholesky.php new file mode 100644 index 000000000..e69de29bb diff --git a/Math/Matrix/InverseType.php b/Math/Matrix/InverseType.php index 47b9ccdd0..64896bac6 100644 --- a/Math/Matrix/InverseType.php +++ b/Math/Matrix/InverseType.php @@ -18,10 +18,10 @@ namespace phpOMS\Math\Matrix; use phpOMS\Datatypes\Enum; /** - * Account type enum. + * Inverse type enum. * * @category Framework - * @package phpOMS\DataStorage\Database + * @package phpOMS\Math\Matrix * @author OMS Development Team * @author Dennis Eichhorn * @license OMS License 1.0 diff --git a/Math/Number/Integer.php b/Math/Number/Integer.php index 7311da752..4f7677897 100644 --- a/Math/Number/Integer.php +++ b/Math/Number/Integer.php @@ -1,8 +1,44 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Number; + +/** + * Integer class + * + * @category Framework + * @package phpOMS\Utils + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class Integer { + /** + * Is integer. + * + * @param mixed $value Value to test + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function isInteger($value) : bool { return is_int($value); diff --git a/Math/Number/Natural.php b/Math/Number/Natural.php index 4f3f2be84..2f655d209 100644 --- a/Math/Number/Natural.php +++ b/Math/Number/Natural.php @@ -1,7 +1,44 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Math\Number; + +/** + * Natura number class + * + * @category Framework + * @package phpOMS\Utils + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class Natural { + /** + * Is natural number. + * + * @param mixed $value Value to test + * + * @return bool + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function isNatural($value) : bool { return is_int($value) && $value >= 0; diff --git a/Math/Optimization/GaussianElimination.php b/Math/Optimization/GaussianElimination.php index f1f36fb63..363024bff 100644 --- a/Math/Optimization/GaussianElimination.php +++ b/Math/Optimization/GaussianElimination.php @@ -1,8 +1,48 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +use phpOMS\Math\Matrix\Matrix; + +/** + * Gaussian elimination class + * + * @category Framework + * @package phpOMS\Math\Matrix + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ class GaussianElimination { - private function swapRows(&$a, &$b, $r1, $r2) + /** + * Swap rows. + * + * @param array $a Matrix A + * @param array $b Vector b + * @param int $r1 Row 1 + * @param int $r2 Row 2 + * + * @return void + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + private function swapRows(&$a, &$b, int $r1, int $r2) { if ($r1 == $r2) { return; @@ -17,8 +57,23 @@ class GaussianElimination $b[$r2] = $tmp; } - public function solve(array $A, array $b, int $limit) : array + /** + * Solve equation with gaussian elimination. + * + * @param Matrix $A Matrix A + * @param Matrix $b Vector b + * + * @return Matrix + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function solve(Matrix $A, Matrix $b) : Matrix { + $limit = min($A->getM(), $A->getN()); + $A = $A->getMatrix(); + $b = $b->getMatrix(); + for ($col = 0; $col < $limit; $col++) { $j = $col; $max = $A[$j][$j]; @@ -47,7 +102,6 @@ class GaussianElimination } $x = []; - for ($col = $limit - 1; $col >= 0; $col--) { $tmp = $b[$col]; @@ -58,6 +112,9 @@ class GaussianElimination $x[$col] = $tmp / $A[$col][$col]; } - return $x; + $Y = new Matrix(count($x), 1); + $Y->setMatrix($x); + + return $Y; } } \ No newline at end of file diff --git a/Math/Optimization/Graph/Dijkstra.php b/Math/Optimization/Graph/Dijkstra.php index 446c4b555..506a6a95e 100644 --- a/Math/Optimization/Graph/Dijkstra.php +++ b/Math/Optimization/Graph/Dijkstra.php @@ -8,8 +8,9 @@ class Dijkstra { $vertices = []; $neighbours = []; + $graphArray = $graph->getEdges(); - foreach ($graph_array as $edge) { + foreach ($graphArray as $edge) { array_push($vertices, $edge[0], $edge[1]); $neighbours[$edge[0]][] = ["end" => $edge[1], "cost" => $edge[2]]; $neighbours[$edge[1]][] = ["end" => $edge[0], "cost" => $edge[2]]; diff --git a/Utils/TaskSchedule/SchedulerFactory.php b/Utils/TaskSchedule/SchedulerFactory.php index f22b37967..083ec0b95 100644 --- a/Utils/TaskSchedule/SchedulerFactory.php +++ b/Utils/TaskSchedule/SchedulerFactory.php @@ -1,12 +1,47 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ namespace phpOMS\Utils\TaskSchedule; use phpOMS\System\OperatingSystem; use phpOMS\System\SystemType; +/** + * Scheduler factory. + * + * @category Framework + * @package phpOMS\Utils\TaskSchedule + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ final class SchedulerFactory { + /** + * Create scheduler instance. + * + * @return ScheduleInterface + * + * @throws \Exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function create() : ScheduleInterface { switch (OperatingSystem::getSystem()) { diff --git a/Utils/TaskSchedule/TaskFactory.php b/Utils/TaskSchedule/TaskFactory.php index 25c2a8c16..9e42e5e50 100644 --- a/Utils/TaskSchedule/TaskFactory.php +++ b/Utils/TaskSchedule/TaskFactory.php @@ -1,12 +1,50 @@ + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ namespace phpOMS\Utils\TaskSchedule; use phpOMS\System\OperatingSystem; use phpOMS\System\SystemType; +/** + * Task factory. + * + * @category Framework + * @package phpOMS\Utils\TaskSchedule + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ final class TaskFactory { + /** + * Create task instance. + * + * @param Interval $interval Task interval + * @param string $cmd Command to run + * + * @return TaskInterface + * + * @throws \Exception + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function create(Interval $interval = null, string $cmd = '') : TaskInterface { switch (OperatingSystem::getSystem()) {