src/NativeEncoder.php
public static function convert($value)
    {
        switch (gettype($value)) {
            case 'NULL':
                // Support for the <nil> extension
                // (http://ontosys.com/xml-rpc/extensions.php)
                return new \fpoirotte\XRL\Types\Nil(null);

            case 'boolean':
                return new \fpoirotte\XRL\Types\Boolean($value);

            case 'integer':
                try {
                    return new \fpoirotte\XRL\Types\I4($value);
                } catch (\InvalidArgumentException $e) {
                }
                return new \fpoirotte\XRL\Types\I8($value);

            case 'double':
                return new \fpoirotte\XRL\Types\Double($value);

            case 'string':
                // We try to encode it as a regular string if possible.
                if (static::isBinaryString($value)) {
                    return new \fpoirotte\XRL\Types\Base64($value);
                }
                return new \fpoirotte\XRL\Types\StringType($value);

            case 'array':
                $newValue = array_map("static::convert", $value);
                try {
                    return new \fpoirotte\XRL\Types\ArrayType($newValue);
                } catch (\InvalidArgumentException $e) {
                }
                return new \fpoirotte\XRL\Types\Struct($newValue);

            case 'object':
            case 'resource':
                // A special treatment is applied afterwards.
                break;
        }

        // Only objects & resources remain after this point.
        if ($value instanceof \fpoirotte\XRL\Types\AbstractType) {
            return $value;
        }

        if ($value instanceof \GMP) {
            $candidates = array(
                '\\fpoirotte\\XRL\\Types\\I4',
                '\\fpoirotte\\XRL\\Types\\I8',
                '\\fpoirotte\\XRL\\Types\\BigInteger',
            );
            foreach ($candidates as $candidate) {
                try {
                    return new $candidate($value);
                } catch (\InvalidArgumentException $e) {
                }
            }
        }

        if ($value instanceof \DateTime) {
            return new \fpoirotte\XRL\Types\DateTimeIso8601($value);
        }

        if (($value instanceof \DOMNode) ||
            ($value instanceof \XMLWriter) ||
            ($value instanceof \SimpleXMLElement)) {
            return new \fpoirotte\XRL\Types\Dom($value);
        }

        if ($value instanceof \Exception) {
            return new \fpoirotte\XRL\Types\Struct(
                array(
                    'faultCode'     => new \fpoirotte\XRL\Types\IntType($value->getCode()),
                    'faultString'   => new \fpoirotte\XRL\Types\StringType(
                        get_class($value).': '.$value->getMessage()
                    ),
                )
            );
        }

        if (is_object($value) && (
            ($value instanceof \Serializable) ||
            method_exists($value, '__sleep'))) {
            $value = serialize($value);

            // We try to encode it as a regular string if possible.
            if (static::isBinaryString($value)) {
                return new \fpoirotte\XRL\Types\Base64($value);
            }
            return new \fpoirotte\XRL\Types\StringType($value);
        }

        throw new \InvalidArgumentException('Unconvertible type');
    }
public static function convert($value)
    {
        switch (gettype($value)) {
            case 'NULL':
                // Support for the <nil> extension
                // (http://ontosys.com/xml-rpc/extensions.php)
                return new \fpoirotte\XRL\Types\Nil(null);

            case 'boolean':
                return new \fpoirotte\XRL\Types\Boolean($value);

            case 'integer':
                try {
                    return new \fpoirotte\XRL\Types\I4($value);
                } catch (\InvalidArgumentException $e) {
                }
                return new \fpoirotte\XRL\Types\I8($value);

            case 'double':
                return new \fpoirotte\XRL\Types\Double($value);

            case 'string':
                // We try to encode it as a regular string if possible.
                if (static::isBinaryString($value)) {
                    return new \fpoirotte\XRL\Types\Base64($value);
                }
                return new \fpoirotte\XRL\Types\StringType($value);

            case 'array':
                $newValue = array_map("static::convert", $value);
                try {
                    return new \fpoirotte\XRL\Types\ArrayType($newValue);
                } catch (\InvalidArgumentException $e) {
                }
                return new \fpoirotte\XRL\Types\Struct($newValue);

            case 'object':
            case 'resource':
                // A special treatment is applied afterwards.
                break;
        }

        // Only objects & resources remain after this point.
        if ($value instanceof \fpoirotte\XRL\Types\AbstractType) {
            return $value;
        }

        if ($value instanceof \GMP) {
            $candidates = array(
                '\\fpoirotte\\XRL\\Types\\I4',
                '\\fpoirotte\\XRL\\Types\\I8',
                '\\fpoirotte\\XRL\\Types\\BigInteger',
            );
            foreach ($candidates as $candidate) {
                try {
                    return new $candidate($value);
                } catch (\InvalidArgumentException $e) {
                }
            }
        }

        if ($value instanceof \DateTime) {
            return new \fpoirotte\XRL\Types\DateTimeIso8601($value);
        }

        if (($value instanceof \DOMNode) ||
            ($value instanceof \XMLWriter) ||
            ($value instanceof \SimpleXMLElement)) {
            return new \fpoirotte\XRL\Types\Dom($value);
        }

        if ($value instanceof \Exception) {
            return new \fpoirotte\XRL\Types\Struct(
                array(
                    'faultCode'     => new \fpoirotte\XRL\Types\IntType($value->getCode()),
                    'faultString'   => new \fpoirotte\XRL\Types\StringType(
                        get_class($value).': '.$value->getMessage()
                    ),
                )
            );
        }

        if (is_object($value) && (
            ($value instanceof \Serializable) ||
            method_exists($value, '__sleep'))) {
            $value = serialize($value);

            // We try to encode it as a regular string if possible.
            if (static::isBinaryString($value)) {
                return new \fpoirotte\XRL\Types\Base64($value);
            }
            return new \fpoirotte\XRL\Types\StringType($value);
        }

        throw new \InvalidArgumentException('Unconvertible type');
    }
Convert the given PHP value to an XML-RPC type.

\param mixed $value
The PHP value to convert.

\retval fpoirotte::XRL::Types::AbstractType
The value encapsulated in an XML-RPC type.

\note
This implementation supports the "nil" XML-RPC extension
(http://ontosys.com/xml-rpc/extensions.php).

\note
An empty PHP array will always be encoded as an empty
XML-RPC array.

\note
The following PHP types are currently supported:
-   \c null (encoded using the "nil" XML-RPC type).
-   integer (encoded using the "i4" or "i8" XML-RPC type,
depending on the actual size required to store the value).
-   boolean (encoded using the "boolean" XML-RPC type).
-   string (encoded using either the default type
of XML-RPC [string] or "base64" if the string
contains invalid UTF-8 sequences, such as when
encoding binary data).
-   double (encoded using the "double" XML-RPC type).
-   array (encoded using either the "array" or "struct"
XML-RPC type):
-   "array" is used for numerically-indexed arrays
where the keys are in the range [0..len(array)-1],
aka "lists".
-   "struct" is used for all other arrays, aka "hashes".
-   DateTime objects (encoded using the "dateTime.iso8601"
XML-RPC type).
-   GMP objects/resources (encoded as "i4", "i8" or "BigInteger",
depending on their actual storage size).
-   XML objects (SimpleXML, DOM and XMLWriter objects)
are encoded as XML DOM fragments.
-   objects that support serialization (encoded as an XML-RPC
"string" or "base64" type depending on the object's
representation in serialized form).