summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Serializers/ExpDataSerializer.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/src/Serializers/ExpDataSerializer.php')
-rw-r--r--www/wiki/extensions/SemanticMediaWiki/src/Serializers/ExpDataSerializer.php65
1 files changed, 65 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/src/Serializers/ExpDataSerializer.php b/www/wiki/extensions/SemanticMediaWiki/src/Serializers/ExpDataSerializer.php
new file mode 100644
index 00000000..1f3a031d
--- /dev/null
+++ b/www/wiki/extensions/SemanticMediaWiki/src/Serializers/ExpDataSerializer.php
@@ -0,0 +1,65 @@
+<?php
+
+namespace SMW\Serializers;
+
+use OutOfBoundsException;
+use Serializers\Serializer;
+use SMW\Exporter\Element\ExpElement;
+use SMWExpData as ExpData;
+
+/**
+ * @license GNU GPL v2+
+ * @since 2.2
+ *
+ * @author mwjames
+ */
+class ExpDataSerializer implements Serializer {
+
+ /**
+ * @see Serializer::serialize
+ *
+ * @since 2.2
+ */
+ public function serialize( $expData ) {
+
+ if ( !$expData instanceof ExpData ) {
+ throw new OutOfBoundsException( 'Object is not supported' );
+ }
+
+ return $this->doSerialize( $expData ) + [ 'serializer' => __CLASS__, 'version' => 0.1 ];
+ }
+
+ private function doSerialize( $expData ) {
+
+ $serialization = [
+ 'subject' => $expData->getSubject()->getSerialization()
+ ];
+
+ $properties = [];
+
+ foreach ( $expData->getProperties() as $property ) {
+ $properties[$property->getUri()] = [
+ 'property' => $property->getSerialization(),
+ 'children' => $this->doSerializeChildren( $expData->getValues( $property ) )
+ ];
+ }
+
+ return $serialization + [ 'data' => $properties ];
+ }
+
+ private function doSerializeChildren( array $elements ) {
+
+ $children = [];
+
+ if ( $elements === [] ) {
+ return $children;
+ }
+
+ foreach ( $elements as $element ) {
+ $children[] = $element instanceof ExpElement ? $element->getSerialization() : $this->doSerialize( $element );
+ }
+
+ return $children;
+ }
+
+}