Browse Source

Normalize serialization output

Jordi Boggiano 12 years ago
parent
commit
8d993004aa
2 changed files with 33 additions and 13 deletions
  1. 16 6
      src/Packagist/WebBundle/Entity/Author.php
  2. 17 7
      src/Packagist/WebBundle/Entity/Version.php

+ 16 - 6
src/Packagist/WebBundle/Entity/Author.php

@@ -81,12 +81,22 @@ class Author
 
     public function toArray()
     {
-        return array(
-            'name' => $this->getName(),
-            'email' => $this->getEmail(),
-            'homepage' => $this->getHomepage(),
-            'role' => $this->getRole(),
-        );
+        $data = array();
+
+        if ($this->getName()) {
+            $data['name'] = $this->getName();
+        }
+        if ($this->getEmail()) {
+            $data['email'] = $this->getEmail();
+        }
+        if ($this->getHomepage()) {
+            $data['homepage'] = $this->getHomepage();
+        }
+        if ($this->getRole()) {
+            $data['role'] = $this->getRole();
+        }
+
+        return $data;
     }
 
     /**

+ 17 - 7
src/Packagist/WebBundle/Entity/Version.php

@@ -221,23 +221,33 @@ class Version
 
         $data = array(
             'name' => $this->getName(),
-            'description' => $this->getDescription(),
+            'description' => (string) $this->getDescription(),
             'keywords' => $tags,
-            'homepage' => $this->getHomepage(),
+            'homepage' => (string) $this->getHomepage(),
             'version' => $this->getVersion(),
             'version_normalized' => $this->getNormalizedVersion(),
             'license' => $this->getLicense(),
             'authors' => $authors,
             'source' => $this->getSource(),
-            'time' => $this->getReleasedAt() ? $this->getReleasedAt()->format('Y-m-d\TH:i:sP') : null,
             'dist' => $this->getDist(),
             'type' => $this->getType(),
-            'target-dir' => $this->getTargetDir(),
-            'autoload' => $this->getAutoload(),
-            'extra' => $this->getExtra(),
-            'include-path' => $this->getIncludePaths(),
         );
 
+        if ($this->getReleasedAt()) {
+            $data['time'] = $this->getReleasedAt()->format('Y-m-d\TH:i:sP');
+        }
+        if ($this->getAutoload()) {
+            $data['autoload'] = $this->getAutoload();
+        }
+        if ($this->getExtra()) {
+            $data['extra'] = $this->getExtra();
+        }
+        if ($this->getTargetDir()) {
+            $data['target-dir'] = $this->getTargetDir();
+        }
+        if ($this->getIncludePaths()) {
+            $data['include-path'] = $this->getIncludePaths();
+        }
         if ($this->getBinaries()) {
             $data['bin'] = $this->getBinaries();
         }