MapDataDefinition.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Drupal\Core\TypedData;
  3. /**
  4. * A typed data definition class for defining maps.
  5. */
  6. class MapDataDefinition extends ComplexDataDefinitionBase {
  7. /**
  8. * The name of the main property, or NULL if there is none.
  9. *
  10. * @var string
  11. */
  12. protected $mainPropertyName = NULL;
  13. /**
  14. * Creates a new map definition.
  15. *
  16. * @param string $type
  17. * (optional) The data type of the map. Defaults to 'map'.
  18. *
  19. * @return static
  20. */
  21. public static function create($type = 'map') {
  22. $definition['type'] = $type;
  23. return new static($definition);
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public static function createFromDataType($data_type) {
  29. return static::create($data_type);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getPropertyDefinitions() {
  35. if (!isset($this->propertyDefinitions)) {
  36. $this->propertyDefinitions = [];
  37. }
  38. return $this->propertyDefinitions;
  39. }
  40. /**
  41. * Sets the definition of a map property.
  42. *
  43. * @param string $name
  44. * The name of the property to define.
  45. * @param \Drupal\Core\TypedData\DataDefinitionInterface|null $definition
  46. * (optional) The property definition to set, or NULL to unset it.
  47. *
  48. * @return $this
  49. */
  50. public function setPropertyDefinition($name, DataDefinitionInterface $definition = NULL) {
  51. if (isset($definition)) {
  52. $this->propertyDefinitions[$name] = $definition;
  53. }
  54. else {
  55. unset($this->propertyDefinitions[$name]);
  56. }
  57. return $this;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getMainPropertyName() {
  63. return $this->mainPropertyName;
  64. }
  65. /**
  66. * Sets the main property name.
  67. *
  68. * @param string|null $name
  69. * The name of the main property, or NULL if there is none.
  70. *
  71. * @return $this
  72. */
  73. public function setMainPropertyName($name) {
  74. $this->mainPropertyName = $name;
  75. return $this;
  76. }
  77. }