Test fixes

This commit is contained in:
Dennis Eichhorn 2016-07-27 17:27:28 +02:00
parent 35ffdf666a
commit 20b07d5959
5 changed files with 69 additions and 92 deletions

View File

@ -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;
}
}

View File

@ -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 <d.eichhorn@oms.com>
*/
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 <d.eichhorn@oms.com>
*/
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 <d.eichhorn@oms.com>
*/
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 <d.eichhorn@oms.com>
*/
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 <d.eichhorn@oms.com>
*/
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 <d.eichhorn@oms.com>
*/
public static function startsWith($haystack, $needle)
{
return $needle === '' || strrpos($haystack, $needle, -strlen($haystack)) !== false;
}
}

View File

@ -248,34 +248,6 @@ class View implements \Serializable
}
}
/**
* Get view/template response.
*
* @return string
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
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 <d.eichhorn@oms.com>
*/
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.
*