Security update for Core, with self-updated composer
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldType / MapItem.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Field\FieldItemBase;
7
8 /**
9  * Defines the 'map' entity field type.
10  *
11  * @FieldType(
12  *   id = "map",
13  *   label = @Translation("Map"),
14  *   description = @Translation("An entity field for storing a serialized array of values."),
15  *   no_ui = TRUE,
16  *   list_class = "\Drupal\Core\Field\MapFieldItemList",
17  * )
18  */
19 class MapItem extends FieldItemBase {
20
21   /**
22    * {@inheritdoc}
23    */
24   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
25     // The properties are dynamic and can not be defined statically.
26     return [];
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public static function schema(FieldStorageDefinitionInterface $field_definition) {
33     return [
34       'columns' => [
35         'value' => [
36           'type' => 'blob',
37           'size' => 'big',
38           'serialize' => TRUE,
39         ],
40       ],
41     ];
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function toArray() {
48     // The default implementation of toArray() only returns known properties.
49     // For a map, return everything as the properties are not pre-defined.
50     return $this->getValue();
51   }
52
53   /**
54    * {@inheritdoc}
55    */
56   public function setValue($values, $notify = TRUE) {
57     $this->values = [];
58     if (!isset($values)) {
59       return;
60     }
61
62     if (!is_array($values)) {
63       if ($values instanceof MapItem) {
64         $values = $values->getValue();
65       }
66       else {
67         $values = unserialize($values);
68       }
69     }
70
71     $this->values = $values;
72
73     // Notify the parent of any changes.
74     if ($notify && isset($this->parent)) {
75       $this->parent->onChange($this->name);
76     }
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function __get($name) {
83     if (!isset($this->values[$name])) {
84       $this->values[$name] = [];
85     }
86
87     return $this->values[$name];
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function __set($name, $value) {
94     if (isset($value)) {
95       $this->values[$name] = $value;
96     }
97     else {
98       unset($this->values[$name]);
99     }
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   public static function mainPropertyName() {
106     // A map item has no main property.
107     return NULL;
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   public function isEmpty() {
114     return empty($this->values);
115   }
116
117 }