@param string      $value
@param Node|string $datatype (optional)
@param string      $lang     (optional)

@return Literal
770989
public function createLiteral($value, $datatype = null, $lang = null)
    {
        if ($datatype !== null) {
            if (!$datatype instanceof Node) {
                $datatype = $this->createNamedNode($datatype);
            } elseif (!$datatype->isNamed()) {
                throw new \Exception('Argument datatype has to be a named node.');
            }
        }

        return new LiteralImpl($value, $datatype, $lang);
    }
450922
public static function filterLanguage($value)
    {
        $uri = self::getExistingLanguageUri($value);
        /** @noinspection NullPointerExceptionInspection */
        return $uri !== null
            ? $uri
            : self::singleton()->getLanguageByCode(DEFAULT_LANG)->getUri();
    }
333065
public static function castFromString($type, $value, $php_type)
    {
        //Here should maybe handle locale specific tz overrides in the future.
        $timezone = null;

        switch ($type) {

            case self::PROPERTY_TYPE_INT:
                return (int) $value;

            case self::PROPERTY_TYPE_FLOAT:
                return (float) $value;

            case self::PROPERTY_TYPE_BOOLEAN:
                return in_array(strtolower($value), ['true', '1', 'yes'], true);

            /** @noinspection PhpMissingBreakStatementInspection */
            case self::PROPERTY_TYPE_TIMESTAMP:
                $timezone = new \DateTimeZone('UTC');

                // no break
            case self::PROPERTY_TYPE_DATE:
                if (preg_match('/Date\\((?<timestamp>[0-9\\+\\.]+)\\)/', $value, $matches)) { //to catch stupid .net date serialisation
                    $value = $matches['timestamp'];
                }

                return new \DateTime($value, $timezone);

            case self::PROPERTY_TYPE_OBJECT:
                $php_type = sprintf('\\XeroPHP\\Models\\%s', $php_type);
                /** @var self $instance */
                $instance = new $php_type();
                $instance->fromStringArray($value);

                return $instance;

            default:
                if (is_scalar($value)) {
                    return (string) $value;
                }

                return (object) $value;
        }
    }
427193
private function bindDataType($value)
    {
        switch (true) {
            case is_string($value):
                return PDO::PARAM_STR;
            case is_int($value):
                return PDO::PARAM_INT;
            case is_bool($value):
                return PDO::PARAM_BOOL;
            case is_null($value):
                return PDO::PARAM_NULL;
            default:
                return false;
        }
    }
563210
protected function selfFormat(XType $type, $value, $view, $lang)
    {

        if ($type instanceof BoolType) {
            return $this->formatBool($type, $value, $view, $lang);
        }

        if ($type instanceof UnitType) {
            return $this->formatUnit($type, $value, $view, $lang);
        }

        if ($type instanceof NumberType) {
            return $this->formatNumber($type, $value, $view, $lang);
        }

        if ($type instanceof StringType) {
            return $this->formatString($type, $value, $view, $lang);
        }

        if ($type instanceof TemporalType) {
            return $this->formatTemporal($type, $value, $view, $lang);
        }

        return 'NaN';
    }
340508
protected function resolveFieldType(string $value, Fluent $data): FieldContract
    {
        if (\preg_match('/^(input):([a-zA-Z]+)$/', $value, $matches)) {
            $value = $matches[2];
        }

        $filterable = \in_array($value, \array_keys($this->templates)) || \method_exists($this->presenter, $value);

        if ((bool) $filterable) {
            $data->method($value);
        } else {
            $data->method('input')->type($value ?: 'text');
        }

        return $data;
    }
249115
public function toPlatformValue($value)
    {
        if (!is_array($this->getExtra()) || !in_array($value, $this->getExtra())) {
            throw new BadArgumentException(sprintf('Enumeration type error. Allowed only (%s)', implode(', ', $this->getExtra())));
        }
        
        return parent::toPlatformValue($value);
    }
873849
public function validate($value) {
        if(!is_string($value)) {
            throw new DataType('value', 'string', $value);
        }
        
        if(!password_verify($value, $this->hash)) {
            return new ValidationResult(false, 'Wrong password');
        }        
        return new ValidationResult(true);
    }
114796
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
    {
        if ($value === null) {
            return null;
        }

        return (string) $value;
    }
223448
protected function resolveFieldType($value, Fluent $data)
    {

        $filterable = array_keys($this->templates);
        $value      = ($value == 'switch') ? 'switch_field' : $value;

        if (preg_match('/^(input):([a-zA-Z]+)$/', $value, $matches)) {
            $value = $matches[2];
        } elseif (preg_match('/^(button):([a-zA-Z]+)$/', $value, $matches)) {
            $value        = $matches[1];
            $data['type'] = $matches[2];
        } elseif (!in_array($value, $filterable)) {
            $value = 'text';
        }
        if (in_array($value, $filterable)) {
            $data->method($value);
        } else {
            $data->method('input')->type($value);
        }

        return $data;
    }
1338860
public function setValue($value, $data = null)
    {
        $value = $this->preventHtmlParserError($value);
        return parent::setValue($value, $data);
    }
1063920
public function cast($value, $connection)
	{
		if ($value === null)
			return null;

		switch ($this->type)
		{
			case self::STRING:	return (string)$value;
			case self::INTEGER:	return (int)$value;
			case self::DECIMAL:	return (double)$value;
			case self::DATETIME:
			case self::DATE:
				if (!$value)
					return null;

				if ($value instanceof DateTime)
					return $value;

				if ($value instanceof \DateTime)
					return new DateTime($value->format('Y-m-d H:i:s T'));

				return $connection->stringToDateTime($value);
		}
		return $value;
	}
329615
static function decodeDataTypes( $node, $type = "" )
    {
        $returnValue = false;

        $attributeValue = '';
        $attribute = $node->getAttributeNodeNS( eZSOAPEnvelope::SCHEMA_INSTANCE, 'type' );
        if ( !$attribute )
        {
            $attribute = $node->getAttributeNodeNS( 'http://www.w3.org/1999/XMLSchema-instance', 'type' );
        }
        $attributeValue = $attribute->value;

        $dataType = $type;
        $attrParts = explode( ":", $attributeValue );
        if ( $attrParts[1] )
        {
            $dataType = $attrParts[1];
        }

/*
        $typeNamespacePrefix = $this->DOMDocument->namespaceByAlias( $attrParts[0] );

        check that this is a namespace type definition
                if ( ( $typeNamespacePrefix == eZSOAPEnvelope::SCHEMA_DATA ) ||
                     ( $typeNamespacePrefix == eZSOAPEnvelope::ENC )
                     )
TODO: add encoding checks with schema validation.
*/

        switch ( $dataType )
        {
            case "string" :
            case "int" :
            case "float" :
            case 'double' :
            {
                $returnValue = $node->textContent;
            } break;

            case "boolean" :
            {
                if ( $node->textContent == "true" )
                    $returnValue = true;
                else
                    $returnValue = false;
            } break;

            case "base64" :
            {
                $returnValue = base64_decode( $node->textContent );
            } break;

            case "Array" :
            {
                // Get array type
                $arrayType = $node->getAttributeNodeNS( eZSOAPEnvelope::ENC, 'arrayType' )->value;
                $arrayTypeParts = explode( ":", $arrayType );

                preg_match( "#(.*)\[(.*)\]#",  $arrayTypeParts[1], $matches );

                $type = $matches[1];
                $count = $matches[2];

                $returnValue = array();
                foreach( $node->childNodes as $child )
                {
                    if ( $child instanceof DOMElement )
                    {
                        $returnValue[] = eZSOAPResponse::decodeDataTypes( $child, $type );
                    }
                }
            }break;

            case "SOAPStruct" :
            {
                $returnValue = array();

                foreach( $node->childNodes as $child )
                {
                    if ( $child instanceof DOMElement )
                    {
                        $returnValue[$child->tagName] = eZSOAPResponse::decodeDataTypes( $child );
                    }
                }
            }break;

            default:
            {
                foreach ( $node->childNodes as $childNode )
                {
                    if ( $childNode instanceof DOMElement )
                    {
                        // check data type for child
                        $attr = $childNode->getAttributeNodeNS( eZSOAPEnvelope::SCHEMA_INSTANCE, 'type' )->value;

                        $dataType = false;
                        $attrParts = explode( ":", $attr );
                        $dataType = $attrParts[1];

                        $returnValue[$childNode->tagName] = eZSOAPResponse::decodeDataTypes( $childNode );
                    }
                }

            } break;
        }

        return $returnValue;
    }
5746
public function bindValue($name, $value, $dataType = \PDO::PARAM_STR)
    {
        $this->binds[$name] = $value;
        return $this;
    }
1002389
protected function elt($value, $rule, $data)
    {
        $val = $this->getDataValue($data, $rule);

        return !is_null($val) && $value <= $val;
    }
936655
function resolveLanguage ( value ) {
	if ( typeof value === 'string' ) {
		const lang = _.find(langs, ( o ) => { return o.locale.indexOf(value) !== -1; });
		if ( typeof lang !== 'undefined' ) {
			return lang.id;
		}
	}
	return value;
}
735144
public static function ChangeType( $xmlType, $value, $type, $nameTable, $nsmgr )
	{
		if ( $type->TypeCode == XmlTypeCode::AnyAtomicType || $xmlType->TypeCode == $type->TypeCode )
		{
			switch ( $xmlType->TypeCode )
			{
				case XmlTypeCode::Decimal:
					$value = $value instanceof XPath2Item ? $value->getValue() : $value;
					if ( $value instanceof DecimalProxy )
					{
						$value = $value->getValue();
					}
					if ( $value instanceof DecimalValue )
					{
						$value = $value->getValue();
					}
				case XmlTypeCode::Double:
				case XmlTypeCode::Float:

					$value = $value instanceof XPath2Item ? $value->getValue() : $value;
					if ( $value instanceof DoubleProxy )
					{
						return $value;
					}

					if ( is_string( $value ) && strtoupper( $value ) == "NAN" )
					{
						$value = NAN;
					}
					else
					{
						if ($xmlType->TypeCode == XmlTypeCode::Double )
						{
							$value = doubleval( $value );
						}
						else if ( $value == INF || $value == -INF )
						{
							$value = XPath2Item::fromValueAndType( $value, $type->SchemaType );
						}
						else
						{
							// Using ::FromFloat will convert 1.2E-3 to a decimal number
							$decimal = DecimalValue::FromFloat( $value );
							$value = XPath2Item::fromValueAndType( $decimal->ToFloat( null ), $type->SchemaType );
						}
					}
					break;
			}
			return $value;
		}
		try
		{
			switch ( $xmlType->TypeCode )
			{
				case XmlTypeCode::String:
				case XmlTypeCode::UntypedAtomic:

					if ( $value instanceof UntypedAtomic )
					{
						$value = $value->getValue();
					}

					switch ( $type->TypeCode )
					{
						case XmlTypeCode::UntypedAtomic:
							return new UntypedAtomic( $value );
						case XmlTypeCode::String:
							return $value; // ->ToString();
						case XmlTypeCode::DateTime:
							return DateTimeValue::Parse( $value );
						case XmlTypeCode::Date:
							return DateValue::Parse( $value );
						case XmlTypeCode::Time:
							return TimeValue::Parse( $value );
						case XmlTypeCode::GYearMonth:
							return GYearMonthValue::Parse( $value );
						case XmlTypeCode::GYear:
							return GYearValue::Parse( $value );
						case XmlTypeCode::GMonth:
							return GMonthValue::Parse( $value );
						case XmlTypeCode::GMonthDay:
							return GMonthDayValue::Parse( $value );
						case XmlTypeCode::GDay:
							return GDayValue::Parse( $value );
						case XmlTypeCode::Duration:
							return DurationValue::Parse( $value );
						case XmlTypeCode::Name:
							return new NameValue( $value );
						case XmlTypeCode::Token:
							return new TokenValue( $value );
						case XmlTypeCode::Language:
							return new LanguageValue( $value );
						case XmlTypeCode::NormalizedString:
							return new NormalizedStringValue( $value );
						case XmlTypeCode::Id:
							return new IDValue( $value );
						case XmlTypeCode::QName:
							if ($xmlType->TypeCode == XmlTypeCode::UntypedAtomic)
								throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004,
									array(
										SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
										$type
									)
								);
							if ( is_string( $value ) && empty( $value ) )
							{
								throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001, array( $value, $type->ItemType->getTypeName() ) );
							}
							$qname = \lyquidity\xml\qname( is_object( $value ) ? $value->ToString() : $value, $nsmgr->getNamespaces() );
							if ( is_null( $qname ) )
							{
								throw XPath2Exception::withErrorCode( "FONS0004", Resources::FONS0004 );
							}
							return QNameValue::fromQName( $qname );
						case XmlTypeCode::Notation:
							return NotationValue::Parse( $value, $nsmgr );
						case XmlTypeCode::AnyUri:
							return new AnyUriValue( $value instanceof IXmlSchemaType ? $value->getValue() : $value );
						case XmlTypeCode::NCName:

							$value = trim( $value );
							$pattern = "/^" . NameValue::$ncName . "$/u";
							if ( ! preg_match( $pattern, $value, $matches ) )
							{
								throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001, array( $value, "xs:NCName" ) );
							}

							return XPath2Item::fromValueAndType( $value, $type->SchemaType );

						default:

							$text = is_object( $value ) ? $value->ToString() : $value;
							$text = trim( $value );
							// $res = $type->SchemaType->Datatype->ParseValue( $text, $nameTable, $nsmgr );
							$res = $text;

							// Validate the text input
							$error = false;
							switch ( $type->TypeCode )
							{
								case $type->TypeCode == XmlTypeCode::Boolean:
									$error = is_string( $text ) && strlen( $text ) == 0;
									break;

								case $type->TypeCode == XmlTypeCode::NonPositiveInteger:
									$error = ! is_numeric( $text ) || $text + 0 > 0 || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::NegativeInteger:
									$error = ! is_numeric( $text ) || $text + 0 >= 0 || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::PositiveInteger:
									$error = ! is_numeric( $text ) || $text + 0 <= 0 || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::UnsignedByte:
									$error = ! is_numeric( $text ) || $text + 0 < 0 || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::UnsignedShort:
									$error = ! is_numeric( $text ) || $text + 0 < 0 || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::UnsignedInt:
									$error = ! is_numeric( $text ) || $text + 0 < 0 || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::UnsignedLong:
									$error = ! is_numeric( $text ) || $text + 0 < 0;
									if ( ! $error )
									{
										// The long value can be bigger than PHP is able to cope with.
										// A large unsigned number will appear like a double in PHP
										$error = DecimalValue::FromValue( $text )->CompareTo( "18446744073709551615" ) == 1;
									}
									break;

								case $type->TypeCode == XmlTypeCode::NonNegativeInteger:
									$error = ! is_numeric( $text ) || $text + 0 < 0 || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::Byte:
									$error = ! is_numeric( $text ) || $text + 0 < -128 || $text + 0 > 0x7f || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::Short:
									$error = ! is_numeric( $text ) || $text + 0 < (int)0xffffffffffff8000 || $text + 0 > (int)0x7fff || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::Int:
									$error = ! is_numeric( $text ) || $text + 0 < (int)0xffffffff80000000 || $text + 0 > (int)0x7fffffff || is_double( $text + 0 );
									break;

								case $type->TypeCode == XmlTypeCode::Long:
									$error = ! is_numeric( $text ) || is_double( $text + 0 ) ||
											 DecimalValue::FromValue( $text )->CompareTo( (int)0x8000000000000000 ) == -1 ||
											 DecimalValue::FromValue( $text )->CompareTo( (int)0x7fffffffffffffff ) == 1;
									break;
								case XmlTypeCode::Double:
								case XmlTypeCode::Float:

									$error = ! preg_match( "/^((-|\+)?(\d*((?=\.\d)\.\d*)?((?i)E[-\+]?\d+((?=\.\d)\.\d*)?)?)|(-?INF)|NaN)$/", trim( $value ) );
									break;
							}

							if ( $error )
							{
								throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001, array( $text, $type->ItemType->getTypeName() ) );
							}

							switch ( $type->TypeCode )
							{
								case XmlTypeCode::Decimal:

									return new DecimalValue( $text );

								case XmlTypeCode::Double:
								case XmlTypeCode::Float:

									if ( is_string( $text ) )
									{
										if ( strtoupper( $text ) == "NAN" )
											return XPath2Item::fromValueAndType( NAN, $type->SchemaType );

										if ( strtoupper( $text ) == "INF" )
											return XPath2Item::fromValueAndType( INF, $type->SchemaType );
											// return INF;

										if ( strtoupper( $text ) == "-INF" )
											return XPath2Item::fromValueAndType( -INF, $type->SchemaType );
											// return -INF;
									}

									if ( is_numeric( $text ) ) return XPath2Item::fromValueAndType( doubleval( $text ), $type->SchemaType );
									throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001, array( $value, $type->ItemType->getTypeName() ) );
									break;

								case XmlTypeCode::Boolean:
									$result = filter_var( $value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE );
									if ( is_null( $result ) )
									{
										throw new InvalidCastException("Cannot convert '$text' to boolean");
									}
									return $result ? CoreFuncs::$True : CoreFuncs::$False;
									// return $result;
								case XmlTypeCode::Integer:
								case XmlTypeCode::PositiveInteger:
								case XmlTypeCode::NegativeInteger:
								case XmlTypeCode::NonPositiveInteger:
								case XmlTypeCode::NonNegativeInteger:
								Case XmlTypeCode::Int:
								case XmlTypeCode::Short:
								case XmlTypeCode::Byte:
								case XmlTypeCode::UnsignedByte:
								case XmlTypeCode::UnsignedInt:
								case XmlTypeCode::UnsignedShort:
									return Integer::FromValue( $res /* Convert::ToDecimal( $res ) */ );

								case XmlTypeCode::Long:
								case XmlTypeCode::UnsignedLong:
									return Long::FromValue( $res, $type->TypeCode == XmlTypeCode::UnsignedLong );

								case XmlTypeCode::DayTimeDuration:
									return DayTimeDurationValue::Parse( $res );

								case XmlTypeCode::YearMonthDuration:
									return YearMonthDurationValue::Parse( $res );

								case XmlTypeCode::HexBinary:
									return HexBinaryValue::fromString( $res );

								case XmlTypeCode::Base64Binary:
									if ( SchemaTypes::endsWith( $res, "==" ) && ( strlen( $res ) < 3 || strpos( "AQgw", $res[ strlen( $res ) -3 ] ) == -1) )
										throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001, array( $res, $type->ItemType->getTypeName() ) );
									return Base64BinaryValue::fromString( $res );

								case XmlTypeCode::Idref:
									if ( $type->SchemaType == XmlSchema::$IDREF )
										return new IDREFValue( $res );
									if ( $type->SchemaType == XmlSchema::$IDREFS )
										return new IDREFSValue( $res );
									return $res;
								case XmlTypeCode::NmToken:
									if ( $type->SchemaType == XmlSchema::$NMTOKEN )
										return new NMTOKENValue( $res );
									if ( $type->SchemaType == XmlSchema::$NMTOKENS )
										return new NMTOKENSValue( $res );
									return $res;
								case XmlTypeCode::Entity:
									if ( $type->SchemaType->Name == "ENTITY" )
										return new ENTITYValue( $res );
									if ( $type->SchemaType == XmlSchema::$ENTITIES )
										return new ENTITIESValue( $res );
									return $res;

								default:
									return $res;
							}
					}

				case XmlTypeCode::Boolean:

					$result = ( is_bool( $value ) && $value ) || ( is_object( $value ) && $value instanceof TrueValue ) ? 1 : 0;

					// Validate the text input
					$error = false;
					switch ( $type->TypeCode)
					{
						case XmlTypeCode::NonPositiveInteger:
							$error = $result;
							break;

						case XmlTypeCode::NegativeInteger:
							$error = true;
							break;

						case XmlTypeCode::PositiveInteger:
							$error = ! $result;
							break;
					}

					if ( $error )
					{
						throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001, array( $value, $type->ItemType->getTypeName() ) );
					}

					switch ( $type->TypeCode)
					{
						case XmlTypeCode::NonPositiveInteger:
						case XmlTypeCode::NegativeInteger:
						case XmlTypeCode::Integer:
						case XmlTypeCode::Long:
						case XmlTypeCode::Int:
						case XmlTypeCode::Short:
						case XmlTypeCode::Byte:
						case XmlTypeCode::NonNegativeInteger:
						case XmlTypeCode::UnsignedLong:
						case XmlTypeCode::UnsignedInt:
						case XmlTypeCode::UnsignedShort:
						case XmlTypeCode::UnsignedByte:
						case XmlTypeCode::PositiveInteger:
							return Integer::FromValue( $result );
							// return XPath2Convert::ChangeObjectType( $value, $type->ItemType );

						case XmlTypeCode::Decimal:
							return DecimalValue::FromValue( $result );

						case XmlTypeCode::Double:
						case XmlTypeCode::Float:
							return $result + 0.0;

						case XmlTypeCode::String:
							return $result ? "true" : "false";

						case XmlTypeCode::UntypedAtomic:
							return new UntypedAtomic( XPath2Convert::ToString( $value ) );

						case XmlTypeCode::Language:

							return XPath2Item::fromValueAndType( $value->ToString( null ), $type->SchemaType );
					}

					break;

				case XmlTypeCode::Integer:
				case XmlTypeCode::NonPositiveInteger:
				case XmlTypeCode::NegativeInteger:
				case XmlTypeCode::Long:
				case XmlTypeCode::Int:
				case XmlTypeCode::Short:
				case XmlTypeCode::Byte:
				case XmlTypeCode::NonNegativeInteger:
				case XmlTypeCode::UnsignedLong:
				case XmlTypeCode::UnsignedInt:
				case XmlTypeCode::UnsignedShort:
				case XmlTypeCode::UnsignedByte:
				case XmlTypeCode::PositiveInteger:
				case XmlTypeCode::Decimal:
				case XmlTypeCode::Float:
				case XmlTypeCode::Double:

					if ( $value instanceof XPath2Item )
					{
						$value = $value->getTypedValue();
					}

					if ( $value instanceof IXmlSchemaType ) $value = $value->getValue();

					switch ( $type->TypeCode )
					{
						case XmlTypeCode::String:

							return XPath2Convert::ToString( $value );

						case XmlTypeCode::UntypedAtomic:

							return new UntypedAtomic( Convert::ToString( $value ) );

						case XmlTypeCode::Boolean:

							return CoreFuncs::BooleanValue( $value );

						case XmlTypeCode::Byte:
						case XmlTypeCode::Integer:
						case XmlTypeCode::Int:
						case XmlTypeCode::Long:
						case XmlTypeCode::NegativeInteger:
						case XmlTypeCode::NonNegativeInteger:
						case XmlTypeCode::NonPositiveInteger:
						case XmlTypeCode::PositiveInteger:
						case XmlTypeCode::Short:
						case XmlTypeCode::UnsignedByte:
						case XmlTypeCode::UnsignedInt:
						case XmlTypeCode::UnsignedLong:
						case XmlTypeCode::UnsignedShort:

							if ( $value instanceof XPath2Item )
							{
								$value = $value->getTypedValue();
							}

							if ( is_numeric( $value ) )
							{
								if ( is_infinite( $value ) || is_nan( $value ) )
								{
									throw XPath2Exception::withErrorCodeAndParams( "FOCA0002", Resources::FOCA0002, array( $value, "Integer" ) );
								}

								if ( $value > PHP_INT_MAX || $value < PHP_INT_MIN )
								{
									throw XPath2Exception::withErrorCodeAndParams( "FOCA0003", Resources::FOCA0003, array( $value, "Integer" ) );
								}
							}

							return XPath2Item::fromValueAndType( Integer::FromValue( Convert::ToInt( $value ) ), $type->SchemaType );

						case XmlTypeCode::Double:

							return Convert::ToDouble( $value, null );

						case XmlTypeCode::Float:

							switch ( $xmlType->TypeCode )
							{
								case XmlTypeCode::Byte:
								case XmlTypeCode::Integer:
								case XmlTypeCode::Int:
								case XmlTypeCode::Long:
								case XmlTypeCode::NegativeInteger:
								case XmlTypeCode::NonNegativeInteger:
								case XmlTypeCode::NonPositiveInteger:
								case XmlTypeCode::PositiveInteger:
								case XmlTypeCode::Short:
								case XmlTypeCode::UnsignedByte:
								case XmlTypeCode::UnsignedInt:
								case XmlTypeCode::UnsignedLong:
								case XmlTypeCode::UnsignedShort:

									return XPath2Item::fromValueAndType( is_numeric( $value ) ? $value + 0.0 : $value, $type->SchemaType );
							}

							if ( preg_match( "/^(?<sign>[+-])?INF/i", $value, $matches ) )
							{
								return ! empty( $matches['sign'] ) && $matches['sign'] == "-" ? -INF : INF;
							}

							if ( is_numeric( $value ) && is_nan( $value ) )
							{
								return $value;
							}

							// Using ::FromFloat will convert 1.2E-3 to a decimal number
							$decimal = DecimalValue::FromFloat( $value );
							return XPath2Item::fromValueAndType( $decimal->ToFloat( null ), $type->SchemaType );

						case XmlTypeCode::Decimal:
							if ( $xmlType->TypeCode == XmlTypeCode::Decimal ) return $value;
							if ( $xmlType->TypeCode == XmlTypeCode::Double ||
								 $xmlType->TypeCode == XmlTypeCode::Float
							)
								return DecimalValue::FromFloat( $value );

							// if ( $xmlType->TypeCode == XmlTypeCode::Integer || $xmlType->TypeCode == XmlTypeCode::Int )
							return new DecimalValue( $value );

						case XmlTypeCode::AnyUri:
							throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004,
								array(
									SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
									$type
								)
							);
						case XmlTypeCode::YearMonthDuration:
						case XmlTypeCode::DayTimeDuration:
						case XmlTypeCode::Duration:
						case XmlTypeCode::DateTime:
						case XmlTypeCode::Time:
						case XmlTypeCode::Date:
						case XmlTypeCode::GDay:
						case XmlTypeCode::GMonth:
						case XmlTypeCode::GMonthDay:
						case XmlTypeCode::GYear:
						case XmlTypeCode::GYearMonth:
						case XmlTypeCode::Base64Binary:
						case XmlTypeCode::HexBinary:
						case XmlTypeCode::QName:

							throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004, array( $value, "Duration" ) );

						case XmlTypeCode::Language:

							throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001,
								array(
									$value,
									"xs:language"
								)
							);

							// return XPath2Item::fromValueAndType( $value . "", $type->SchemaType );

						default:
							return XPath2Convert::ChangeObjectType( $value, $type->ItemType );
					}

				case XmlTypeCode::DateTime:
				case XmlTypeCode::Date:
				case XmlTypeCode::Time:
				case XmlTypeCode::GDay:
				case XmlTypeCode::GMonth:
				case XmlTypeCode::GMonthDay:
				case XmlTypeCode::GYear:
				case XmlTypeCode::GYearMonth:

					if ( $type->TypeCode == $xmlType->TypeCode ) return $value;

					switch ( $type->TypeCode )
					{
						case XmlTypeCode::DateTime:

							if ( in_array( $xmlType->TypeCode, array( XmlTypeCode::Date, XmlTypeCode::DateTime ) ) )
							{
								return $value instanceof \DateTime
									? DateTimeValue::fromDate( false, $value )
									: DateTimeValue::fromDate( $value->S, $value->getValue(), ! $value->IsLocal );
							}

							break;

						case XmlTypeCode::Date:

							if ( in_array( $xmlType->TypeCode, array( XmlTypeCode::Date, XmlTypeCode::DateTime ) ) )
							{
								return $value instanceof \DateTime
									? DateValue::fromDate( false, $value )
									: DateValue::fromDate( $value->S, $value->getValue(), ! $value->IsLocal );
							}

							break;

						case XmlTypeCode::Time:

							if ( in_array( $xmlType->TypeCode, array( XmlTypeCode::Time, XmlTypeCode::DateTime ) ) )
							// if ( $xmlType->TypeCode != XmlTypeCode::Date )
							{
								return $value instanceof \DateTime
									? new TimeValue( $value )
									: new TimeValue( $value->getValue(), ! $value->IsLocal );
							}

							break;

						case XmlTypeCode::GYear:

							if ( in_array( $xmlType->TypeCode, array( XmlTypeCode::Date, XmlTypeCode::DateTime ) ) )
							// if ( $xmlType->TypeCode != XmlTypeCode::Time )
							{
								return new GYearValue( $value->S, $value->getValue() );
							}

							break;

						case XmlTypeCode::GYearMonth:

							if ( in_array( $xmlType->TypeCode, array( XmlTypeCode::Date, XmlTypeCode::DateTime ) ) )
							// if ( $xmlType->TypeCode != XmlTypeCode::Time )
							{
								return new GYearMonthValue( $value->S, $value->getValue() );
							}

							break;

						case XmlTypeCode::GDay:

							if ( in_array( $xmlType->TypeCode, array( XmlTypeCode::Date, XmlTypeCode::DateTime ) ) )
							// if ( $xmlType->TypeCode != XmlTypeCode::Time )
							{
								return new GDayValue( $value->getValue() );
							}

							break;

						case XmlTypeCode::GMonth:

							if ( in_array( $xmlType->TypeCode, array( XmlTypeCode::Date, XmlTypeCode::DateTime ) ) )
							// if ( $xmlType->TypeCode != XmlTypeCode::Time )
							{
								return new GMonthValue( $value->getValue() );
							}

							break;

						case XmlTypeCode::GMonthDay:

							if ( in_array( $xmlType->TypeCode, array( XmlTypeCode::Date, XmlTypeCode::DateTime ) ) )
							// if ( $xmlType->TypeCode != XmlTypeCode::Time )
							{
								return new GMonthDayValue( $value->getValue() );
							}

							break;

						case XmlTypeCode::UntypedAtomic:

							return new UntypedAtomic( $value->ToString( null ) );

						case XmlTypeCode::String:

							return $value->ToString( null );

						case XmlTypeCode::Language:

							throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001,
								array(
									$value,
									"xs:language"
								)
							);
					}

					throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004,
						array(
							SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
							$type
						)
					);

				case XmlTypeCode::Duration:
				case XmlTypeCode::DayTimeDuration:
				case XmlTypeCode::YearMonthDuration:

					switch ( $type->TypeCode )
					{
						case XmlTypeCode::DayTimeDuration:
							return DayTimeDurationValue::FromDuration( $value );

						case XmlTypeCode::YearMonthDuration:
							return YearMonthDurationValue::FromDuration( $value );

						case XmlTypeCode::Duration:
							return new DurationValue( $value->getValue() );

						case XmlTypeCode::UntypedAtomic:
							return new UntypedAtomic( $value->ToString( null ) );

						case XmlTypeCode::String:
							return ( $value->ToString( null ) );

						case XmlTypeCode::Language:

							throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001,
								array(
									$value,
									"xs:language"
								)
							);

					}

					throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004,
						array(
							SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
							$type
						)
					);

				case XmlTypeCode::Base64Binary:

					if ( $type->TypeCode == $xmlType->TypeCode ) return $value;

					switch ( $type->TypeCode )
					{
						case XmlTypeCode::String:

							return $value->ToString();

						case XmlTypeCode::UntypedAtomic:

							return new UntypedAtomic( $value->ToString() );

						case XmlTypeCode::HexBinary:

							return new HexBinaryValue( $value->getValue() );

						case XmlTypeCode::Language:

							throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001,
								array(
									$value,
									"xs:language"
								)
							);
					}

					throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004,
						array(
							SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
							$type
						)
					);

					break;

				case XmlTypeCode::HexBinary:

					if ( $type->TypeCode == $xmlType->TypeCode ) return $value;

					switch ( $type->TypeCode )
					{
						case XmlTypeCode::String:

							return $value->ToString();

						case XmlTypeCode::UntypedAtomic:

							return new UntypedAtomic( $value->ToString() );

						case XmlTypeCode::Base64Binary:

							return new Base64BinaryValue( $value->getValue() );

						case XmlTypeCode::Language:

							throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001,
								array(
									$value,
									"xs:language"
								)
							);
					}

					throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004,
						array(
							SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
							$type
						)
					);

					break;

				case XmlTypeCode::AnyUri:

					if ( $type->TypeCode == $xmlType->TypeCode ) return $value;

					switch ( $type->TypeCode )
					{
						case XmlTypeCode::String:

							return $value->ToString();

						case XmlTypeCode::UntypedAtomic:

							return new UntypedAtomic( $value->ToString() );

					}

					throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004,
						array(
							SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
							$type
						)
					);

					break;

				case XmlTypeCode::QName:

					if ( $type->TypeCode == $xmlType->TypeCode ) return $value;

					switch ( $type->TypeCode )
					{
						case XmlTypeCode::String:

							return $value->ToString( null );

						case XmlTypeCode::UntypedAtomic:

							return new UntypedAtomic( $value->ToString( null ) );

						// case XmlTypeCode::Base64Binary:
						// case XmlTypeCode::HexBinary:
						case XmlTypeCode::Language:

							throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001,
								array(
									$value,
									"xs:language"
								)
							);

					}

					throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004,
						array(
							SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
							$type
						)
					);

				default:

					$convertable = $value instanceof IXmlConvertable;
					if ( $convertable )
					{
						/**
						 * @var IXmlConvertable $convert
						 */
						$convert = $value;
						return $convert->ValueAs( $type, $nsmgr );
					}
					if ( $type->TypeCode == XmlTypeCode::String)
						return XPath2Convert::ToString( $value);
					if ( $type->TypeCode == XmlTypeCode::UntypedAtomic)
						return new UntypedAtomic( XPath2Convert::ToString( $value ) );

					// Getting here is a dead end
					return $type->SchemaType->Datatype->ChangeType1( $value, $type->ValueType );
			}
		}
		catch ( XmlSchemaException $ex )
		{
			throw XPath2Exception::asDefault( $ex->getMessage(), $ex);
		}
		catch ( \InvalidArgumentException $ex )
		{
			throw XPath2Exception::asDefault( $ex->getMessage(), $ex);
		}
		catch ( InvalidCastException $ex )
		{
			throw XPath2Exception::withErrorCodeAndParams( "FORG0001", Resources::FORG0001,
					array(
						SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
						$type
					)
				);
		}
		catch ( FormatException $ex )
		{
			throw XPath2Exception::withErrorCodeAndParams( "XPTY0004", Resources::XPTY0004,
				array(
					SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
					$type
				)
			);
		}
		throw XPath2Exception::withErrorCodeAndParams("XPTY0004", Resources::XPTY0004,
			array(
				SequenceType::WithTypeCodeAndCardinality( $xmlType->TypeCode, XmlTypeCardinality::One ),
				$type
			)
		);
	}