partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

Footprint.php (5593B)


      1 <?php
      2 
      3 namespace PartKeepr\FootprintBundle\Entity;
      4 
      5 use Doctrine\Common\Collections\ArrayCollection;
      6 use Doctrine\ORM\Mapping as ORM;
      7 use PartKeepr\CoreBundle\Entity\BaseEntity;
      8 use PartKeepr\DoctrineReflectionBundle\Annotation\TargetService;
      9 use PartKeepr\UploadedFileBundle\Annotation\UploadedFile;
     10 use PartKeepr\UploadedFileBundle\Annotation\UploadedFileCollection;
     11 use Symfony\Component\Serializer\Annotation\Groups;
     12 
     13 /**
     14  * @ORM\Entity
     15  * @TargetService(uri="/api/footprints")
     16  */
     17 class Footprint extends BaseEntity
     18 {
     19     /**
     20      * Holds the footprint name.
     21      *
     22      * @ORM\Column(length=64,unique=true)
     23      * @Groups({"default"})
     24      *
     25      * @var string
     26      */
     27     private $name;
     28 
     29     /**
     30      * Holds the footprint description.
     31      *
     32      * @ORM\Column(type="text",nullable=true)
     33      * @Groups({"default"})
     34      *
     35      * @var string
     36      */
     37     private $description;
     38 
     39     /**
     40      * The category of the footprint.
     41      *
     42      * @ORM\ManyToOne(targetEntity="FootprintCategory", inversedBy="footprints")
     43      * @Groups({"default"})
     44      *
     45      * @var FootprintCategory
     46      */
     47     private $category;
     48 
     49     /**
     50      * Holds the footprint image.
     51      *
     52      * @ORM\OneToOne(targetEntity="PartKeepr\FootprintBundle\Entity\FootprintImage",
     53      *               mappedBy="footprint", cascade={"persist", "remove"}, orphanRemoval=true)
     54      *
     55      * @Groups({"default"})
     56      * @UploadedFile()
     57      *
     58      * @var FootprintImage
     59      */
     60     private $image;
     61 
     62     /**
     63      * Holds the footprint attachments.
     64      *
     65      * @ORM\OneToMany(targetEntity="PartKeepr\FootprintBundle\Entity\FootprintAttachment",
     66      *                mappedBy="footprint", cascade={"persist", "remove"}, orphanRemoval=true)
     67      * @UploadedFileCollection()
     68      * @Groups({"default"})
     69      *
     70      * @var FootprintAttachment
     71      */
     72     private $attachments;
     73 
     74     /**
     75      * Returns the category path.
     76      *
     77      * @Groups({"default"})
     78      *
     79      * @return string
     80      */
     81     public function getCategoryPath()
     82     {
     83         if ($this->category !== null) {
     84             return $this->category->getCategoryPath();
     85         } else {
     86             return '';
     87         }
     88     }
     89 
     90     /**
     91      * Constructs a new Footprint entity.
     92      */
     93     public function __construct()
     94     {
     95         $this->attachments = new ArrayCollection();
     96     }
     97 
     98     /**
     99      * Sets the name of this footprint.
    100      *
    101      * @param string $name The footprint name
    102      *
    103      * @return void
    104      */
    105     public function setName($name)
    106     {
    107         $this->name = $name;
    108     }
    109 
    110     /**
    111      * Returns the name of this footprint.
    112      *
    113      * @return string The name of this footprint
    114      */
    115     public function getName()
    116     {
    117         return $this->name;
    118     }
    119 
    120     /**
    121      * Sets the description of this footprint.
    122      *
    123      * @param string $description The description
    124      *
    125      * @return void
    126      */
    127     public function setDescription($description)
    128     {
    129         $this->description = $description;
    130     }
    131 
    132     /**
    133      * Returns the description of this footprint.
    134      *
    135      * @return string The description
    136      */
    137     public function getDescription()
    138     {
    139         return $this->description;
    140     }
    141 
    142     /**
    143      * Sets the category for this footprint.
    144      *
    145      * @param FootprintCategory $category The category
    146      *
    147      * @return void
    148      */
    149     public function setCategory(FootprintCategory $category)
    150     {
    151         $this->category = $category;
    152     }
    153 
    154     /**
    155      * Returns the category of this footprint.
    156      *
    157      * @return FootprintCategory The footprint category
    158      */
    159     public function getCategory()
    160     {
    161         return $this->category;
    162     }
    163 
    164     /**
    165      * Sets the footprint image.
    166      *
    167      * @param FootprintImage $image The footprint image
    168      *
    169      * @return void
    170      */
    171     public function setImage($image)
    172     {
    173         if ($image instanceof FootprintImage) {
    174             $image->setFootprint($this);
    175             $this->image = $image;
    176         } else {
    177             // Because this is a 1:1 relationship. only allow the temporary image to be set when no image exists.
    178             // If an image exists, the frontend needs to deliver the old file ID with the replacement property set.
    179             if ($this->getImage() === null) {
    180                 $this->image = $image;
    181             }
    182         }
    183     }
    184 
    185     /**
    186      * Returns the footprint image.
    187      *
    188      * @return FootprintImage The footprint image
    189      */
    190     public function getImage()
    191     {
    192         return $this->image;
    193     }
    194 
    195     /**
    196      * Returns the attachments for this footprint.
    197      *
    198      * @return ArrayCollection The attachments
    199      */
    200     public function getAttachments()
    201     {
    202         return $this->attachments->getValues();
    203     }
    204 
    205     /**
    206      * Adds an IC Logo.
    207      *
    208      * @param FootprintAttachment|\PartKeepr\UploadedFileBundle\Entity\TempUploadedFile $attachment
    209      *                                                                                              Either a FootprintAttachment or a TempUploadedFile
    210      *
    211      * @return void
    212      */
    213     public function addAttachment($attachment)
    214     {
    215         if ($attachment instanceof FootprintAttachment) {
    216             $attachment->setFootprint($this);
    217         }
    218 
    219         $this->attachments->add($attachment);
    220     }
    221 
    222     /**
    223      * Removes an IC Logo.
    224      *
    225      * @param FootprintAttachment $attachment
    226      *
    227      * @return void
    228      */
    229     public function removeAttachment($attachment)
    230     {
    231         if ($attachment instanceof FootprintAttachment) {
    232             $attachment->setFootprint(null);
    233         }
    234         $this->attachments->removeElement($attachment);
    235     }
    236 }