Update Restler to 3.0RC6 (last bug fixes of branch v3)

This commit is contained in:
Laurent Destailleur 2019-10-08 11:14:53 +02:00
parent f9f8cf9627
commit 0cf014b01f
6 changed files with 93 additions and 49 deletions

View File

@ -137,20 +137,7 @@ with:
RESTLER: RESTLER:
-------- --------
Change content of file htdocs/includes/restler/framework/Luracast/Restler/explorer/index.html
* Add 2 lines into file AutoLoader.php to complete function
private function alias($className, $currentClass)
{
...
to get
private function alias($className, $currentClass)
{
if ($className == 'Luracast\Restler\string') return;
if ($className == 'Luracast\Restler\mixed') return;
...
Change also content of file htdocs/includes/restler/framework/Luracast/Restler/explorer/index.html
+With swagger 2: +With swagger 2:

View File

@ -263,13 +263,13 @@ class AutoLoader
* @return bool false unless className now exists * @return bool false unless className now exists
*/ */
private function loadLastResort($className, $loader = null) { private function loadLastResort($className, $loader = null) {
// @CHANGE LDR Add protection to avoid conflict with other autoloader // @CHANGE LDR Add protection to avoid conflict with other autoloader
/*print 'Try to load '.$className."\n"; /*print 'Try to load '.$className."\n";
if (in_array($className, array('Google_Client'))) if (in_array($className, array('Google_Client')))
{ {
return false; return false;
}*/ }*/
$loaders = array_unique(static::$rogueLoaders); $loaders = array_unique(static::$rogueLoaders);
if (isset($loader)) { if (isset($loader)) {
if (false === array_search($loader, $loaders)) if (false === array_search($loader, $loaders))
static::$rogueLoaders[] = $loader; static::$rogueLoaders[] = $loader;
@ -291,11 +291,20 @@ class AutoLoader
* *
* @return bool false unless className exists * @return bool false unless className exists
*/ */
private function loadThisLoader($className, $loader) { private function loadThisLoader($className, $loader)
if (is_callable($loader) {
&& false !== $file = $loader($className) if (is_array($loader)
&& $this->exists($className, $loader)) && is_callable($loader)) {
$b = new $loader[0];
if (false !== $file = $b::$loader[1]($className)
&& $this->exists($className, $b::$loader[1])) {
return $file; return $file;
}
} elseif (is_callable($loader)
&& false !== $file = $loader($className)
&& $this->exists($className, $loader)) {
return $file;
}
return false; return false;
} }
@ -307,10 +316,8 @@ class AutoLoader
*/ */
private function alias($className, $currentClass) private function alias($className, $currentClass)
{ {
// @CHANGE LDR
if ($className == 'Luracast\Restler\string') return; if ($className == 'Luracast\Restler\string') return;
if ($className == 'Luracast\Restler\mixed') return; if ($className == 'Luracast\Restler\mixed') return;
if ($className != $currentClass if ($className != $currentClass
&& false !== strpos($className, $currentClass)) && false !== strpos($className, $currentClass))
if (!class_exists($currentClass, false) if (!class_exists($currentClass, false)

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Luracast\Restler\Data; namespace Luracast\Restler\Data;
use Luracast\Restler\CommentParser; use Luracast\Restler\CommentParser;
@ -25,7 +26,8 @@ class Validator implements iValidate
public static $exceptions = array(); public static $exceptions = array();
public static $preFilters = array( public static $preFilters = array(
'*' => 'trim', //'*' => 'some_global_filter', //applied to all parameters
'string' => 'trim', //apply filter function by type (string)
//'string' => 'strip_tags', //'string' => 'strip_tags',
//'string' => 'htmlspecialchars', //'string' => 'htmlspecialchars',
//'int' => 'abs', //'int' => 'abs',
@ -59,6 +61,29 @@ class Validator implements iValidate
throw new Invalid('Expecting only alphabetic characters.'); throw new Invalid('Expecting only alphabetic characters.');
} }
/**
* Validate UUID strings.
*
* Check that given value contains only alpha numeric characters and the length is 36 chars.
*
* @param $input
* @param ValidationInfo $info
*
* @return string
*
* @throws Invalid
*/
public static function uuid($input, ValidationInfo $info = null)
{
if (is_string($input) && preg_match(
'/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i',
$input
)) {
return strtolower($input);
}
throw new Invalid('Expecting a Universally Unique IDentifier (UUID) string.');
}
/** /**
* Validate alpha numeric characters. * Validate alpha numeric characters.
* *
@ -141,7 +166,7 @@ class Validator implements iValidate
public static function color($input, ValidationInfo $info = null) public static function color($input, ValidationInfo $info = null)
{ {
if (preg_match('/^#[a-f0-9]{6}$/i', $input)) { if (preg_match('/^#[a-f0-9]{6}$/i', $input)) {
return $input; return $input;
} }
throw new Invalid('Expecting color as hexadecimal digits.'); throw new Invalid('Expecting color as hexadecimal digits.');
} }
@ -204,8 +229,9 @@ class Validator implements iValidate
public static function ip($input, ValidationInfo $info = null) public static function ip($input, ValidationInfo $info = null)
{ {
$r = filter_var($input, FILTER_VALIDATE_IP); $r = filter_var($input, FILTER_VALIDATE_IP);
if ($r) if ($r) {
return $r; return $r;
}
throw new Invalid('Expecting IP address in IPV6 or IPV4 format'); throw new Invalid('Expecting IP address in IPV6 or IPV4 format');
} }
@ -471,8 +497,7 @@ class Validator implements iValidate
} }
if (method_exists($class = get_called_class(), $info->type) && $info->type != 'validate') { if (method_exists($class = get_called_class(), $info->type) && $info->type != 'validate') {
if(!$info->required && empty($input)) if (!$info->required && empty($input)) {
{
//optional parameter with a empty value assume null //optional parameter with a empty value assume null
return null; return null;
} }
@ -524,6 +549,7 @@ class Validator implements iValidate
case 'string' : case 'string' :
case 'password' : //password fields with string case 'password' : //password fields with string
case 'search' : //search field with string case 'search' : //search field with string
if (is_bool($input)) $input = $input ? 'true' : 'false';
if (!is_string($input)) { if (!is_string($input)) {
$error .= '. Expecting alpha numeric value'; $error .= '. Expecting alpha numeric value';
break; break;
@ -555,22 +581,41 @@ class Validator implements iValidate
case 'bool': case 'bool':
case 'boolean': case 'boolean':
if ($input === 'true' || $input === true) return true; if (is_bool($input)) {
if (is_numeric($input)) return $input > 0; return $input;
return false; }
if (is_numeric($input)) {
if ($input == 1) {
return true;
}
if ($input == 0) {
return false;
}
} elseif (is_string($input)) {
switch (strtolower($input)) {
case 'true':
return true;
case 'false':
return false;
}
}
if ($info->fix) {
return $input ? true : false;
}
$error .= '. Expecting boolean value';
break;
case 'array': case 'array':
if ($info->fix && is_string($input)) { if ($info->fix && is_string($input)) {
$input = explode(CommentParser::$arrayDelimiter, $input); $input = explode(CommentParser::$arrayDelimiter, $input);
} }
if (is_array($input)) { if (is_array($input)) {
$contentType = $contentType =
Util::nestedValue($info, 'contentType') ? : null; Util::nestedValue($info, 'contentType') ?: null;
if ($info->fix) { if ($info->fix) {
if ($contentType == 'indexed') { if ($contentType == 'indexed') {
$input = $info->filterArray($input, true); $input = $info->filterArray($input, true);
} elseif ($contentType == 'associative') { } elseif ($contentType == 'associative') {
$input = $info->filterArray($input, true); $input = $info->filterArray($input, false);
} }
} elseif ( } elseif (
$contentType == 'indexed' && $contentType == 'indexed' &&
@ -609,6 +654,8 @@ class Validator implements iValidate
$name = $info->name; $name = $info->name;
$info->type = $contentType; $info->type = $contentType;
unset($info->contentType); unset($info->contentType);
unset($info->min);
unset($info->max);
foreach ($input as $key => $chinput) { foreach ($input as $key => $chinput) {
$info->name = "{$name}[$key]"; $info->name = "{$name}[$key]";
$input[$key] = static::validate($chinput, $info); $input[$key] = static::validate($chinput, $info);

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Luracast\Restler\Format; namespace Luracast\Restler\Format;
use Luracast\Restler\Data\Obj; use Luracast\Restler\Data\Obj;
@ -127,9 +128,9 @@ class JsonFormat extends Format
public function decode($data) public function decode($data)
{ {
if(empty($data)){ if (empty($data)) {
return null; return null;
} }
$options = 0; $options = 0;
if (self::$bigIntAsString) { if (self::$bigIntAsString) {
@ -147,7 +148,7 @@ class JsonFormat extends Format
} }
try { try {
$decoded = json_decode($data, $options); $decoded = json_decode($data, true, 512, $options);
$this->handleJsonError(); $this->handleJsonError();
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
throw new RestException(400, $e->getMessage()); throw new RestException(400, $e->getMessage());
@ -157,13 +158,13 @@ class JsonFormat extends Format
throw new RestException(400, 'Error parsing JSON'); throw new RestException(400, 'Error parsing JSON');
} }
return Obj::toArray($decoded); return $decoded; //Obj::toArray($decoded);
} }
/** /**
* Pretty print JSON string * Pretty print JSON string
* *
* @param string $json * @param string $json
* *
* @return string formatted json * @return string formatted json
*/ */
@ -271,7 +272,7 @@ class JsonFormat extends Format
} }
if (isset($message)) { if (isset($message)) {
throw new \RuntimeException('Error encoding/decoding JSON: '. $message); throw new \RuntimeException('Error encoding/decoding JSON: ' . $message);
} }
} }
} }

View File

@ -44,7 +44,9 @@ class Scope
//API classes //API classes
'Resources' => 'Luracast\Restler\Resources', 'Resources' => 'Luracast\Restler\Resources',
'Explorer' => 'Luracast\Restler\Explorer', 'Explorer' => 'Luracast\Restler\Explorer\v2\Explorer',
'Explorer1' => 'Luracast\Restler\Explorer\v1\Explorer',
'Explorer2' => 'Luracast\Restler\Explorer\v2\Explorer',
//Cache classes //Cache classes
'HumanReadableCache' => 'Luracast\Restler\HumanReadableCache', 'HumanReadableCache' => 'Luracast\Restler\HumanReadableCache',
@ -52,7 +54,7 @@ class Scope
'MemcacheCache' => 'Luracast\Restler\MemcacheCache', 'MemcacheCache' => 'Luracast\Restler\MemcacheCache',
//Utility classes //Utility classes
'Obj' => 'Luracast\Restler\Data\Obj', 'Object' => 'Luracast\Restler\Data\Obj',
'Text' => 'Luracast\Restler\Data\Text', 'Text' => 'Luracast\Restler\Data\Text',
'Arr' => 'Luracast\Restler\Data\Arr', 'Arr' => 'Luracast\Restler\Data\Arr',

View File

@ -230,7 +230,7 @@ class Util
if (! is_string($className)) return ''; if (! is_string($className)) return '';
//var_dump($className); //var_dump($className);
$className = explode('\\', $className); $className = explode('\\', $className);
return end($className); return end($className);
} }
} }