compared with
Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (67)

View Page History

{zone-data:skeletons}
{code}class Zend_Soap_SimpleTypeValuesValidator {
/*
* Keep memory of children class name for
* easy use.
*/
private $_className;
/*
* Result of reflection on children class name
* is stored here. We do no wish to compute
* reflection each time.
*/
private $_classReflection;
/*
* List of check to do for a final type
*/
private $_checkMap;
/*
* Memory of is simple type a restriction,
* union or list.
*/
private $_simpleTypeBase = null;
/*
* List of all uniq instance of children
* already instanciated.
*/
private static $_uniqInstance;
/*
* Keep memory of children class name for
* easy use.
*/
private $_className;
/**
* Function to call to launch the test on value
*
* @param mixed $value
* @return bool true on success
* @throws SoapFault on error
*/
public function test($value) {
/*
* Result of reflection on children class name
* is stored here. We do no wish to compute
* reflection each time.
*/
private $_classReflection;
if($this->_simpleTypeBase === null) {
if (preg_match_all('/@xsd\s+([^\s]+)([ \t\f]+(.*))?/m', $this->_classReflection->getDocComment(), $firstMatches)) {
foreach ($firstMatches[0] as $firstKey => $someValue) {
switch ($firstMatches[1][$firstKey]) {
case 'restriction' :
$this->_simpleTypeBase = 'restriction';
//$firstMatches[2][$firstKey] = substr($firstMatches[2][$firstKey], 0, -1);
//TODO: have to think about how it could be implemented to match value
// of restriction against another simpletype in base property
//$this->_addAttributes($firstMatches[2][$firstKey], $all);
foreach ($this->_classReflection->getProperties() as $property) {
if ($property->isPublic()
&& preg_match_all('/@xsd\s+([^\s]+)([ \t\f]+(.*))?/m', $property->getDocComment(), $matches)) {
$this->_compileCheckMap($matches);
}
}
break;
case 'union' :
$this->_simpleTypeBase = 'union';
//$firstMatches[2][$firstKey] = substr($firstMatches[2][$firstKey], 0, -1);
foreach ($this->_classReflection->getProperties() as $property) {
if ($property->isPublic()
&& preg_match_all('/@xsd\s+simpleType([ \t\f]+(.*))?/m', $property->getDocComment(), $matches)) {
foreach ($matches[0] as $key => $someValue) {
$matches[1][$key] = substr($matches[1][$key], 0, -1);
if (class_exists($matches[1][$key])) {
$this->_checkMap[0][] = 'simpleType';
if (is_callable($matches[1][$key] . '::getInstance')) {
$this->_checkMap[1][] = $matches[1][$key];
} else {
throw new Exception($matches[1][$key] . ' does not extend SimpleTypeValuesValidator', 500);
}
} else {
$this->_checkMap[0][] = 'baseType';
$this->_checkMap[1][] = '';
}
}
}
}
break;
case 'list' :
$this->_simpleTypeBase = 'list';
//$firstMatches[2][$firstKey] = substr($firstMatches[2][$firstKey], 0, -1);
foreach ($this->_classReflection->getProperties() as $property) {
if ($property->isPublic()
&& preg_match_all('/@xsd\s+itemType([ \t\f]+(.*))?/m', $property->getDocComment(), $matches)) {
$this->_checkMap[0][] = 'itemType';
foreach ($matches[0] as $key => $someValue) {
$matches[1][$key] = substr($matches[1][$key], 0, -1);
if (class_exists($matches[1][$key])) {
if (is_callable($matches[1][$key] . '::getInstance')) {
$this->_checkMap[1][] = $matches[1][$key];
} else {
throw new Exception($matches[1][$key] . ' does not extend SimpleTypeValuesValidator', 500);
}
} else {
throw new Exception($matches[1][$key] . ' does not exists as class', 500);
}
}
}
}
break;
}
}
}
}
/*
* List of check to do for a final type
*/
private $_checkMap;
switch ($this->_simpleTypeBase) {
case 'restriction' :
return self::_checkValues($this->_checkMap[0], $this->_checkMap[1], $value);
break;
case 'union' :
foreach ($this->_checkMap[0] as $key => $simpeTypeElement) {
if ($simpeTypeElement == 'simpleType') {
$className = $this->_checkMap[1][$key];
$result = $className::getInstance()->test($value);
if ($result === true)
return true;
} else {
if(self::checkBaseType($matches[1][$key], $value))
return true;
}
}
return false;
break;
case 'list' :
foreach ($this->_checkMap[0] as $key => $simpeTypeElement) {
foreach ($this->_checkMap[1] as $listKey => $listType) {
if (preg_match_all('/[^\s]+/m', $value, $listMatches)) {
foreach ($listMatches[0] as $listValueKey => $listValue) {
$result = $listType::getInstance()->test($listValue);
if ($result !== true)
return false;
}
return true;
}
}
}
return false;
break;
}
/*
* Memory of is simple type a restriction,
* union or list.
*/
private $_simpleTypeBase = null;
return false;
}
/*
* List of all uniq instance of children
* already instanciated.
*/
private static $_uniqInstance;
/**
* Function to get the wanted children instance.
*
* To get it use children_class_name::getInstance()
* To launch a test: children_class_name::getInstance()->test(some_value)
* Chilren must extends SimpleTypeValuesValidator class.
*
* @return object
*/
public static function getInstance() {
$className = get_called_class();
if (!isset(self::$_uniqInstance[$className])) {
self::$_uniqInstance[$className] = new $className;
self::$_uniqInstance[$className]->_className = $className;
self::$_uniqInstance[$className]->_classReflection = new ReflectionClass($className);
}
return self::$_uniqInstance[$className];
}

/**
* Function to prepare the map to execute all needed check.
*
* @param array $matches
*/
protected function _compileCheckMap($matches) {
/**
* Function to call to launch the test on value
*
* @param mixed $value
* @return bool true on success
* @throws SoapFault on error
*/
public function test($value) {
$this->_checkMap[0] = $matches[1];
if($this->_simpleTypeBase === null) {
if (preg_match_all('/@xsd\s+([^\s]+)\s(.*)/m', $this->_classReflection->getDocComment(), $firstMatches)) {
foreach ($firstMatches[0] as $firstKey => $someValue) {
switch ($firstMatches[1][$firstKey]) {
foreach ($matches[0] as $key => $someValue) {
$matches[2][$key] = substr($matches[2][$key], 0, -1);
eval('$this->_checkMap[1][$key] = ' . $matches[2][$key] . ';');
}
}

/**
* Function to check all final value against declared type.
*
* @param array $matches
* @param array $matchesArgs
* @param mixed $value
*/
protected static function _checkValues($matches, $matchesArgs, $value) {
foreach ($matches as $key => $checkType) {
switch ($checkType) {
case 'minExclusive' : return true;
if ($value >= $matchesArgs[$key]['value'])
return false;
case 'restriction' : break;
case 'minInclusive' :
if ($value > $matchesArgs[$key]['value'])
return false;
$this->_simpleTypeBase = 'restriction'; break;
//$firstMatches[2][$firstKey] = substr($firstMatches[2][$firstKey], 0, -1);
//TODO: have to think about how it could be implemented to match value
case 'maxExclusive' :
if ($value <= $matchesArgs[$key]['value'])
// of restriction against another simpletype in base property return false;
break;
case 'maxInclusive' :
if ($value < $matchesArgs[$key]['value'])
//$this->_addAttributes($firstMatches[2][$firstKey], $all); return false;
break;
case 'totalDigits' :
if (strlen($value) != $matchesArgs[$key]['value'])
foreach ($this->_classReflection->getProperties() as $property) { return false;
break;
case 'fractionDigits' :
if (strlen(strchr('.', $value))-1 != $matchesArgs[$key]['value'])
return false; if ($property->isPublic()
&& preg_match_all('/@xsd\s+([^\s]+)\s(.*)/m', $property->getDocComment(), $matches)) {
break;
case 'length' :
if (strlen($value) != $matchesArgs[$key]['value'])
return false; $this->_compileCheckMap($matches);
break;
case 'minLength' :
if (strlen($value) < $matchesArgs[$key]['value'])
return false; }
break;
case 'maxLength' :
if (strlen($value) > $matchesArgs[$key]['value'])
} return false;
break;
case 'enumeration' :
if ($matchesArgs[$key]['value'] == $value)
return true;
case 'union' : break;
case 'whiteSpace' :
// dixit W3C documentation, no validation rules for that.
$this->_simpleTypeBase = 'union'; return true;
//$firstMatches[2][$firstKey] = substr($firstMatches[2][$firstKey], 0, -1);
break;
case 'pattern' :
if (!preg_match("/" . $matchesArgs[$key]['value'] . "/", $value))
foreach ($this->_classReflection->getProperties() as $property) { return false;
if ($property->isPublic()
&& preg_match_all('/@xsd\s+simpleType\s(.*)/m', $property->getDocComment(), $matches)) {
foreach ($matches[0] as $key => $someValue) {
$matches[1][$key] = substr($matches[1][$key], 0, -1);
if (class_exists($matches[1][$key])) {
$this->_checkMap[0][] = 'simpleType';
if (is_callable($matches[1][$key] . '::getInstance')) {
$this->_checkMap[1][] = $matches[1][$key];
} else {
throw new Exception($matches[1][$key] . ' does not extend SimpleTypeValuesValidator', 500);
}
} else {
$this->_checkMap[0][] = 'baseType';
$this->_checkMap[1][] = '';
}
}
break; }
}
break;
case 'list' default :
throw new Exception($checkType . ' unknown SimpleTypeValuesValidator', 500);
}
}
if ($checkType == 'enumeration')
return false;
else
return true;
}

/**
* Function to check if a value is part of
* base type of any XSD
*
* @param string $baseType
* @param mixed $value
*/
public static function checkBaseType($baseType, $value) {
switch ($baseType) {
case 'string':
case 'str':
return is_string($value); $this->_simpleTypeBase = 'list';
//$firstMatches[2][$firstKey] = substr($firstMatches[2][$firstKey], 0, -1);
foreach ($this->_classReflection->getProperties() as $property) {
if ($property->isPublic()
&& preg_match_all('/@xsd\s+itemType\s(.*)/m', $property->getDocComment(), $matches)) {
$this->_checkMap[0][] = 'itemType';
foreach ($matches[0] as $key => $someValue) {
$matches[1][$key] = substr($matches[1][$key], 0, -1);
if (class_exists($matches[1][$key])) {
if (is_callable($matches[1][$key] . '::getInstance')) {
$this->_checkMap[1][] = $matches[1][$key];
} else {
throw new Exception($matches[1][$key] . ' does not extend SimpleTypeValuesValidator', 500);
}
} else {
throw new Exception($matches[1][$key] . ' does not exists as class', 500);
}
}
}
}
break;
case 'int':
case 'integer':
} return is_int($value);
case 'float':
case 'double':
return is_float($value);
case 'boolean':
case 'bool':
return is_bool($value);
case 'array':
return is_array($value);
case 'object':
return is_object($value);
case 'mixed':
return true;
case 'void':
return empty($value);
} default:
throw new Exception($baseType . ' unknown SimpleTypeValuesValidator BaseType', 500);
}
}
} }

switch ($this->_simpleTypeBase) {
case 'restriction' :
return self::_checkValues($this->_checkMap[0], $this->_checkMap[1], $value);
break;
case 'union' :
foreach ($this->_checkMap[0] as $key => $simpeTypeElement) {
if ($simpeTypeElement == 'simpleType') {
$className = $this->_checkMap[1][$key];
$result = $className::getInstance()->test($value);
if ($result === true)
return true;
} else {
if(self::checkBaseType($matches[1][$key], $value))
return true;
}
}
return false;
break;
case 'list' :
foreach ($this->_checkMap[0] as $key => $simpeTypeElement) {
foreach ($this->_checkMap[1] as $listKey => $listType) {
if (preg_match_all('/[^\s]+/m', $value, $listMatches)) {
foreach ($listMatches[0] as $listValueKey => $listValue) {
$result = $listType::getInstance()->test($listValue);
if ($result !== true)
return false;
}
return true;
}
}
}
return false;
break;
}

return false;
}

/**
* Function to get the wanted children instance.
*
* To get it use children_class_name::getInstance()
* To launch a test: children_class_name::getInstance()->test(some_value)
* Chilren must extends SimpleTypeValuesValidator class.
*
* @return object
*/
public static function getInstance() {
$className = get_called_class();
if (!isset(self::$_uniqInstance[$className])) {
self::$_uniqInstance[$className] = new $className;
self::$_uniqInstance[$className]->_className = $className;
self::$_uniqInstance[$className]->_classReflection = new ReflectionClass($className);
}
return self::$_uniqInstance[$className];
}

/**
* Function to prepare the map to execute all needed check.
*
* @param array $matches
*/
protected function _compileCheckMap($matches) {

$this->_checkMap[0] = $matches[1];
foreach ($matches[0] as $key => $someValue) {
$matches[2][$key] = substr($matches[2][$key], 0, -1);
eval('$this->_checkMap[1][$key] = ' . $matches[2][$key] . ';');
}
}

/**
* Function to check all final value against declared type.
*
* @param array $matches
* @param array $matchesArgs
* @param mixed $value
*/
protected static function _checkValues($matches, $matchesArgs, $value) {
foreach ($matches as $key => $checkType) {
switch ($checkType) {
case 'minExclusive' : return true;
if ($value >= $matchesArgs[$key]['value'])
return false;
break;
case 'minInclusive' :
if ($value > $matchesArgs[$key]['value'])
return false;
break;
case 'maxExclusive' :
if ($value <= $matchesArgs[$key]['value'])
return false;
break;
case 'maxInclusive' :
if ($value < $matchesArgs[$key]['value'])
return false;
break;
case 'totalDigits' :
if (strlen($value) != $matchesArgs[$key]['value'])
return false;
break;
case 'fractionDigits' :
if (strlen(strchr('.', $value))-1 != $matchesArgs[$key]['value'])
return false;
break;
case 'length' :
if (strlen($value) != $matchesArgs[$key]['value'])
return false;
break;
case 'minLength' :
if (strlen($value) < $matchesArgs[$key]['value'])
return false;
break;
case 'maxLength' :
if (strlen($value) > $matchesArgs[$key]['value'])
return false;
break;
case 'enumeration' :
if ($matchesArgs[$key]['value'] == $value)
return true;
break;
case 'whiteSpace' :
// dixit W3C documentation, no validation rules for that.
return true;
break;
case 'pattern' :
if (!preg_match("/" . $matchesArgs[$key]['value'] . "/", $value))
return false;
break;
default :
throw new Exception($checkType . ' unknown SimpleTypeValuesValidator', 500);
}
}
if ($checkType == 'enumeration')
return false;
else
return true;
}

/**
* Function to check if a value is part of
* base type of any XSD
*
* @param string $baseType
* @param mixed $value
*/
public static function checkBaseType($baseType, $value) {
switch ($baseType) {
case 'string':
case 'str':
return is_string($value);
case 'int':
case 'integer':
return is_int($value);
case 'float':
case 'double':
return is_float($value);
case 'boolean':
case 'bool':
return is_bool($value);
case 'array':
return is_array($value);
case 'object':
return is_object($value);
case 'mixed':
return true;
case 'void':
return empty($value);
default:
throw new Exception($baseType . ' unknown SimpleTypeValuesValidator BaseType', 500);
}
}
}
{code}
{zone-data}