/** * Parses a WebDAV multistatus response body * * This method returns an array with the following structure * * array( * 'url/to/resource' => array( * '200' => array( * '{DAV:}property1' => 'value1', * '{DAV:}property2' => 'value2', * ), * '404' => array( * '{DAV:}property1' => null, * '{DAV:}property2' => null, * ), * ) * 'url/to/resource2' => array( * .. etc .. * ) * ) * * * @param string $body xml body * @return array */ public function parseMultiStatus($body) { $body = XMLUtil::convertDAVNamespace($body); $responseXML = simplexml_load_string($body, null, LIBXML_NOBLANKS | LIBXML_NOCDATA); if ($responseXML === false) { throw new \InvalidArgumentException('The passed data is not valid XML'); } $responseXML->registerXPathNamespace('d', 'urn:DAV'); $propResult = array(); foreach ($responseXML->xpath('d:response') as $response) { $response->registerXPathNamespace('d', 'urn:DAV'); $href = $response->xpath('d:href'); $href = (string) $href[0]; $properties = array(); foreach ($response->xpath('d:propstat') as $propStat) { $propStat->registerXPathNamespace('d', 'urn:DAV'); $status = $propStat->xpath('d:status'); list($httpVersion, $statusCode, $message) = explode(' ', (string) $status[0], 3); // Only using the propertymap for results with status 200. $propertyMap = $statusCode === '200' ? $this->propertyMap : array(); $properties[$statusCode] = XMLUtil::parseProperties(dom_import_simplexml($propStat), $propertyMap); } $propResult[$href] = $properties; } return $propResult; }
function testParsePropertiesMapHref() { $xml = '<?xml version="1.0"?> <root xmlns="DAV:"> <prop> <displayname>Calendars</displayname> </prop> <prop> <someprop><href>http://sabredav.org/</href></someprop> </prop> </root>'; $dom = XMLUtil::loadDOMDocument($xml); $properties = XMLUtil::parseProperties($dom->firstChild, array('{DAV:}someprop' => 'Sabre\\DAV\\Property\\Href')); $this->assertEquals(array('{DAV:}displayname' => 'Calendars', '{DAV:}someprop' => new Property\Href('http://sabredav.org/', false)), $properties); }