Handle base64 and datetime values

@param   mixed    $value  The referenced value
@param   string   $type   The type of value

@return  \Comodojo\Xmlrpc\XmlrpcEncoder
324704
final public function setValueType(&$value, $type) {

        if ( empty($value) || !in_array(strtolower($type), array("base64", "datetime", "cdata")) ) throw new XmlrpcException("Invalid value type");

        $this->special_types[$value] = strtolower($type);

        return $this;

    }
76562
private function filters($value, $type = null, $safe = true) {
            if ($type == "date") {
                $ret = $this->convertDate($value, false);
                return $ret;
            }
            if ($type == "datetime-local") {
                return $ret = strtotime($value);
            }
            if ($type == "date_string") {
                return date("Y-m-d", \AsyncWeb\Date\Time::getUnix($this->convertDate($value, false)));
            }
            if ($type == "number") {
                $value = str_replace(" ", "", $value);
                $value = str_replace(",", ".", $value);
            }
            if (!$safe) {
                if (($val = $this->execute($value)) !== false) {
                    return $val;
                }
            }
            return $value;
        }
1153308
protected function castParameter($value, $type = null) {
		//check null value
		if (is_null($value))
			return 'NULL';
		elseif (is_null($type)) {
			//obtain default type handler
			$typeHandler = $this->getDefaultTypeHandler($value);
			if (is_null($typeHandler))
				return 'NULL';
			if (is_array($value))
				return $this->castArray($value, $typeHandler, '_');
		}
		else {
			//cast value to the specified type
			$typeHandler = $this->typeManager->getTypeHandler($type);
				
			if ($typeHandler === false)
				throw new \RuntimeException("No type handler found for type '$type'");
				
			if (is_array($value))
				return $this->castArray($value, $typeHandler, '_');
			
			//check if returned value is null
			$value = $typeHandler->castParameter($value);
			if (is_null($value))
				return 'NULL';
		}
	
		//get parameter expression
		$value = $typeHandler->setParameter($value);
	
		//check null value
		if (is_null($value))
			return 'NULL';
		//cast to string
		if (!is_string($value))
			$value = strval($value);
		return $value;
	}
850700
public static function encodeForMaria($value)
    {
        return is_string($value)
        && (!mb_check_encoding($value, 'UTF-8') || strpos($value, self::DATA_URI_PREFIX) === 0)
            ? self::DATA_URI_PREFIX . base64_encode($value)
            : $value;
    }
814182
protected function cast($value = null, int $type = Variables::TYPE_STRING)
    {
        if ($value === null) {
            return $this->getDefault($type);
        }
        switch ($type) {
            case Variables::TYPE_INT:
                $value = @\intval($value);
                break;
            case Variables::TYPE_FLOAT:
                $value = @\floatval($value);
                break;
            case Variables::TYPE_STRING:
                $value = @\strval($value);
                break;
            case Variables::TYPE_ARRAY:
                $value = (array)$value;
                break;
            case Variables::TYPE_JSON_DECODED:
                $value = @\json_decode(@\strval($value), true, 512) ?: '';
                break;
            case Variables::TYPE_AUTO:
                break;
            default:
                throw new DomainException('Invalid variable type specified');
                break;
        }
        return $value;
    }
1270191
private function checkPartMatchingType($value, $type)
    {
        switch ($type) {
            case "digit":
                return ctype_digit($value);
                break;

            case "hex":
                return ctype_xdigit($value);
                break;

            case "alpha":
                return ctype_alpha($value);
                break;

            case "alphanum":
                return ctype_alnum($value);
                break;

            default:
                return false;
        }
    }
725600
public function formatTemporalValue($value, $type)
    {
        /** @var $dt PropelDateTime */
        if ($dt = PropelDateTime::newInstance($value)) {
            if ($type instanceof ColumnMap) {
                $type = $type->getType();
            }
            switch ($type) {
                case PropelColumnTypes::TIMESTAMP:
                case PropelColumnTypes::BU_TIMESTAMP:
                    $value = $dt->format($this->getTimestampFormatter());
                    break;
                case PropelColumnTypes::DATE:
                case PropelColumnTypes::BU_DATE:
                    $value = $dt->format($this->getDateFormatter());
                    break;
                case PropelColumnTypes::TIME:
                    $value = $dt->format($this->getTimeFormatter());
                    break;
            }
        }

        return $value;
    }
180446
private function cast($value, $type)
    {
        switch ($type) {
            case 'bool':
                return (bool)$value;
            case 'float':
                return (float)$value;
            case 'int':
                return (int)$value;
            default:
                return $value;
        }
    }
436378
public function deserialize($value, Schema $schema): \DateTime
    {
        if ($schema instanceof ScalarSchema && $schema->hasFormat(Schema::FORMAT_DATE)) {
            if (false === $result = \DateTime::createFromFormat('Y-m-d H:i:s', "$value 00:00:00")) {
                throw new DateTimeNotParsableException(
                    sprintf("'%s' not parsable in YYYY-MM-DD format", $value)
                );
            }

            return $result;
        }

        foreach ($this->inputDateTimeFormats as $format) {
            if (false !== $result = \DateTime::createFromFormat($format, $value)) {
                return $result;
            }
        }

        throw new DateTimeNotParsableException(
            sprintf("Datetime '%s' not parsable as one of '%s'", $value, implode(', ', $this->inputDateTimeFormats))
        );
    }
136131
protected function convertType($value, $type){
		$dateFormat = $this->connection['result']['formatDate'];
		if($value === null)
			return null;
		switch($type){
			case Manager::TEXT:
				return (string) $value;

			case Manager::INT:
				return (int) $value;

			case Manager::FLOAT:
				return (float) $value;

			case Manager::BOOL:
				return ((bool) $value) && $value !== 'f' && $value !== 'F';

			case Manager::BINARY:
				return $this->connection->getDriver()->unescape($value, $type);

			case Manager::DATETIME:
				if((int) $value === 0)
					return null;
				elseif(!$dateFormat)
					return new DateTime(is_numeric($value) ? date('Y-m-d H:i:s', $value) : $value);
				elseif($dateFormat == 'U')
					return is_numeric($value) ? (int) $value : strtotime($value);
				elseif(is_numeric($value))
					return date($dateFormat, $value);
				else{
					$d = new DateTime($value);
					return $d->format($dateFormat);
				}

			default:
				return $value;
		}
	}
187225
protected function castToType($value, string $type)
    {
        $platform = $this->connection->getDatabasePlatform();

        return Type::getType($type)->convertToDatabaseValue($value, $platform);
    }
338546
public function escape($value, $type){
		switch($type){
			case Manager::BOOL:
				return $value ? 1 : 0;

			case Manager::TEXT:
			case Manager::BINARY:
				return "'" . sqlite_escape_string($value) . "'";

			case Manager::IDENTIFIER:
				return str_replace('[*]', '*', '[' . str_replace('.', '].[', $value) . ']');

			case Manager::DATETIME:
				return ($value instanceof DateTime) ? $value->format("'Y-m-d H:i:s'") : date("'Y-m-d H:i:s'", $value);

			default:
				throw new InvalidArgumentException('Unsupported data type.');
				break;
		}
	}
1123593
public function convertValue($value, $type) {
		if (is_array($value)) {
			$newReturn = array();
			foreach ($value as $v) {
				$newReturn[] = $this->convertValue($v, $type);
			}
			return $newReturn;
		} else {
			switch ($type) {
				case 'Date':
				case 'SS_Datetime': {
						// we don't want a complete iso8601 date, we want it 
						// in UTC time with a Z at the end. It's okay, php's
						// strtotime will correctly re-convert this to the correct
						// timestamp, but this is how Solr wants things.
						// If we don't have a full DateTime stamp we won't remove any hours
                        $hoursToRemove = date('Z');
						$ts = strtotime($value);
						$tsHTR = $ts - $hoursToRemove;
						$date = date('Y-m-d\TH:i:s\Z', $tsHTR);
						return $date;
				}
				case 'HTMLText': {
						return strip_tags($value);
				}
				case 'SolrGeoPoint': {
					return $value->y . ',' . $value->x;
				}
				default: {
					return $value;
				}
			}
		}
	}
543314
protected function encodeDateTime(DateTime $value = null) {
        return null === $value ? "" : $value->format(self::DATETIME_FORMAT);
    }
357424
protected function readTransform($value, $type)
    {
        if (is_null($value)) {
            return;
        }

        if (is_array($type)) {
            list($type, $param) = $type;
        } elseif (strpos($type, ':')) {
            list($type, $param) = explode(':', $type, 2);
        }

        switch ($type) {
            case 'integer':
                $value = (int) $value;
                break;
            case 'float':
                if (empty($param)) {
                    $value = (float) $value;
                } else {
                    $value = (float) number_format($value, $param, '.', '');
                }
                break;
            case 'boolean':
                $value = (bool) $value;
                break;
            case 'timestamp':
                if (!is_null($value)) {
                    $format = !empty($param) ? $param : $this->dateFormat;
                    $value  = $this->formatDateTime($format, $value, true);
                }
                break;
            case 'datetime':
                if (!is_null($value)) {
                    $format = !empty($param) ? $param : $this->dateFormat;
                    $value  = $this->formatDateTime($format, $value);
                }
                break;
            case 'json':
                $value = json_decode($value, true);
                break;
            case 'array':
                $value = empty($value) ? [] : json_decode($value, true);
                break;
            case 'object':
                $value = empty($value) ? new \stdClass() : json_decode($value);
                break;
            case 'serialize':
                $value = unserialize($value);
                break;
            default:
                if (false !== strpos($type, '\\')) {
                    // 对象类型
                    $value = new $type($value);
                }
        }

        return $value;
    }
809950
protected function Sub( $value )
	{
	    switch ( $value->GetValueCode() )
	    {
	        case DateProxyFactory::Code:
	            return new DayTimeDurationProxy( DateValue::Sub( $this->_value, /* DateValue */ $value->getValue() ) );

	        case YearMonthDurationProxyFactory::Code:
	        	/**
	        	 * @var YearMonthDurationProxy $value
	        	 */
	        	// Clone the DateValue contained in the proxy or the original is changed
	        	$clone = new DateValue( $this->_value->S, clone $this->_value->Value );
	        	$value->getValue()->Value->invert = ! $value->getValue()->Value->invert;
	        	return new DateProxy( DateValue::AddYearMonthDuration( $clone, $value->getValue() ) );

	        case DayTimeDurationProxyFactory::Code:
	        	/**
	        	 * @var DayTimeDurationProxy $value
	        	 */
	        	// Clone the DateValue contained in the proxy or the original is changed
	        	$clone = new DateValue( $this->_value->S, clone $this->_value->Value );
	        	$value->getValue()->Value->invert = ! $value->getValue()->Value->invert;
	        	return new DateProxy( DateValue::AddDayTimeDuration( $clone, $value->getValue() ) );

	        default:
	            throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::BinaryOperatorNotDefined,
					array(
						"op:sub",
						SequenceType::WithTypeCodeAndCardinality( SequenceType::GetXmlTypeCodeFromObject( $this->_value ), XmlTypeCardinality::One ),
						SequenceType::WithTypeCodeAndCardinality( SequenceType::GetXmlTypeCodeFromObject( $value->getValue() ), XmlTypeCardinality::One )
					)
				);
	    }
	}
1175764
public function encode($value)
	{
		$type = gettype($value);
		if (!isset($this->typeEncoders[$type]))
		{
			throw new RuntimeException('Cannot encode ' . $type . ' value');
		}

		return $this->typeEncoders[$type]($value);
	}