Last active
September 13, 2017 16:44
-
-
Save trogwarz/c05f01f2ea63c0e8ec7f27ffcb003589 to your computer and use it in GitHub Desktop.
Toster 460459
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use AppBundle\Entity\Interfaces\Favoritable; | |
use AppBundle\Entity\User\User; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Bridge\Doctrine\Validator\Constraints; | |
/** | |
* @ORM\Entity(repositoryClass="AppBundle\Repository\Favorite") | |
* @Constraints\UniqueEntity(fields={"user","order"}) | |
* @ORM\Table(uniqueConstraints={ | |
* @ORM\UniqueConstraint(name="IDX_UNIQ_FAV_ORDER", columns={"user_id", "order_id"}), | |
* }) | |
*/ | |
class Favorite | |
{ | |
/** | |
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="favorites") | |
*/ | |
private $user; | |
/** | |
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Order") | |
*/ | |
private $order; | |
public function __construct(User $user, Favoritable $target) | |
{ | |
parent::__construct(); | |
$this->setUser($user); | |
switch (true) { | |
case $target instanceof Order: $this->order = $target; break; | |
default: throw new \InvalidArgumentException('Object of ' . get_class($target) . ' is not mapped to favorite'); | |
} | |
} | |
public function getUser() : User | |
{ | |
return $this->user; | |
} | |
public function getTarget() : ?Favoritable | |
{ | |
if ($this->order) return $this->order; | |
return null; | |
} | |
public function setUser(User $v) | |
{ | |
$this->user = $v; | |
} | |
public function setTarget(Favoritable $v) | |
{ | |
switch (true) { | |
case $target instanceof Order: $this->order = $target; break; | |
default: throw new \InvalidArgumentException('Object of ' . get_class($target) . ' is not mapped to favorites'); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Traits; | |
use AppBundle\Entity\Favorite; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\ORM\Mapping as ORM; | |
trait HasFavorites | |
{ | |
/** | |
* @var ArrayCollection | |
* @ORM\OneToMany(targetEntity="AppBundle\Entity\Favorite", mappedBy="user") | |
*/ | |
protected $favorites; | |
public function getFavorites() : Collection { return $this->favorites; } | |
public function addFavorite(Favorite $favorite) { $this->favorites->add($favorite); } | |
public function removeFavorite(Favorite $favorite) { $this->favorites->removeElement($favorite); } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Entity; | |
use AppBundle\Entity\Interfaces\HasFavorites as IHasFavorites; | |
use AppBundle\Entity\Traits\HasFavorites as HasFavoritesTrait; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Bridge\Doctrine\Validator\Constraints; | |
/** | |
* @ORM\Entity(repositoryClass="\AppBundle\Repository\User") | |
* @Constraints\UniqueEntity("username") | |
* @Constraints\UniqueEntity("email") | |
*/ | |
class User implements IHasFavorites | |
{ | |
use HasFavoritesTrait; | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(type="string") | |
*/ | |
protected $username; | |
public function __construct(string $username) | |
{ | |
parent::__construct(); | |
$this->setUsername($username); | |
$this->favorites = new ArrayCollection(); | |
} | |
public function getUsername() : string { return $this->username; } | |
public function setUsername(string $v = null) | |
{ | |
\Assert\Assert::that($v)->nullOr()->string()->betweenLength(3, 100); | |
$this->username = $v; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Schema; | |
use AppBundle\Entity\Order; | |
class OrderSchema | |
{ | |
protected $resourceType = 'orders'; | |
/** | |
* @param Order $order | |
* | |
* @return array | |
*/ | |
public function getAttributes($order) | |
{ | |
return [ | |
'id' => $order->getId(), | |
'user_id' => $order->getUser()->getId(), | |
'in_favorites' => \random_int(0, 1), // <= Here is it! @todo | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment