public static function toArray(\SimpleXMLElement $xml, $namespace = null)
    {
        if ($namespace !== null) {

            $namespaces = $xml->getNamespaces();

            if (false === isset($namespaces[$namespace])) {
                throw new \OutOfBoundsException('namespace [' . $namespace . '] not found');
            }

            $xml = $xml->children($namespaces[$namespace]);
        }

        return Json::decode(Json::encode($xml), true);
    }