Browse Source

Switch funding prop to json, sort contents to maximize metadata compression

Jordi Boggiano 5 years ago
parent
commit
54fcb5016a
1 changed files with 14 additions and 5 deletions
  1. 14 5
      src/Packagist/WebBundle/Entity/Version.php

+ 14 - 5
src/Packagist/WebBundle/Entity/Version.php

@@ -180,7 +180,7 @@ class Version
     private $support;
 
     /**
-     * @ORM\Column(type="text", nullable=true)
+     * @ORM\Column(type="json", nullable=true)
      */
     private $funding;
 
@@ -270,7 +270,7 @@ class Version
         if ($serializeForApi && $this->getSupport()) {
             $data['support'] = $this->getSupport();
         }
-        if ($serializeForApi && $this->getFunding()) {
+        if ($this->getFunding()) {
             $data['funding'] = $this->getFunding();
         }
         if ($this->getReleasedAt()) {
@@ -329,7 +329,6 @@ class Version
             $array['support'] = $this->getSupport();
             ksort($array['support']);
         }
-        // TODO do we need to do something for funding here? sort by type/url?
 
         return $array;
     }
@@ -632,7 +631,17 @@ class Version
      */
     public function setFunding($funding)
     {
-        $this->funding = $funding ? json_encode($funding) : null;
+        // sort records when storing so to help the V2 metadata compression algo
+        if ($funding) {
+            usort($funding, function ($a, $b) {
+                $keyA = ($a['type'] ?? '') . ($a['url'] ?? '');
+                $keyB = ($b['type'] ?? '') . ($b['url'] ?? '');
+                
+                return $keyA <=> $keyB;
+            });
+        }
+
+        $this->funding = $funding;
     }
 
     /**
@@ -642,7 +651,7 @@ class Version
      */
     public function getFunding()
     {
-        return $this->funding ? json_decode($this->funding, true) : null;
+        return $this->funding;
     }
 
     /**