Skip to content

Commit

Permalink
Drop dependency on XML library
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 29, 2024
1 parent 0e73c3d commit 1cae611
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"keywords": ["module", "xp"],
"require" : {
"xp-framework/core": "^12.0 | ^11.0 | ^10.0",
"xp-framework/xml" : "^12.0 | ^11.0 | ^10.0",
"php" : ">=7.0.0"
},
"require-dev" : {
Expand Down
36 changes: 22 additions & 14 deletions src/main/php/img/io/XMPSegment.class.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?php namespace img\io;

use xml\dom\Document;
use DOMDocument;
use lang\FormatException;


/**
* XMP/XAP meta data segment
*
*/
/** XMP/XAP meta data segment */
class XMPSegment extends Segment {
protected $document= null;
public $source;
private $document= null;

/**
* Creates a segment instance
*
* @param string $marker
* @param xml.dom.Document document
* @param string $source
*/
public function __construct($marker, Document $document) {
public function __construct($marker, $source) {
parent::__construct($marker, null);
$this->document= $document;
$this->source= $source;
}

/**
Expand All @@ -31,15 +29,25 @@ public function __construct($marker, Document $document) {
public static function read($marker, $bytes) {

// Begin parsing after 28 bytes - strlen("http://ns.adobe.com/xap/1.0/")
return new self($marker, Document::fromString(trim(substr($bytes, 28), "\x00")));
return new self($marker, trim(substr($bytes, 28), "\x00"));
}

/**
* Gets XML document
*
* @return xml.dom.Document
* @return DOMDocument
* @throws lang.FormatException if parsing the XML fails
*/
public function document() {
if (null === $this->document) {
$this->document= new DOMDocument();
if (false === $this->document->loadXML($this->source)) {
$this->document= null;
$e= new FormatException('Cannot parse XMP/XAP segment');
\xp::gc(__FILE__);
throw $e;
}
}
return $this->document;
}

Expand All @@ -49,7 +57,7 @@ public function document() {
* @return string
*/
public function toString() {
return nameof($this).'<'.$this->marker.'>'.\xp::stringOf($this->document);
return nameof($this).'('.$this->marker.')'.$this->source;
}

/**
Expand All @@ -62,7 +70,7 @@ public function equals($cmp) {
return (
$cmp instanceof self &&
$cmp->marker === $this->marker &&
$cmp->document->equals($this->document)
$cmp->source === $this->source
);
}
}
13 changes: 5 additions & 8 deletions src/test/php/img/unittest/MetaDataReaderTest.class.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
<?php namespace img\unittest;

use DOMDocument;
use img\ImagingException;
use img\io\{CommentSegment, MetaDataReader, SOFNSegment, XMPSegment};
use test\{Assert, Before, Expect, Test};
use xml\Node;

/**
* TestCase for MetaDataReader class
*
* @see xp://img.io.MetaDataReader
*/
class MetaDataReaderTest {
protected $fixture;
private $fixture;

/**
* Sets up this unittest
Expand Down Expand Up @@ -108,7 +103,9 @@ public function com_segment() {
public function xmp_segment() {
$segments= $this->extractFromFile('xmp.jpg')->segmentsOf('img.io.XMPSegment');
$this->assertArrayOf('img.io.XMPSegment', 1, $segments);
Assert::instance(Node::class, $segments[0]->document()->getElementsByTagName('dc:title')[0]);

Assert::matches('/^<.+/', $segments[0]->source);
Assert::instance(DOMDocument::class, $segments[0]->document());
}

#[Test]
Expand Down

0 comments on commit 1cae611

Please sign in to comment.