src/Entity/Company.php line 21

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2019, MND Next GmbH - www.mndnext.de
  4.  */
  5. namespace App\Entity;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\Common\Collections\Criteria;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use Symfony\Component\Intl\Intl;
  13. /**
  14.  * @ORM\Entity
  15.  * @ORM\HasLifecycleCallbacks()
  16.  * @ORM\Table(name="companies")
  17.  */
  18. class Company
  19. {
  20.     public const API_OBJECT_NAME 'company';
  21.     const MANDATORY_PROPS = ['name''street''streetnr''postcode''city''country'];
  22.     /**
  23.      * @ORM\Id
  24.      * @ORM\Column(type="integer")
  25.      * @ORM\GeneratedValue(strategy="AUTO")
  26.      */
  27.     protected $id;
  28.     /**
  29.      * @var string
  30.      * @ORM\Column(name="name", type="string", length=255, nullable=true)
  31.      * @Assert\NotBlank(message="Bitte geben Sie einen Firmennamen ein.", groups={"Registration", "Profile"})
  32.      */
  33.     private $name;
  34.     /**
  35.      * @var string
  36.      * @ORM\Column(name="department", type="string", length=255, nullable=true)
  37.      */
  38.     private $department;
  39.     /**
  40.      * @var string
  41.      * @ORM\Column(name="street", type="string", length=255, nullable=false)
  42.      * @Assert\NotBlank(message="Bitte geben Sie eine Straße ein.", groups={"Registration", "Profile"})
  43.      */
  44.     private $street;
  45.     /**
  46.      * @var string
  47.      * @ORM\Column(name="streetnumber", type="string", length=20, nullable=false)
  48.      * @Assert\NotBlank(message="Bitte geben Sie eine Hausnummer ein.", groups={"Registration", "Profile"})
  49.      */
  50.     private $streetNr;
  51.     /**
  52.      * @var string
  53.      * @ORM\Column(name="postcode", type="string", length=10, nullable=false)
  54.      * @Assert\NotBlank(message="Bitte geben Sie eine PLZ ein.", groups={"Registration", "Profile"})
  55.      */
  56.     private $postcode;
  57.     /**
  58.      * @var string
  59.      * @ORM\Column(name="city", type="string", length=255, nullable=false)
  60.      * @Assert\NotBlank(message="Bitte geben Sie einen Ort ein.", groups={"Registration", "Profile"})
  61.      */
  62.     private $city;
  63.     /**
  64.      * @var string
  65.      * @ORM\Column(name="country", type="string", length=255, nullable=false)
  66.      * @Assert\NotBlank(message="Bitte wählen Sie ein Land.", groups={"Registration", "Profile"})
  67.      */
  68.     private $country;
  69.     /**
  70.      * @var string
  71.      * @ORM\Column(name="phone", type="string", length=255, nullable=true)
  72.      * @Assert\NotBlank(message="Bitte geben Sie eine Telefonnummer ein.", groups={"Registration", "Profile"})
  73.      */
  74.     private $phone;
  75.     /**
  76.      * @var \DateTime
  77.      *
  78.      * @ORM\Column(name="created", type="datetime", nullable=false)
  79.      */
  80.     private $created;
  81.     /**
  82.      * @var string
  83.      * @ORM\Column(name="notes", type="text", nullable=true)
  84.      */
  85.     private $notes;
  86.     /**
  87.      * @var string
  88.      * @ORM\Column(name="manufacturer", type="boolean", nullable=false, options={"default" : false})
  89.      */
  90.     private $manufacturer false;
  91.     /**
  92.      * @var string
  93.      * @ORM\Column(name="directory", type="string", length=10, nullable=true)
  94.      */
  95.     private $fileDir;
  96.     /**
  97.      * @ORM\OneToMany(targetEntity="Employee", mappedBy="company")
  98.      */
  99.     private $employees;
  100.     /**
  101.      * @ORM\OneToMany(targetEntity="Machine", mappedBy="company")
  102.      */
  103.     private $machines;
  104.     /**
  105.      * @ORM\OneToMany(targetEntity="CompanyFile", mappedBy="company", cascade={"remove"})
  106.      */
  107.     private $files;
  108.     /**
  109.      * Constructor
  110.      */
  111.     public function __construct()
  112.     {
  113.         $this->employees = new \Doctrine\Common\Collections\ArrayCollection();
  114.         $this->files = new \Doctrine\Common\Collections\ArrayCollection();
  115.         $this->created = new \DateTime();
  116.     }
  117.     public function getId()
  118.     {
  119.         return $this->id;
  120.     }
  121.     public function setName($name)
  122.     {
  123.         $this->name $name;
  124.         return $this;
  125.     }
  126.     public function getName()
  127.     {
  128.         return $this->name;
  129.     }
  130.     public function setDepartment($clientnr)
  131.     {
  132.         $this->department $clientnr;
  133.         return $this;
  134.     }
  135.     public function getDepartment()
  136.     {
  137.         return $this->department;
  138.     }
  139.     public function setStreet($street)
  140.     {
  141.         $this->street $street;
  142.         return $this;
  143.     }
  144.     public function getStreet()
  145.     {
  146.         return $this->street;
  147.     }
  148.     public function setStreetNr($streetnr)
  149.     {
  150.         $this->streetNr $streetnr;
  151.         return $this;
  152.     }
  153.     public function getStreetNr()
  154.     {
  155.         return $this->streetNr;
  156.     }
  157.     public function setPostcode($postcode)
  158.     {
  159.         $this->postcode $postcode;
  160.         return $this;
  161.     }
  162.     public function getPostcode()
  163.     {
  164.         return $this->postcode;
  165.     }
  166.     public function setCity($city)
  167.     {
  168.         $this->city $city;
  169.         return $this;
  170.     }
  171.     public function getCity()
  172.     {
  173.         return $this->city;
  174.     }
  175.     public function setCountry($country)
  176.     {
  177.         $this->country $country;
  178.         return $this;
  179.     }
  180.     public function getCountry()
  181.     {
  182.         return $this->country;
  183.     }
  184.     public function getCountryName() {
  185.         return Intl::getRegionBundle()->getCountryName($this->getCountry(), 'de_DE');
  186.     }
  187.     public function setPhone($phone)
  188.     {
  189.         $this->phone $phone;
  190.         return $this;
  191.     }
  192.     public function getPhone()
  193.     {
  194.         return $this->phone;
  195.     }
  196.     public function setCreated($created)
  197.     {
  198.         $this->created $created;
  199.         return $this;
  200.     }
  201.     public function getCreated()
  202.     {
  203.         return $this->created;
  204.     }
  205.     public function setNotes($notes)
  206.     {
  207.         $this->notes $notes;
  208.         return $this;
  209.     }
  210.     public function getNotes()
  211.     {
  212.         return $this->notes;
  213.     }
  214.     public function getFileDir()
  215.     {
  216.         return $this->fileDir;
  217.     }
  218.     public function setFileDir($fileDir)
  219.     {
  220.         if (!$this->fileDir) {
  221.             $this->fileDir $fileDir;
  222.         }
  223.         return $this;
  224.     }
  225.     public function setManufacturer($state)
  226.     {
  227.         $this->manufacturer $state;
  228.         return $this;
  229.     }
  230.     public function isManufacturer()
  231.     {
  232.         return $this->manufacturer;
  233.     }
  234.     /**
  235.      * @return Collection|Employee[]
  236.      */
  237.     public function getEmployees() : Collection
  238.     {
  239.         return $this->employees;
  240.     }
  241.     /**
  242.      * @return Collection|Employee[]
  243.      */
  244.     public function getContactPersons() : Collection
  245.     {
  246.         $contacts = new ArrayCollection();
  247.         /* @var Employee $employee */
  248.         foreach ($this->employees as $employee) {
  249.             if ($employee->isContractor()) {
  250.                 $contacts->add($employee);
  251.             }
  252.         }
  253.         return $contacts;
  254.     }
  255.     /**
  256.      * @return Collection|Machine[]
  257.      */
  258.     public function getMachines() : Collection
  259.     {
  260.         return $this->machines;
  261.     }
  262.     /**
  263.      * @return Collection|CompanyFile[]
  264.      */
  265.     public function getEmployeeFiles() : Collection
  266.     {
  267.         $files = new ArrayCollection();
  268.         /* @var CompanyFile $file */
  269.         foreach ($this->files as $file) {
  270.             if ($file->isVisible() && !$file->isSensitive()) {
  271.                 $files->add($file);
  272.             }
  273.         }
  274.         return $files;
  275.     }
  276.     /**
  277.      * @return Collection|CompanyFile[]
  278.      */
  279.     public function getContactFiles() : Collection
  280.     {
  281.         $files = new ArrayCollection($this->files->toArray());
  282.         /* @var CompanyFile $file */
  283.         foreach ($this->files as $file) {
  284.             if (!$file->isVisible()) {
  285.                 $files->removeElement($file);
  286.             }
  287.         }
  288.         return $files;
  289.     }
  290.     /**
  291.      * @return Collection|CompanyFile[]
  292.      */
  293.     public function getFiles() : Collection
  294.     {
  295.         return $this->files;
  296.     }
  297.     public function __toString()
  298.     {
  299.         return $this->getName();
  300.     }
  301.     public function getAddress()
  302.     {
  303.         $address $this->getStreet() . ' ' $this->getStreetNr();
  304.         $address .= "\n";
  305.         $address .= $this->getPostcode() . ' ' $this->getCity();
  306.         return $address;
  307.     }
  308.     private static function getOptProp($data$prop)
  309.     {
  310.         if (array_key_exists($prop$data)) {
  311.             if (!is_string($data[$prop])) {
  312.                 throw new BadRequestHttpException(sprintf('The key "%s" must be must be a string in "company".'$prop));
  313.             }
  314.             return $data[$prop];
  315.         }
  316.     }
  317.     public static function createFromData($data)
  318.     {
  319.         foreach (self::MANDATORY_PROPS as $prop) {
  320.             if (!array_key_exists($prop$data)) {
  321.                 throw new BadRequestHttpException(sprintf('The key "%s" must be provided in "company".'$prop));
  322.             }
  323.         }
  324.         $object = new Company();
  325.         $object->setName(self::getOptProp($data'name'));
  326.         $object->setStreet(self::getOptProp($data'street'));
  327.         $object->setStreetNr(self::getOptProp($data'streetnr'));
  328.         $object->setPostcode(self::getOptProp($data'postcode'));
  329.         $object->setCity(self::getOptProp($data'city'));
  330.         $object->setCountry(self::getOptProp($data'country'));
  331.         $object->setPhone(self::getOptProp($data'phone'));
  332.         return $object;
  333.     }
  334. }