src/Entity/ContactPropertyValue.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ContactPropertyValueRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. /**
  8. * @ORM\Entity(repositoryClass=ContactPropertyValueRepository::class)
  9. */
  10. class ContactPropertyValue
  11. {
  12. /**
  13. * @ORM\Id
  14. * @ORM\GeneratedValue
  15. * @ORM\Column(type="integer")
  16. */
  17. private $id;
  18. /**
  19. * @ORM\ManyToOne(targetEntity=Contact::class, inversedBy="contactPropertyValues")
  20. * @ORM\JoinColumn(nullable=false)
  21. */
  22. private $contact;
  23. /**
  24. * @ORM\ManyToOne(targetEntity=Property::class, inversedBy="contactPropertyValues")
  25. * @ORM\JoinColumn(nullable=false)
  26. */
  27. private $property;
  28. /**
  29. * @ORM\Column(type="string", length=255, nullable=true)
  30. */
  31. private $value;
  32. /**
  33. * @Assert\Callback
  34. */
  35. public function validate(ExecutionContextInterface $context, $playload){
  36. if($this->value == null)
  37. return;
  38. if($this->property->getValidationClass() == null)
  39. return;
  40. $class = $this->property->getValidationClass();
  41. $validator = new $class();
  42. $isValidate = $validator->validate($this->value);
  43. if(!$isValidate){
  44. $context->buildViolation($validator->getErrorMessage())
  45. ->atPath('value')
  46. ->addViolation();
  47. }
  48. }
  49. public function getId(): ?int
  50. {
  51. return $this->id;
  52. }
  53. public function getContact(): ?Contact
  54. {
  55. return $this->contact;
  56. }
  57. public function setContact(?Contact $contact): self
  58. {
  59. $this->contact = $contact;
  60. return $this;
  61. }
  62. public function getProperty(): ?Property
  63. {
  64. return $this->property;
  65. }
  66. public function setProperty(?Property $property): self
  67. {
  68. $this->property = $property;
  69. return $this;
  70. }
  71. public function getValue()
  72. {
  73. return $this->value;
  74. }
  75. public function setValue($value): self
  76. {
  77. if (is_a($value, 'DateTime')) {
  78. $this->value = $value->format('d/m/Y');
  79. }elseif(is_array($value)){
  80. $this->value = "[".implode(",",$value)."]";
  81. }else{
  82. $this->value = $value;
  83. }
  84. return $this;
  85. }
  86. }