Skip to content

Commit

Permalink
Merge pull request #5 from xp-framework/refactor/remove-deprecated-ma…
Browse files Browse the repository at this point in the history
…rshalling

Remove deprecated static Marshaller / Unmarshaller usage
  • Loading branch information
thekid authored Mar 29, 2024
2 parents 02a2932 + 6e1ee7d commit 6aead26
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 107 deletions.
38 changes: 1 addition & 37 deletions src/main/php/xml/meta/Marshaller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@
/**
* Marshalls XML from objects by using annotations.
*
* Example:
* ```php
* // [...create transmission object...]
*
* $xml= Marshaller::marshal($transmission);
* ```
*
* @test xml.unittest.MarshallerTest
* @ext dom
* @test xml.unittest.MarshallerTest
* @see http://castor.org/xml-mapping.html
*/
class Marshaller {
Expand Down Expand Up @@ -121,35 +114,6 @@ protected static function recurse($instance, $type, $node, $inject) {
}
}

/**
* Marshal an object to xml
*
* @param object instance
* @param xml.QName qname default NULL
* @return string xml
* @deprecated Use marshalTo() instead
*/
public static function marshal($instance, $qname= null) {
$type= Reflection::type($instance);

// Create XML tree and root node. Use the information provided by the
// qname argument if existant, use the class` non-qualified (and
// lowercased) name otherwise.
$tree= new Tree();
if ($qname) {
$prefix= $qname->prefix ? $qname->prefix : $qname->localpart[0];
$tree->root()->setName($prefix.':'.$qname->localpart);
$tree->root()->setAttribute('xmlns:'.$prefix, $qname->namespace);
} else if ($type->annotation(Xmlns::class)) {
$tree->root()->setName($type->class()->getSimpleName());
} else {
$tree->root()->setName(strtolower($type->class()->getSimpleName()));
}

self::recurse($instance, $type, $tree->root(), []);
return $tree->getSource(INDENT_DEFAULT);
}

/**
* Marshal an object to xml
*
Expand Down
33 changes: 1 addition & 32 deletions src/main/php/xml/meta/Unmarshaller.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,8 @@
/**
* Creates objects from XML by using annotations.
*
* Example:
* ```php
* // [...load $xml from a file or a stream...]
*
* $transmission= Unmarshaller::unmarshal($xml, 'com.1and1.qf.xml.types.TransmissionType');
* ```
*
* @test xp://xml.unittest.UnmarshallerTest
* @ext dom
* @test xp://xml.unittest.UnmarshallerTest
* @see http://castor.org/xml-mapping.html
*/
class Unmarshaller {
Expand Down Expand Up @@ -160,30 +153,6 @@ protected static function recurse($xpath, $element, $type, $inject) {
return $instance;
}

/**
* Unmarshal XML to an object
*
* @param string xml
* @param string classname
* @return object
* @throws lang.ClassNotFoundException
* @throws xml.XMLFormatException
* @deprecated Use unmarshalFrom() instead
*/
public static function unmarshal($xml, $classname) {
libxml_clear_errors();
$doc= new DOMDocument();
$source= '(string)';
if ('' === (string)$xml) { // Handle empty string, raise XML_IO_NO_INPUT
throw new XMLFormatException('Empty string supplied as input', 1547, $source, 0, 0);
}
if (!$doc->loadXML($xml)) {
$e= libxml_get_last_error();
throw new XMLFormatException(trim($e->message), $e->code, $source, $e->line, $e->column);
}
return self::recurse(new XPath($doc), $doc->documentElement, Reflection::type($classname), []);
}

/**
* Unmarshal XML to an object
*
Expand Down
18 changes: 0 additions & 18 deletions src/test/php/xml/unittest/MarshallerTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,6 @@ public function asTree() {
Assert::equals('file.open', $node->getAttribute('id'));
}

#[Test]
public function deprecatedUsage() {
$dialog= new DialogType();
Assert::equals(
Marshaller::marshal($dialog),
$this->fixture->marshalTo(new Node('dialogtype'), $dialog)->getSource(INDENT_DEFAULT)
);
}

#[Test]
public function deprecatedUsageWithNamespace() {
$app= new ApplicationType();
Assert::equals(
Marshaller::marshal($app),
$this->fixture->marshalTo(new Node('ApplicationType'), $app)->getSource(INDENT_DEFAULT)
);
}

#[Test]
public function inject() {
$window= (new WindowType())->withOwnerWindow(1);
Expand Down
22 changes: 2 additions & 20 deletions src/test/php/xml/unittest/UnmarshallerTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,28 +124,10 @@ public function emptyStream() {
);
}

#[Test]
public function deprecatedUsage() {
$xml= '<dialogtype id="file.open"/>';
$type= 'xml.unittest.DialogType';
Assert::equals(
Unmarshaller::unmarshal($xml, $type),
$this->fixture->unmarshalFrom(new StreamInputSource(new MemoryInputStream($xml)), $type)
);
}

#[Test, Expect(XMLFormatException::class)]
public function malformedString() {
Unmarshaller::unmarshal(
'<not-valid-xml',
'xml.unittest.DialogType'
);
}

#[Test, Expect(XMLFormatException::class)]
public function emptyString() {
Unmarshaller::unmarshal(
'',
$this->fixture->unmarshalFrom(
new StreamInputSource(new MemoryInputStream('<not-valid-xml'), 'memory'),
'xml.unittest.DialogType'
);
}
Expand Down

0 comments on commit 6aead26

Please sign in to comment.