Skip to content

Instantly share code, notes, and snippets.

@sunviwo
Created January 12, 2020 11:52
Show Gist options
  • Save sunviwo/de78aa33158cd5b2984ddcaa9055a0ad to your computer and use it in GitHub Desktop.
Save sunviwo/de78aa33158cd5b2984ddcaa9055a0ad to your computer and use it in GitHub Desktop.
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Filter\NearbyFilter;
use App\Filter\OrderFilter;
use App\Controller\PropertyPostAction;
/**
* @ORM\Entity(repositoryClass="App\Repository\PropertyRepository")
* @ORM\Table(name="_property", indexes={@ORM\Index(name="idx_point", columns={"point"}, flags={"spatial"})})
* @ApiFilter(
* OrderFilter::class,
* properties={
* "latitude",
* "longitude"
* }
* )
* @ApiResource(
* attributes={
* "order"={"createdAt": "DESC"},
* "pagination_enabled"=true,
* "pagination_items_per_page"=20,
* "pagination_client_items_per_page"=true,
* "pagination_partial"=true
* },
* itemOperations={
* "get"={
* "normalization_context"={
* "groups"={"get-properties"}
* }
* },
* "put"={
* "denormalization_context"={
* "groups"={"put-property"}
* }
* },
* "delete"
* },
* collectionOperations={
* "get"={
* "normalization_context"={
* "groups"={"get-properties"}
* }
* },
* "api_users_properties_get_subresource"={
* "method"="GET",
* "normalization_context"={"groups"={"get-properties"}}
* },
* "post"={
* "denormalization_context"={
* "groups"={"post-property"}
* },
* "controller"=PropertyPostAction::class,
* },
* "api_businesses_properties_get_subresource"={
* "method"="GET",
* "normalization_context"={"groups"={"get-business-property"}}
* }
* }
* )
*/
class Property
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
* @Groups({"get-properties", "post-property"})
*/
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
* @Groups({"get-properties", "get-business-property"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"get-properties", "post-property", "put-property", "get-business-property"})
*/
private $name;
/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $description;
/**
* @ORM\Column(type="float")
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $price;
/**
* @ORM\Column(type="string", length=50, nullable=true)
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $currency;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="properties")
* @ORM\JoinColumn(nullable=false)
* @Groups({"get-properties", "post-property", "get-business-property"})
*/
private $announcer;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PropertyType", inversedBy="properties")
* @ORM\JoinColumn(nullable=false)
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $propertyType;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Operation", inversedBy="properties")
* @ORM\JoinColumn(nullable=false)
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $operation;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Business", inversedBy="properties")
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $business;
/**
* @ORM\Column(type="float")
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $latitude;
/**
* @ORM\Column(type="float")
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $longitude;
/**
* @ORM\Column(type="geography", nullable=true, options={"geometry_type"="POINT", "srid"=4326})
* @Groups({"post-property", "put-property"})
*/
private $point;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Media", mappedBy="property")
* @ApiSubresource()
* @Groups({"get-properties", "get-business-property"})
*/
private $media;
/**
* @ORM\OneToMany(targetEntity="App\Entity\PropertyFeature", mappedBy="property", orphanRemoval=true, cascade={"persist"})
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $features;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $subscriptiondate;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $subscriptionenddate;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $enabled;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Offer", mappedBy="property", orphanRemoval=true)
*/
private $offers;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Comment", mappedBy="property", orphanRemoval=true)
* @ApiSubresource()
*/
private $comments;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $city;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $locality;
/**
* @ORM\Column(type="text")
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $locationinfo;
/**
* @ORM\Column(type="string", length=10)
* @Groups({"get-properties", "post-property", "put-property"})
*/
private $currencycountrycode;
public function __construct()
{
$this->media = new ArrayCollection();
$this->features = new ArrayCollection();
$this->offers = new ArrayCollection();
$this->comments = new ArrayCollection();
}
public function __toString()
{
return $this->getName().'';
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
protected function setPoint()
{
$this->point = sprintf(
'POINT(%f %f)',
(string)$this->longitude,
(string)$this->latitude
);
}
public function getPoint()
{
return $this->point;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = $price;
return $this;
}
public function getAnnouncer(): ?User
{
return $this->announcer;
}
public function setAnnouncer(?User $announcer): self
{
$this->announcer = $announcer;
return $this;
}
public function getPropertyType(): ?PropertyType
{
return $this->propertyType;
}
public function setPropertyType(?PropertyType $propertyType): self
{
$this->propertyType = $propertyType;
return $this;
}
public function getOperation(): ?Operation
{
return $this->operation;
}
public function setOperation(?Operation $operation): self
{
$this->operation = $operation;
return $this;
}
public function getBusiness(): ?Business
{
return $this->business;
}
public function setBusiness(?Business $business): self
{
$this->business = $business;
return $this;
}
public function getLatitude(): ?float
{
return $this->latitude;
}
public function setLatitude(float $latitude): self
{
$this->latitude = $latitude;
return $this;
}
public function getLongitude(): ?float
{
return $this->longitude;
}
public function setLongitude(float $longitude): self
{
$this->longitude = $longitude;
return $this;
}
/**
* @return Collection|Media[]
*/
public function getMedia(): Collection
{
return $this->media;
}
public function addMedium(Media $medium): self
{
if (!$this->media->contains($medium)) {
$this->media[] = $medium;
$medium->setProperty($this);
}
return $this;
}
public function removeMedium(Media $medium): self
{
if ($this->media->contains($medium)) {
$this->media->removeElement($medium);
// set the owning side to null (unless already changed)
if ($medium->getProperty() === $this) {
$medium->setProperty(null);
}
}
return $this;
}
/**
* @return Collection|PropertyFeature[]
*/
public function getFeatures(): Collection
{
return $this->features;
}
public function addFeature(PropertyFeature $feature): self
{
if (!$this->features->contains($feature)) {
$this->features[] = $feature;
$feature->setProperty($this);
}
return $this;
}
public function removeFeature(PropertyFeature $feature): self
{
if ($this->features->contains($feature)) {
$this->features->removeElement($feature);
// set the owning side to null (unless already changed)
if ($feature->getProperty() === $this) {
$feature->setProperty(null);
}
}
return $this;
}
public function getSubscriptiondate(): ?\DateTimeInterface
{
return $this->subscriptiondate;
}
public function setSubscriptiondate(?\DateTimeInterface $subscriptiondate): self
{
$this->subscriptiondate = $subscriptiondate;
return $this;
}
public function getSubscriptionenddate(): ?\DateTimeInterface
{
return $this->subscriptionenddate;
}
public function setSubscriptionenddate(?\DateTimeInterface $subscriptionenddate): self
{
$this->subscriptionenddate = $subscriptionenddate;
return $this;
}
public function getEnabled(): ?bool
{
return $this->enabled;
}
public function setEnabled(?bool $enabled): self
{
$this->enabled = $enabled;
return $this;
}
public function getCurrency(): ?string
{
return $this->currency;
}
public function setCurrency(?string $currency): self
{
$this->currency = $currency;
return $this;
}
/**
* @return Collection|Offer[]
*/
public function getOffers(): Collection
{
return $this->offers;
}
public function addOffer(Offer $offer): self
{
if (!$this->offers->contains($offer)) {
$this->offers[] = $offer;
$offer->setProperty($this);
}
return $this;
}
public function removeOffer(Offer $offer): self
{
if ($this->offers->contains($offer)) {
$this->offers->removeElement($offer);
// set the owning side to null (unless already changed)
if ($offer->getProperty() === $this) {
$offer->setProperty(null);
}
}
return $this;
}
/**
* @return Collection|Comment[]
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(Comment $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setProperty($this);
}
return $this;
}
public function removeComment(Comment $comment): self
{
if ($this->comments->contains($comment)) {
$this->comments->removeElement($comment);
// set the owning side to null (unless already changed)
if ($comment->getProperty() === $this) {
$comment->setProperty(null);
}
}
return $this;
}
public function getCity(): ?string
{
return $this->city;
}
public function setCity(string $city): self
{
$this->city = $city;
return $this;
}
public function getLocality(): ?string
{
return $this->locality;
}
public function setLocality(string $locality): self
{
$this->locality = $locality;
return $this;
}
public function getLocationinfo(): ?string
{
return $this->locationinfo;
}
public function setLocationinfo(string $locationinfo): self
{
$this->locationinfo = $locationinfo;
return $this;
}
public function getCurrencycountrycode(): ?string
{
return $this->currencycountrycode;
}
public function setCurrencycountrycode(string $currencycountrycode): self
{
$this->currencycountrycode = $currencycountrycode;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment