Browse Source

Add Requirement entity and relations

Jordi Boggiano 13 years ago
parent
commit
499f94306b

+ 0 - 2
src/Packagist/WebBundle/Entity/Author.php

@@ -35,7 +35,6 @@ class Author
      * Unique package name
      *
      * @ORM\Column(type="text", nullable="true")
-     * @Assert\NotBlank()
      */
     private $name;
 
@@ -56,7 +55,6 @@ class Author
 
     /**
      * @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\User", inversedBy="authors")
-     * @Assert\Type(type="Packagist\WebBundle\Entity\User")
      */
     private $owner;
 

+ 121 - 0
src/Packagist/WebBundle/Entity/Requirement.php

@@ -0,0 +1,121 @@
+<?php
+
+/*
+ * This file is part of Packagist.
+ *
+ * (c) Jordi Boggiano <j.boggiano@seld.be>
+ *     Nils Adermann <naderman@naderman.de>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Packagist\WebBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use Symfony\Component\Validator\Constraints as Assert;
+
+/**
+ * @ORM\Entity
+ * @ORM\Table(name="requirement")
+ * @author Jordi Boggiano <j.boggiano@seld.be>
+ */
+class Requirement
+{
+    /**
+     * @ORM\Id
+     * @ORM\Column(type="integer")
+     * @ORM\GeneratedValue(strategy="AUTO")
+     */
+    private $id;
+
+    /**
+     * @ORM\Column()
+     */
+    private $packageName;
+
+    /**
+     * @ORM\Column()
+     */
+    private $packageVersion;
+
+    /**
+     * @ORM\ManyToOne(targetEntity="Packagist\WebBundle\Entity\Version", inversedBy="requirements")
+     */
+    private $version;
+
+    public function toArray()
+    {
+        return array($this->packageName => $this->packageVersion);
+    }
+
+    public function setId($id)
+    {
+        $this->id = $id;
+    }
+
+    public function getId()
+    {
+        return $this->id;
+    }
+
+    /**
+     * Set packageName
+     *
+     * @param string $packageName
+     */
+    public function setPackageName($packageName)
+    {
+        $this->packageName = $packageName;
+    }
+
+    /**
+     * Get packageName
+     *
+     * @return string
+     */
+    public function getPackageName()
+    {
+        return $this->packageName;
+    }
+
+    /**
+     * Set packageVersion
+     *
+     * @param string $packageVersion
+     */
+    public function setPackageVersion($packageVersion)
+    {
+        $this->packageVersion = $packageVersion;
+    }
+
+    /**
+     * Get packageVersion
+     *
+     * @return string
+     */
+    public function getPackageVersion()
+    {
+        return $this->packageVersion;
+    }
+
+    /**
+     * Set version
+     *
+     * @param Packagist\WebBundle\Entity\Version $version
+     */
+    public function setVersion(\Packagist\WebBundle\Entity\Version $version)
+    {
+        $this->version = $version;
+    }
+
+    /**
+     * Get version
+     *
+     * @return Packagist\WebBundle\Entity\Version
+     */
+    public function getVersion()
+    {
+        return $this->version;
+    }
+}

+ 47 - 33
src/Packagist/WebBundle/Entity/Version.php

@@ -85,22 +85,19 @@ class Version
     private $authors;
 
     /**
-     * JSON object of source spec
-     *
+     * @ORM\OneToMany(targetEntity="Packagist\WebBundle\Entity\Requirement", mappedBy="version")
+     */
+    private $requirements;
+
+    /**
      * @ORM\Column(type="text")
-     * @Assert\NotBlank()
      */
     private $source;
 
     /**
-     * JSON object of requirements
-     *
-     * @ORM\Column(type="text", name="requires")
-     * @Assert\NotBlank()
+     * @ORM\Column(type="text")
      */
-    private $require;
-
-//    dist (later)
+    private $dist;
 
     /**
      * @ORM\Column(type="datetime")
@@ -135,6 +132,11 @@ class Version
         foreach ($this->getAuthors() as $author) {
             $authors[] = $author->toArray();
         }
+        $requirements = array();
+        foreach ($this->getRequirements() as $requirement) {
+            $requirement = $requirement->toArray();
+            $requirements[key($requirement)] = current($requirement);
+        }
         return array(
             'name' => $this->name,
             'description' => $this->description,
@@ -143,10 +145,10 @@ class Version
             'version' => $this->version,
             'license' => $this->license,
             'authors' => $authors,
-            'require' => $this->getRequire(),
+            'require' => $requirements,
             'source' => $this->getSource(),
             'time' => $this->releasedAt ? $this->releasedAt->format('Y-m-d\TH:i:sP') : null,
-            'dist' => array(),
+            'dist' => $this->getDist(),
         );
     }
 
@@ -267,9 +269,7 @@ class Version
      */
     public function setSource($source)
     {
-        if (preg_match('#^([a-z-]+) (\S+)$#', $source, $m)) {
-            $this->source = json_encode(array('type' => $m[1], 'url' => $m[2]));
-        }
+        $this->source = json_encode($source);
     }
 
     /**
@@ -279,33 +279,27 @@ class Version
      */
     public function getSource()
     {
-        return json_decode($this->source);
+        return json_decode($this->source, true);
     }
 
     /**
-     * Set require
+     * Set dist
      *
-     * @param text $require
+     * @param text $dist
      */
-    public function setRequire($require)
+    public function setDist($dist)
     {
-        if (preg_match_all('#^(\S+) (\S+)\r?\n?$#m', $require, $m)) {
-            $requires = array();
-            foreach ($m[1] as $idx => $package) {
-                $requires[$package] = $m[2][$idx];
-            }
-            $this->require = json_encode($requires);
-        }
+        $this->dist = json_encode($dist);
     }
 
     /**
-     * Get require
+     * Get dist
      *
-     * @return text $require
+     * @return text
      */
-    public function getRequire()
+    public function getDist()
     {
-        return json_decode($this->require);
+        return json_decode($this->dist, true);
     }
 
     /**
@@ -353,7 +347,7 @@ class Version
      *
      * @param Packagist\WebBundle\Entity\Package $package
      */
-    public function setPackage(\Packagist\WebBundle\Entity\Package $package)
+    public function setPackage(Package $package)
     {
         $this->package = $package;
     }
@@ -373,7 +367,7 @@ class Version
      *
      * @param Packagist\WebBundle\Entity\Tag $tags
      */
-    public function addTags(\Packagist\WebBundle\Entity\Tag $tags)
+    public function addTags(Tag $tags)
     {
         $this->tags[] = $tags;
     }
@@ -462,7 +456,7 @@ class Version
      *
      * @param Packagist\WebBundle\Entity\Author $authors
      */
-    public function addAuthors(\Packagist\WebBundle\Entity\Author $authors)
+    public function addAuthors(Author $authors)
     {
         $this->authors[] = $authors;
     }
@@ -476,4 +470,24 @@ class Version
     {
         return $this->authors;
     }
+
+    /**
+     * Add requirements
+     *
+     * @param Packagist\WebBundle\Entity\Requirement $requirements
+     */
+    public function addRequirements(Requirement $requirements)
+    {
+        $this->requirements[] = $requirements;
+    }
+
+    /**
+     * Get requirements
+     *
+     * @return Doctrine\Common\Collections\Collection
+     */
+    public function getRequirements()
+    {
+        return $this->requirements;
+    }
 }