RelationshipTrait.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php declare(strict_types=1);
  2. namespace Grav\Framework\Relationships\Traits;
  3. use Grav\Framework\Contracts\Object\IdentifierInterface;
  4. use Grav\Framework\Flex\FlexIdentifier;
  5. use Grav\Framework\Media\MediaIdentifier;
  6. use Grav\Framework\Object\Identifiers\Identifier;
  7. use RuntimeException;
  8. use function get_class;
  9. /**
  10. * Trait RelationshipTrait
  11. *
  12. * @template T of object
  13. */
  14. trait RelationshipTrait
  15. {
  16. /** @var IdentifierInterface */
  17. protected $parent;
  18. /** @var string */
  19. protected $name;
  20. /** @var string */
  21. protected $type;
  22. /** @var array */
  23. protected $options;
  24. /** @var bool */
  25. protected $modified = false;
  26. /**
  27. * @return string
  28. * @phpstan-pure
  29. */
  30. public function getName(): string
  31. {
  32. return $this->name;
  33. }
  34. /**
  35. * @return string
  36. * @phpstan-pure
  37. */
  38. public function getType(): string
  39. {
  40. return $this->type;
  41. }
  42. /**
  43. * @return bool
  44. * @phpstan-pure
  45. */
  46. public function isModified(): bool
  47. {
  48. return $this->modified;
  49. }
  50. /**
  51. * @return IdentifierInterface
  52. * @phpstan-pure
  53. */
  54. public function getParent(): IdentifierInterface
  55. {
  56. return $this->parent;
  57. }
  58. /**
  59. * @param IdentifierInterface $identifier
  60. * @return bool
  61. * @phpstan-pure
  62. */
  63. public function hasIdentifier(IdentifierInterface $identifier): bool
  64. {
  65. return $this->getIdentifier($identifier->getId(), $identifier->getType()) !== null;
  66. }
  67. /**
  68. * @return int
  69. * @phpstan-pure
  70. */
  71. abstract public function count(): int;
  72. /**
  73. * @return void
  74. * @phpstan-pure
  75. */
  76. public function check(): void
  77. {
  78. $min = $this->options['min'] ?? 0;
  79. $max = $this->options['max'] ?? 0;
  80. if ($min || $max) {
  81. $count = $this->count();
  82. if ($min && $count < $min) {
  83. throw new RuntimeException(sprintf('%s relationship has too few objects in it', $this->name));
  84. }
  85. if ($max && $count > $max) {
  86. throw new RuntimeException(sprintf('%s relationship has too many objects in it', $this->name));
  87. }
  88. }
  89. }
  90. /**
  91. * @param IdentifierInterface $identifier
  92. * @return IdentifierInterface
  93. */
  94. private function checkIdentifier(IdentifierInterface $identifier): IdentifierInterface
  95. {
  96. if ($this->type !== $identifier->getType()) {
  97. throw new RuntimeException(sprintf('Bad identifier type %s', $identifier->getType()));
  98. }
  99. if (get_class($identifier) !== Identifier::class) {
  100. return $identifier;
  101. }
  102. if ($this->type === 'media') {
  103. return new MediaIdentifier($identifier->getId());
  104. }
  105. return new FlexIdentifier($identifier->getId(), $identifier->getType());
  106. }
  107. private function parseOptions(array $options): void
  108. {
  109. $this->type = $options['type'];
  110. $this->options = $options;
  111. }
  112. }