vendor/spatie/schema-org/src/BaseType.php line 87

Open in your IDE?
  1. <?php
  2. namespace Spatie\SchemaOrg;
  3. use ArrayAccess;
  4. use DateTime;
  5. use DateTimeInterface;
  6. use JsonSerializable;
  7. use ReflectionClass;
  8. use Spatie\SchemaOrg\Exceptions\InvalidProperty;
  9. abstract class BaseType implements Type, ArrayAccess, JsonSerializable
  10. {
  11. /** @var array */
  12. protected $properties = [];
  13. public function getContext(): string
  14. {
  15. return 'https://schema.org';
  16. }
  17. public function getType(): string
  18. {
  19. return (new ReflectionClass($this))->getShortName();
  20. }
  21. public function setProperty(string $property, $value)
  22. {
  23. if ($value !== null && $value !== '') {
  24. $this->properties[$property] = $value;
  25. }
  26. return $this;
  27. }
  28. public function addProperties(array $properties)
  29. {
  30. foreach ($properties as $property => $value) {
  31. $this->setProperty($property, $value);
  32. }
  33. return $this;
  34. }
  35. public function if($condition, $callback)
  36. {
  37. if ($condition) {
  38. $callback($this);
  39. }
  40. return $this;
  41. }
  42. public function getProperty(string $property, $default = null)
  43. {
  44. return $this->properties[$property] ?? $default;
  45. }
  46. public function getProperties(): array
  47. {
  48. return $this->properties;
  49. }
  50. /**
  51. * @return ReferencedType|static
  52. */
  53. public function referenced()
  54. {
  55. return new ReferencedType($this);
  56. }
  57. public function offsetExists($offset)
  58. {
  59. return array_key_exists($offset, $this->properties);
  60. }
  61. public function offsetGet($offset)
  62. {
  63. return $this->getProperty($offset);
  64. }
  65. public function offsetSet($offset, $value)
  66. {
  67. $this->setProperty($offset, $value);
  68. }
  69. public function offsetUnset($offset)
  70. {
  71. unset($this->properties[$offset]);
  72. }
  73. public function toArray(): array
  74. {
  75. $this->serializeIdentifier();
  76. $properties = $this->serializeProperty($this->getProperties());
  77. return [
  78. '@context' => $this->getContext(),
  79. '@type' => $this->getType(),
  80. ] + $properties;
  81. }
  82. protected function serializeProperty($property)
  83. {
  84. if (is_array($property)) {
  85. return array_map([$this, 'serializeProperty'], $property);
  86. }
  87. if ($property instanceof Type) {
  88. $property = $property->toArray();
  89. unset($property['@context']);
  90. }
  91. if ($property instanceof DateTimeInterface) {
  92. $property = $property->format(DateTime::ATOM);
  93. }
  94. if (is_object($property) && method_exists($property, '__toString')) {
  95. $property = (string) $property;
  96. }
  97. if (is_object($property)) {
  98. throw new InvalidProperty();
  99. }
  100. return $property;
  101. }
  102. protected function serializeIdentifier()
  103. {
  104. if (
  105. isset($this['identifier'])
  106. && ! $this['identifier'] instanceof Type
  107. ) {
  108. $this->setProperty('@id', $this['identifier']);
  109. unset($this['identifier']);
  110. }
  111. }
  112. public function toScript(): string
  113. {
  114. return '<script type="application/ld+json">'.json_encode($this->toArray(), JSON_UNESCAPED_UNICODE).'</script>';
  115. }
  116. public function jsonSerialize()
  117. {
  118. return $this->toArray();
  119. }
  120. public function __call(string $method, array $arguments)
  121. {
  122. return $this->setProperty($method, $arguments[0] ?? '');
  123. }
  124. public function __toString(): string
  125. {
  126. return $this->toScript();
  127. }
  128. }