From 20b07d5959a92c4cb813170143c935ba45d0150c Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 27 Jul 2016 17:27:28 +0200 Subject: [PATCH] Test fixes --- Socket/Client/Client.php | 48 +++++++++------- Utils/Encoding/{Xor.php => XorEncoding.php} | 0 .../Types/{Function.php => MathFunction.php} | 0 Validation/Validator.php | 57 +++++-------------- Views/View.php | 56 +++++++++--------- 5 files changed, 69 insertions(+), 92 deletions(-) rename Utils/Encoding/{Xor.php => XorEncoding.php} (100%) rename Utils/Parser/LaTex/Types/{Function.php => MathFunction.php} (100%) diff --git a/Socket/Client/Client.php b/Socket/Client/Client.php index 14aae8a34..120f9bda6 100644 --- a/Socket/Client/Client.php +++ b/Socket/Client/Client.php @@ -77,33 +77,37 @@ class Client extends SocketAbstract $i = 0; while ($this->run) { - $i++; - $msg = 'disconnect'; - socket_write($this->sock, $msg, strlen($msg)); + try { + $i++; + $msg = 'disconnect'; + socket_write($this->sock, $msg, strlen($msg)); - $read = [$this->sock]; + $read = [$this->sock]; - //if(socket_select($read, $write = null, $except = null, 0) < 1) { - // error - // socket_last_error(); - // socket_strerror(socket_last_error()); - //} + //if(socket_select($read, $write = null, $except = null, 0) < 1) { + // error + // socket_last_error(); + // socket_strerror(socket_last_error()); + //} - if (count($read) > 0) { - $data = socket_read($this->sock, 1024); + if (count($read) > 0) { + $data = socket_read($this->sock, 1024); - /* Server no data */ - if ($data === false) { - continue; - } - - /* Normalize */ - $data = trim($data); - - if (!empty($data)) { - $data = explode(' ', $data); - $this->commands->trigger($data[0], 0, $data); + /* Server no data */ + if ($data === false) { + continue; + } + + /* Normalize */ + $data = trim($data); + + if (!empty($data)) { + $data = explode(' ', $data); + $this->commands->trigger($data[0], 0, $data); + } } + } catch (\Error $e) { + $this->run = false; } } diff --git a/Utils/Encoding/Xor.php b/Utils/Encoding/XorEncoding.php similarity index 100% rename from Utils/Encoding/Xor.php rename to Utils/Encoding/XorEncoding.php diff --git a/Utils/Parser/LaTex/Types/Function.php b/Utils/Parser/LaTex/Types/MathFunction.php similarity index 100% rename from Utils/Parser/LaTex/Types/Function.php rename to Utils/Parser/LaTex/Types/MathFunction.php diff --git a/Validation/Validator.php b/Validation/Validator.php index 8b5af98b2..4067e31d3 100644 --- a/Validation/Validator.php +++ b/Validation/Validator.php @@ -14,6 +14,7 @@ * @link http://orange-management.com */ namespace phpOMS\Validation; +use phpOMS\Utils\StringUtils; /** * Validator class. @@ -36,11 +37,11 @@ final class Validator extends ValidatorAbstract * @since 1.0.0 * @author Dennis Eichhorn */ - public static function isValid($var, $constraints) + public static function isValid($var, array $constraints) { foreach ($constraints as $callback => $settings) { $valid = self::$callback($var, ...$settings); - $valid = (self::endsWith($callback, 'Not') ? $valid : !$valid); + $valid = (StringUtils::endsWith($callback, 'Not') ? $valid : !$valid); if (!$valid) { return false; @@ -50,27 +51,11 @@ final class Validator extends ValidatorAbstract return true; } - /** - * String ends with ? - * - * @param string $haystack String to search in - * @param string $needle String to search for - * - * @return bool - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function endsWith($haystack, $needle) - { - return $needle === '' || strpos($haystack, $needle, strlen($haystack) - strlen($needle)) !== false; - } - /** * Validate variable by type. * - * @param mixed $var Variable to validate - * @param string[] $constraint Array of allowed types + * @param mixed $var Variable to validate + * @param string[]|string $constraint Array of allowed types * * @return bool * @@ -79,6 +64,10 @@ final class Validator extends ValidatorAbstract */ public static function isType($var, $constraint) { + if (!is_array($constraint)) { + $constraint = [$constraint]; + } + foreach ($constraint as $key => $value) { if (!is_a($var, $value)) { return false; @@ -100,7 +89,7 @@ final class Validator extends ValidatorAbstract * @since 1.0.0 * @author Dennis Eichhorn */ - public static function hasLength($var, $min = 0, $max = PHP_INT_MAX) + public static function hasLength(string $var, int $min = 0, int $max = PHP_INT_MAX) { $length = strlen($var); @@ -114,17 +103,17 @@ final class Validator extends ValidatorAbstract /** * Validate variable by substring. * - * @param string $var Variable to validate - * @param string $substr Substring + * @param string $var Variable to validate + * @param string|array $substr Substring * * @return bool * * @since 1.0.0 * @author Dennis Eichhorn */ - public static function contains($var, $substr) + public static function contains(string $var, $substr) { - return (strpos($var, $substr) !== false ? true : false); + return StringUtils::contains($var, $substr); } /** @@ -138,7 +127,7 @@ final class Validator extends ValidatorAbstract * @since 1.0.0 * @author Dennis Eichhorn */ - public static function matches($var, $pattern) + public static function matches(string $var, string $pattern) { return (preg_match($pattern, $var) !== false ? true : false); } @@ -163,20 +152,4 @@ final class Validator extends ValidatorAbstract return false; } - - /** - * String starts with ? - * - * @param string $haystack String to search in - * @param string $needle String to search for - * - * @return bool - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function startsWith($haystack, $needle) - { - return $needle === '' || strrpos($haystack, $needle, -strlen($haystack)) !== false; - } } diff --git a/Views/View.php b/Views/View.php index e142b2fa6..6c735a61f 100644 --- a/Views/View.php +++ b/Views/View.php @@ -248,34 +248,6 @@ class View implements \Serializable } } - /** - * Get view/template response. - * - * @return string - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public function render() : string - { - $path = realpath($oldPath = __DIR__ . '/../..' . $this->template . '.tpl.php'); - - if ($path === false || Validator::startsWith($path, ROOT_PATH) === false) { - throw new PathException($oldPath); - } - - ob_start(); - /** @noinspection PhpIncludeInspection */ - $data = include $path; - $ob = ob_get_clean(); - - if (is_array($data)) { - return $data; - } - - return $ob; - } - /** * @param string $id Data Id * @@ -384,6 +356,34 @@ class View implements \Serializable } } + /** + * Get view/template response. + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function render() : string + { + $path = realpath($oldPath = __DIR__ . '/../..' . $this->template . '.tpl.php'); + + if ($path === false || Validator::startsWith($path, ROOT_PATH) === false) { + throw new PathException($oldPath); + } + + ob_start(); + /** @noinspection PhpIncludeInspection */ + $data = include $path; + $ob = ob_get_clean(); + + if (is_array($data)) { + return $data; + } + + return $ob; + } + /** * Serialize view for rendering. *