Cast value based on type
@param SimpleXMLElement $xmlElement
@return mixed
@access protected
542188
protected function castValue(SimpleXMLElement $xmlElement)
    {
        $attributes = $xmlElement->attributes();
        if (isset($attributes['nil']) && $attributes['nil'] == 'true') {
            $value = null;
        } else {
            switch (isset($attributes['type']) ? strval($attributes['type']) : null) {
                case 'integer':
                    $value = intval($xmlElement);
                    break;
                case 'float':
                    $value = floatval($xmlElement);
                    break;
                case 'boolean':
                    $value = strval($xmlElement) == 'true';
                    break;
                case 'datetime':
                case 'date':
                    $value = new DateTime(strval($xmlElement));
                    break;
                case 'string':
                default:
                    $value = strval($xmlElement);
                    break;
            }
        }
        return $value;
    }
857944
public function getValue()
    {
        $value = '';

        if (is_array($this->_value)) {
            foreach ($this->_value as $v) {
                if ($v instanceof XMLElement) {
                    $value .= $v->generate();
                } else {
                    $value .= $v;
                }
            }
        } elseif (!is_null($this->_value)) {
            $value = $this->_value;
        }

        return $value;
    }
37731
protected function getDateIntervalFromXML($xmlText)
    {
        if (empty($xmlText)) {
            return;
        }

        $xml = new SimpleXMLElement($xmlText);
        $aIntervalString = array(
            (int)$xml->year['value'] . ' years',
            (int)$xml->month['value'] . ' months',
            (int)$xml->day['value'] . ' days',
            (int)$xml->hour['value'] . ' hours',
            (int)$xml->minute['value'] . ' minutes',
            (int)$xml->second['value'] . ' seconds',
        );

        return DateInterval::createFromDateString(implode(', ', $aIntervalString));
    }
743272
private static function deserializeTokenVerificationKey($xmlElement)
    {
        if (!isset($xmlElement->attributes(Resources::XSI_XML_NAMESPACE)->type)) {
            throw new \RuntimeException("A TokenVerificationKey must contains a 'type' attribute");
        }

        $type = $xmlElement->attributes(Resources::XSI_XML_NAMESPACE)->type;

        if ($type == 'SymmetricVerificationKey') {
            if (!isset($xmlElement->KeyValue)) {
                throw new \RuntimeException("The SymmetricVerificationKey must contains an 'KeyValue' element");
            }

            $key = new SymmetricVerificationKey();
            $key->setKeyValue(base64_decode($xmlElement->KeyValue));

            return $key;
        }

        if ($type == 'X509CertTokenVerificationKey') {
            if (!isset($xmlElement->RawBody)) {
                throw new \RuntimeException("The X509CertTokenVerificationKey must contains a 'RawBody' element");
            }

            $key = new X509CertTokenVerificationKey();
            $key->setRawBody(base64_decode($xmlElement->RawBody));

            return $key;
        }

        throw new \RuntimeException("Unknown TokenVerificationKey type: type='{$type}'");
    }
1301527
public function parseString($xmlString, $xmlFile = null)
    {
        // we don't want infinite recursion
        if ($this->isAlreadyParsed($xmlFile)) {
            return;
        }
        // store current schema file path
        $this->schemasTagsStack[$xmlFile] = [];
        $this->currentXmlFile = $xmlFile;

        $parser = xml_parser_create();
        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
        xml_set_object($parser, $this);
        xml_set_element_handler($parser, 'startElement', 'endElement');
        if (!xml_parse($parser, $xmlString)) {
            throw new SchemaException(sprintf('XML error: %s at line %d',
                xml_error_string(xml_get_error_code($parser)),
                xml_get_current_line_number($parser))
            );
        }
        xml_parser_free($parser);

        array_pop($this->schemasTagsStack);

        return $this->schema;
    }
733019
protected function parseTables(\SimpleXMLElement $element)
    {
        /** @var \SimpleXMLElement $tableElement */
        foreach ($element as $tableElement) {
            $attributes = $tableElement->attributes();
            
            $tableName = (string) $attributes['table'];
            $tablePhpName = isset($attributes['name']) && !empty((string) $attributes['name'])
                ? (string) $attributes['name']
                : $tableName;
            
            $table = new Table($tableName);
            $table->setName($tablePhpName);
            
            $this->schema->addTable($table);
            $this->parseColumns($tableElement, $table);
            $this->parseRelations($tableElement, $table);
        }
        
        return $this;
    }
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
			)
		);
	}
70716
private function InferXmlType()
	{
		if ( ! is_object( $this->_value ) )
		{
			if ( is_string( $this->_value ) )
				$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::String );
			else if ( is_int( $this->_value ) )
				$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Integer );
			else if ( is_double( $this->_value ) )
				$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Double );
			else if ( is_numeric( $this->_value ) )
				$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Int );
			else if ( is_bool( $this->_value ) )
				$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Boolean );
			else if ( $this->_value instanceof FalseValue || $this->_value instanceof TrueValue )
				$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Boolean );
			// else if ( $this->_value is Int64)
			// 	$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Long );
			// else if ( $this->_value is Single)
			// 	$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Float );
			// else if ( $this->_value is Int16)
			// 	$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Short );
			// else if ( $this->_value is UInt16)
			// 	$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::UnsignedShort );
			// else if ( $this->_value is UInt32)
			// 	$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::UnsignedInt );
			// else if ( $this->_value is UInt64)
			// 	$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::UnsignedLong );
			// else if ( $this->_value is SByte)
			// 	$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Byte );
			// else if ( $this->_value is Byte)
			// 	$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::UnsignedByte );
		}
		else if ( $this->_value instanceof TrueValue || $this->_value instanceof FalseValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Boolean );
		else if ( $this->_value instanceof DecimalValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Decimal );
		else if ( $this->_value instanceof UntypedAtomic )
			$this->_xmlType = XmlSchema::$UntypedAtomic;
		else if ( $this->_value instanceof Integer )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Integer );
		else if ( $this->_value instanceof DateTimeValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::DateTime );
		else if ( $this->_value instanceof DateValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Date );
		else if ( $this->_value instanceof TimeValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Time );
		else if ( $this->_value instanceof DayTimeDurationValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::DayTimeDuration );
		else if ( $this->_value instanceof YearMonthDurationValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::YearMonthDuration );
		else if ( $this->_value instanceof DurationValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Duration );
		else if ( $this->_value instanceof GYearMonthValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::GYearMonth );
		else if ( $this->_value instanceof GYearValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::GYear );
		else if ( $this->_value instanceof GDayValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::GDay );
		else if ( $this->_value instanceof GMonthValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::GMonth );
		else if ( $this->_value instanceof GMonthDayValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::GMonthDay );
		else if ( $this->_value instanceof QNameValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::QName );
		else if ( $this->_value instanceof AnyUriValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::AnyUri );
		else if ( $this->_value instanceof HexBinaryValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::HexBinary );
		else if ( $this->_value instanceof Base64BinaryValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Base64Binary );
		else if ( $this->_value instanceof NMTOKENSValue )
			$this->_xmlType = XmlSchema::$NMTOKENS;
		else if ( $this->_value instanceof IDREFSValue )
			$this->_xmlType = XmlSchema::$IDREFS;
		else if ( $this->_value instanceof ENTITIESValue )
			$this->_xmlType = XmlSchema::$ENTITIES;
		else if ( $this->_value instanceof NotationValue )
			$this->_xmlType = DOMSchemaType::GetBuiltInSimpleTypeByTypeCode( XmlTypeCode::Notation );
		else
		{
			$typeName = get_class( $this->_value );
			throw new ArgumentException("Unrecognized value type in XPath2Item instance ($typeName)" );
		}
	}
906470
public static function parseResponseRuleXml(SimpleXMLElement $xml){
        $returnValue = self::parseElementXml($xml);
        foreach($xml->children() as $child){
            $returnValue['expression'] = self::parseExpressionXml($child);
            break;
        }
        return $returnValue;
    }
567539
private static function deserializePlayReadyPlayRight($xmlElement)
    {
        $result = new PlayReadyPlayRight();

        if (isset($xmlElement->FirstPlayExpiration)) {
            $result->setFirstPlayExpiration(new \DateInterval((string) $xmlElement->FirstPlayExpiration));
        }

        if (isset($xmlElement->ScmsRestriction) && isset($xmlElement->ScmsRestriction->ConfigurationData)) {
            $result->setScmsRestriction(
                new ScmsRestriction(intval((string) $xmlElement->ScmsRestriction->ConfigurationData))
            );
        }

        if (isset($xmlElement->AgcAndColorStripeRestriction) &&
            isset($xmlElement->AgcAndColorStripeRestriction->ConfigurationData)) {
            $result->setAgcAndColorStripeRestriction(
                new AgcAndColorStripeRestriction(
                    intval((string) $xmlElement->AgcAndColorStripeRestriction->ConfigurationData)
                )
            );
        }

        if (isset($xmlElement->ExplicitAnalogTelevisionOutputRestriction) &&
            isset($xmlElement->ExplicitAnalogTelevisionOutputRestriction->ConfigurationData)) {
            $bestEffort = false;
            if (isset($xmlElement->ExplicitAnalogTelevisionOutputRestriction->BestEffort)) {
                $bestEffort = $xmlElement->ExplicitAnalogTelevisionOutputRestriction->BestEffort == 'true';
            }
            $configurationData =
                intval((string) $xmlElement->ExplicitAnalogTelevisionOutputRestriction->ConfigurationData);
            $result->setExplicitAnalogTelevisionOutputRestriction(
                new ExplicitAnalogTelevisionRestriction($configurationData, $bestEffort)
            );
        }

        if (isset($xmlElement->DigitalVideoOnlyContentRestriction)) {
            $result->setDigitalVideoOnlyContentRestriction($xmlElement->DigitalVideoOnlyContentRestriction == 'true');
        }

        if (isset($xmlElement->ImageConstraintForAnalogComponentVideoRestriction)) {
            $result->setImageConstraintForAnalogComponentVideoRestriction(
                $xmlElement->ImageConstraintForAnalogComponentVideoRestriction == 'true'
            );
        }

        if (isset($xmlElement->ImageConstraintForAnalogComputerMonitorRestriction)) {
            $result->setImageConstraintForAnalogComputerMonitorRestriction(
                $xmlElement->ImageConstraintForAnalogComputerMonitorRestriction == 'true'
            );
        }

        if (isset($xmlElement->AllowPassingVideoContentToUnknownOutput)) {
            $result->setAllowPassingVideoContentToUnknownOutput(
                (string) $xmlElement->AllowPassingVideoContentToUnknownOutput
            );
        }

        if (isset($xmlElement->UncompressedDigitalVideoOpl)) {
            $result->setUncompressedDigitalVideoOpl((int) $xmlElement->UncompressedDigitalVideoOpl);
        }

        if (isset($xmlElement->CompressedDigitalVideoOpl)) {
            $result->setCompressedDigitalVideoOpl((int) $xmlElement->CompressedDigitalVideoOpl);
        }

        if (isset($xmlElement->AnalogVideoOpl)) {
            $result->setAnalogVideoOpl((int) $xmlElement->AnalogVideoOpl);
        }

        if (isset($xmlElement->CompressedDigitalAudioOpl)) {
            $result->setCompressedDigitalAudioOpl((int) $xmlElement->CompressedDigitalAudioOpl);
        }

        if (isset($xmlElement->UncompressedDigitalAudioOpl)) {
            $result->setUncompressedDigitalAudioOpl((int) $xmlElement->UncompressedDigitalAudioOpl);
        }

        return $result;
    }
1237067
private function xmlLoadString($xmlString)
    {
        libxml_clear_errors();
        libxml_use_internal_errors(true);
        $xml = simplexml_load_string($xmlString);

        if ($errors = libxml_get_errors()) {
            throw new \RuntimeException('Failed to parse OFX: ' . var_export($errors, true));
        }

        return $xml;
    }
471937
private static function deserializePlayReadyContentKey($xmlElement)
    {
        $type = $xmlElement->attributes(Resources::XSI_XML_NAMESPACE)->type;

        if ($type == 'ContentEncryptionKeyFromHeader') {
            return new ContentEncryptionKeyFromHeader();
        }

        if ($type == 'ContentEncryptionKeyFromKeyIdentifier') {
            return new ContentEncryptionKeyFromKeyIdentifier($xmlElement->KeyIdentifier);
        }

        throw new \RuntimeException("Unknown PlayReadyContentKey type={$type}");
    }
909599
public function toXML(\DOMNode $parentElement = null) : DOMElement
    {
        if ($parentElement === null) {
            $document = DOMDocumentFactory::create();
            $parentElement = $document;
        } else {
            $document = $parentElement->ownerDocument;
        }

        $root = $document->createElementNS(Constants::NS_SAML, 'saml:'.'Assertion');
        $parentElement->appendChild($root);

        /* Ugly hack to add another namespace declaration to the root element. */
        $root->setAttributeNS(Constants::NS_SAMLP, 'samlp:tmp', 'tmp');
        $root->removeAttributeNS(Constants::NS_SAMLP, 'tmp');
        $root->setAttributeNS(Constants::NS_XSI, 'xsi:tmp', 'tmp');
        $root->removeAttributeNS(Constants::NS_XSI, 'tmp');
        $root->setAttributeNS(Constants::NS_XS, 'xs:tmp', 'tmp');
        $root->removeAttributeNS(Constants::NS_XS, 'tmp');

        $root->setAttribute('ID', $this->id);
        $root->setAttribute('Version', '2.0');
        $root->setAttribute('IssueInstant', gmdate('Y-m-d\TH:i:s\Z', $this->issueInstant));

        $issuer = $this->issuer->toXML($root);

        $this->addSubject($root);
        $this->addConditions($root);
        $this->addAuthnStatement($root);
        if ($this->getRequiredEncAttributes() === false) {
            $this->addAttributeStatement($root);
        } else {
            $this->addEncryptedAttributeStatement($root);
        }

        if ($this->signatureKey !== null) {
            Utils::insertSignature($this->signatureKey, $this->certificates, $root, $issuer->nextSibling);
        }

        return $root;
    }
311801
private static function deserializeOpenIdConnectDiscoveryDocument($xmlElement)
    {
        if (!isset($xmlElement->OpenIdDiscoveryUri)) {
            throw new \RuntimeException(
                "The OpenIdConnectDiscoveryDocument must contains a 'OpenIdDiscoveryUri' element"
            );
        }

        $result = new OpenIdConnectDiscoveryDocument();
        $result->setOpenIdDiscoveryUri((string) $xmlElement->OpenIdDiscoveryUri);

        return $result;
    }
172368
public static function parseElementXml(SimpleXMLElement $xml){

        $attributes = array();
        foreach($xml->attributes() as $name => $value){
            $attributes[$name] = (string) $value;
        }

        $returnValue = array(
            'qtiClass' => $xml->getName()
        );

        if(count($attributes)){
            $returnValue['attributes'] = $attributes;
        }

        return $returnValue;
    }
494451
public static function parseResponseConditionXml(SimpleXMLElement $xml){
        $returnValue = self::parseElementXml($xml);
        foreach($xml->responseIf as $responseIfXml){
            $returnValue['responseIf'] = self::parseResponseIfXml($responseIfXml);
            break;
        }
        foreach($xml->responseElseIf as $responseIfXml){
            if(!isset($returnValue['responseElseIfs'])){
                $returnValue['responseElseIfs'] = array();
            }
            $returnValue['responseElseIfs'][] = self::parseResponseIfXml($responseIfXml);
        }
        foreach($xml->responseElse as $responseIfXml){
            $returnValue['responseElse'] = self::parseResponseElseXml($responseIfXml);
            break;
        }
        return $returnValue;
    }
1162650
public static function WithElement( $schemaElement )
	{
		$result = new SequenceType();

		$result->TypeCode = XmlTypeCode::Element;
		$result->SchemaElement = $schemaElement;
		$result->IsNode = true;
		$result->ItemType = $result->TypeCodeToItemType( $result->TypeCode, $result->SchemaType );

		return $result;
	}