Exemple #1
0
 /**
  * @inheritdoc
  */
 public function parse($data)
 {
     $reader = $this->reader->getReader();
     $reader->xml($data);
     $valid = $reader->setSchema(__DIR__ . '/../../schema/MARC21slim.xsd');
     if (!$valid) {
         throw new \InvalidArgumentException('Incorrect Schema');
     }
     $result = $reader->parse($data)['value'];
     if ($result instanceof Collection) {
         return $result;
     }
     $record = new Record($result['leader'], $result['control'], $result['data']);
     return new Collection([$record]);
 }
    function testSerialize()
    {
        $service = new Service();
        $service->namespaceMap['urn:test'] = null;
        $xml = $service->write('{urn:test}collection', function ($writer) {
            repeatingElements($writer, ['foo', 'bar'], '{urn:test}item');
        });
        $expected = <<<XML
<?xml version="1.0"?>
<collection xmlns="urn:test">
   <item>foo</item>
   <item>bar</item>
</collection>
XML;
        $this->assertXmlStringEqualsXmlString($expected, $xml);
    }
Exemple #3
0
    function testSerialize()
    {
        $service = new Service();
        $service->namespaceMap['urn:test'] = null;
        $xml = $service->write('{urn:test}root', function ($writer) {
            enum($writer, ['{urn:test}foo1', '{urn:test}foo2']);
        });
        $expected = <<<XML
<?xml version="1.0"?>
<root xmlns="urn:test">
   <foo1/>
   <foo2/>
</root>
XML;
        $this->assertXmlStringEqualsXmlString($expected, $xml);
    }
Exemple #4
0
    function testSerializeDefaultNamespace()
    {
        $service = new Service();
        $service->elementMap['{urn:test}root'] = function ($reader) {
            return enum($reader, 'urn:test');
        };
        $xml = <<<XML
<?xml version="1.0"?>
<root xmlns="urn:test">
   <foo1/>
   <foo2/>
</root>
XML;
        $result = $service->parse($xml);
        $expected = ['foo1', 'foo2'];
        $this->assertEquals($expected, $result);
    }
    function testRead()
    {
        $service = new Service();
        $service->elementMap['{urn:test}collection'] = function ($reader) {
            return repeatingElements($reader, '{urn:test}item');
        };
        $xml = <<<XML
<?xml version="1.0"?>
<collection xmlns="urn:test">
    <item>foo</item>
    <item>bar</item>
</collection>
XML;
        $result = $service->parse($xml);
        $expected = ['foo', 'bar'];
        $this->assertEquals($expected, $result);
    }
Exemple #6
0
 /**
  * Does a PROPFIND request
  *
  * The list of requested properties must be specified as an array, in clark
  * notation.
  *
  * The returned array will contain a list of filenames as keys, and
  * properties as values.
  *
  * The properties array will contain the list of properties. Only properties
  * that are actually returned from the server (without error) will be
  * returned, anything else is discarded.
  *
  * Depth should be either 0 or 1. A depth of 1 will cause a request to be
  * made to the server to also return all child resources.
  *
  * @param string $url
  * @param array $properties
  * @param int $depth
  * @return array
  */
 function propFind($url, array $properties, $depth = 0)
 {
     $dom = new \DOMDocument('1.0', 'UTF-8');
     $dom->formatOutput = true;
     $root = $dom->createElementNS('DAV:', 'd:propfind');
     $prop = $dom->createElement('d:prop');
     foreach ($properties as $property) {
         list($namespace, $elementName) = \Sabre\Xml\Service::parseClarkNotation($property);
         if ($namespace === 'DAV:') {
             $element = $dom->createElement('d:' . $elementName);
         } else {
             $element = $dom->createElementNS($namespace, 'x:' . $elementName);
         }
         $prop->appendChild($element);
     }
     $dom->appendChild($root)->appendChild($prop);
     $body = $dom->saveXML();
     $url = $this->getAbsoluteUrl($url);
     $request = new HTTP\Request('PROPFIND', $url, ['Depth' => $depth, 'Content-Type' => 'application/xml'], $body);
     $response = $this->send($request);
     if ((int) $response->getStatus() >= 400) {
         throw new \Sabre\HTTP\ClientHttpException($response);
     }
     $result = $this->parseMultiStatus($response->getBodyAsString());
     // If depth was 0, we only return the top item
     if ($depth === 0) {
         reset($result);
         $result = current($result);
         return isset($result[200]) ? $result[200] : [];
     }
     $newResult = [];
     foreach ($result as $href => $statusList) {
         $newResult[$href] = isset($statusList[200]) ? $statusList[200] : [];
     }
     return $newResult;
 }
Exemple #7
0
 /**
  * Get tag name from a Clark notation.
  *
  * @param string $clarkedTagName
  *
  * @return string
  */
 protected static function getTagName($clarkedTagName)
 {
     list(, $tagName) = SabreXml\Service::parseClarkNotation($clarkedTagName);
     return $tagName;
 }
Exemple #8
0
 /**
  * Parse sapphp.xml file.
  * 
  * @return bool|string Boolean if not found/no data
  *                     File path if found.
  */
 private static function parseXmlFile()
 {
     $path = realpath(implode(DIRECTORY_SEPARATOR, [__DIR__, '..', '..', ''])) . DIRECTORY_SEPARATOR . 'sapphp.xml';
     if (!file_exists($path)) {
         return $path;
     }
     $xml = file_get_contents($path);
     $service = new Service();
     $service->elementMap = ['{}sapphp' => function ($reader) {
         return \Sabre\Xml\Deserializer\keyValue($reader, '');
     }, '{}box' => function ($reader) {
         return \Sabre\Xml\Deserializer\keyValue($reader, '');
     }];
     $repository = collect($service->parse($xml)['boxes'])->pluck('value');
     if ($repository->count() === 0) {
         return $path;
     }
     self::$repository = $repository;
     return true;
 }
Exemple #9
0
 /**
  * Writes a new attribute.
  *
  * The name may be specified in clark-notation.
  *
  * Returns true when successful.
  *
  * @param string $name
  * @param string $value
  * @return bool
  */
 function writeAttribute($name, $value)
 {
     if ($name[0] === '{') {
         list($namespace, $localName) = Service::parseClarkNotation($name);
         if (array_key_exists($namespace, $this->namespaceMap)) {
             // It's an attribute with a namespace we know
             $this->writeAttribute($this->namespaceMap[$namespace] . ':' . $localName, $value);
         } else {
             // We don't know the namespace, we must add it in-line
             if (!isset($this->adhocNamespaces[$namespace])) {
                 $this->adhocNamespaces[$namespace] = 'x' . (count($this->adhocNamespaces) + 1);
             }
             $this->writeAttributeNS($this->adhocNamespaces[$namespace], $localName, $namespace, $value);
         }
     } else {
         return parent::writeAttribute($name, $value);
     }
 }
 /**
  * This method takes an xml element in clark-notation, and turns it into a
  * shortened version with a prefix, if it was a known namespace.
  *
  * @param string $element
  * @return string
  */
 function xmlName($element)
 {
     list($ns, $localName) = XmlService::parseClarkNotation($element);
     if (isset($this->namespaceMap[$ns])) {
         $propName = $this->namespaceMap[$ns] . ':' . $localName;
     } else {
         $propName = $element;
     }
     return "<span title=\"" . $this->h($element) . "\">" . $this->h($propName) . "</span>";
 }
Exemple #11
0
 /**
  * @inheritdoc
  */
 public function toString()
 {
     return '<?xml version="1.0" encoding="UTF-8"?>' . $this->writer->write(static::nameElement('collection'), $this->collection);
 }
Exemple #12
0
 /**
  * Creates xml response
  *
  * @param $data - array of elements in xml response
  * @param $root_element - XML Container for $data
  * @param $namespaces - XML namespaces for response
  */
 static function create_xml_response(array $data, array $root_element = [], array $namespaces = [])
 {
     // Use Sabre xml for outputting the data
     $xml_service = new XML_Service();
     // Create Sabre xml writer
     $xml_writer = $xml_service->getWriter();
     $xml_writer->openMemory();
     $xml_writer->setIndent(true);
     // Add all namespaces to xml response
     $xml_writer->namespaceMap = $namespaces;
     // Start document with explicit xml version and encoding just to be sure
     $xml_writer->startDocument('1.0', 'UTF-8');
     // Add root element and attributes for root element
     if (isset($root_element['name']) && !empty($root_element['name'])) {
         $xml_writer->startElement($root_element['name']);
         if (isset($root_element['attributes']) && !empty($root_element['attributes'])) {
             foreach ($root_element['attributes'] as $key => $value) {
                 $xml_writer->writeAttribute($key, $value);
             }
         }
     }
     // Write data from associative array to xml
     $xml_writer->write($data);
     // Close the root element
     if (isset($root_element['name']) && !empty($root_element['name'])) {
         $xml_writer->endElement();
     }
     return $xml_writer->outputMemory();
 }