Нет описания

SubscriberCustomFieldEntity.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace MailPoet\Entities;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Doctrine\EntityTraits\AutoincrementedIdTrait;
  5. use MailPoet\Doctrine\EntityTraits\CreatedAtTrait;
  6. use MailPoet\Doctrine\EntityTraits\SafeToOneAssociationLoadTrait;
  7. use MailPoet\Doctrine\EntityTraits\UpdatedAtTrait;
  8. use MailPoet\InvalidStateException;
  9. use MailPoet\Util\DateConverter;
  10. use MailPoetVendor\Doctrine\ORM\Mapping as ORM;
  11. /**
  12. * @ORM\Entity()
  13. * @ORM\Table(name="subscriber_custom_field")
  14. */
  15. class SubscriberCustomFieldEntity {
  16. use AutoincrementedIdTrait;
  17. use CreatedAtTrait;
  18. use UpdatedAtTrait;
  19. use SafeToOneAssociationLoadTrait;
  20. /**
  21. * @ORM\ManyToOne(targetEntity="MailPoet\Entities\SubscriberEntity")
  22. * @var SubscriberEntity|null
  23. */
  24. private $subscriber;
  25. /**
  26. * @ORM\ManyToOne(targetEntity="MailPoet\Entities\CustomFieldEntity")
  27. * @var CustomFieldEntity|null
  28. */
  29. private $customField;
  30. /**
  31. * @ORM\Column(type="string")
  32. * @var string
  33. */
  34. private $value;
  35. /**
  36. * @param string|array|null $value
  37. */
  38. public function __construct(
  39. SubscriberEntity $subscriber,
  40. CustomFieldEntity $customField,
  41. $value
  42. ) {
  43. $this->subscriber = $subscriber;
  44. $this->customField = $customField;
  45. $this->setValue($value);
  46. }
  47. /**
  48. * @return SubscriberEntity|null
  49. */
  50. public function getSubscriber() {
  51. $this->safelyLoadToOneAssociation('subscriber');
  52. return $this->subscriber;
  53. }
  54. public function getValue(): string {
  55. return $this->value;
  56. }
  57. /**
  58. * @return CustomFieldEntity|null
  59. */
  60. public function getCustomField() {
  61. return $this->customField;
  62. }
  63. /**
  64. * @param string|array|null $value
  65. */
  66. public function setValue($value): void {
  67. $customField = $this->getCustomField();
  68. if (!$customField instanceof CustomFieldEntity) {
  69. throw new InvalidStateException('CustomField has to be set');
  70. }
  71. // format custom field data depending on type
  72. if (is_array($value) && $customField->getType() === CustomFieldEntity::TYPE_DATE) {
  73. $customFieldParams = $customField->getParams();
  74. $dateFormat = $customFieldParams['date_format'] ?? null;
  75. $dateType = isset($customFieldParams['date_type']) ? $customFieldParams['date_type'] : 'year_month_day';
  76. switch ($dateType) {
  77. case 'year_month_day':
  78. $value = str_replace(['DD', 'MM', 'YYYY'], [$value['day'], $value['month'], $value['year']], $dateFormat);
  79. break;
  80. case 'year_month':
  81. $value = str_replace(['MM', 'YYYY'], [$value['month'], $value['year']], $dateFormat);
  82. break;
  83. case 'month':
  84. $value = (int)$value['month'] === 0 ? '' : sprintf('%s', $value['month']);
  85. break;
  86. case 'day':
  87. $value = (int)$value['day'] === 0 ? '' : sprintf('%s', $value['day']);
  88. break;
  89. case 'year':
  90. $value = (int)$value['year'] === 0 ? '' : sprintf('%04d', $value['year']);
  91. break;
  92. }
  93. if (!empty($value) && is_string($value)) {
  94. $value = (new DateConverter())->convertDateToDatetime($value, $dateFormat);
  95. }
  96. }
  97. if (is_array($value)) {
  98. throw new InvalidStateException('Final value has to be string');
  99. }
  100. $this->value = (string)$value;
  101. }
  102. }