Backup of db before drupal security update
[yaffs-website] / web / core / modules / locale / src / StringBase.php
1 <?php
2
3 namespace Drupal\locale;
4
5 /**
6  * Defines the locale string base class.
7  *
8  * This is the base class to be used for locale string objects and contains
9  * the common properties and methods for source and translation strings.
10  */
11 abstract class StringBase implements StringInterface {
12   /**
13    * The string identifier.
14    *
15    * @var int
16    */
17   public $lid;
18
19   /**
20    * The string locations indexed by type.
21    *
22    * @var string
23    */
24   public $locations;
25
26   /**
27    * The source string.
28    *
29    * @var string
30    */
31   public $source;
32
33   /**
34    * The string context.
35    *
36    * @var string
37    */
38   public $context;
39
40   /**
41    * The string version.
42    *
43    * @var string
44    */
45   public $version;
46
47   /**
48    * The locale storage this string comes from or is to be saved to.
49    *
50    * @var \Drupal\locale\StringStorageInterface
51    */
52   protected $storage;
53
54   /**
55    * Constructs a new locale string object.
56    *
57    * @param object|array $values
58    *   Object or array with initial values.
59    */
60   public function __construct($values = []) {
61     $this->setValues((array) $values);
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function getId() {
68     return isset($this->lid) ? $this->lid : NULL;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function setId($lid) {
75     $this->lid = $lid;
76     return $this;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function getVersion() {
83     return isset($this->version) ? $this->version : NULL;
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function setVersion($version) {
90     $this->version = $version;
91     return $this;
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function getPlurals() {
98     return explode(LOCALE_PLURAL_DELIMITER, $this->getString());
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function setPlurals($plurals) {
105     $this->setString(implode(LOCALE_PLURAL_DELIMITER, $plurals));
106     return $this;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   public function getStorage() {
113     return isset($this->storage) ? $this->storage : NULL;
114   }
115
116   /**
117    * {@inheritdoc}
118    */
119   public function setStorage($storage) {
120     $this->storage = $storage;
121     return $this;
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function setValues(array $values, $override = TRUE) {
128     foreach ($values as $key => $value) {
129       if (property_exists($this, $key) && ($override || !isset($this->$key))) {
130         $this->$key = $value;
131       }
132     }
133     return $this;
134   }
135
136   /**
137    * {@inheritdoc}
138    */
139   public function getValues(array $fields) {
140     $values = [];
141     foreach ($fields as $field) {
142       if (isset($this->$field)) {
143         $values[$field] = $this->$field;
144       }
145     }
146     return $values;
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function getLocations($check_only = FALSE) {
153     if (!isset($this->locations) && !$check_only) {
154       $this->locations = [];
155       foreach ($this->getStorage()->getLocations(['sid' => $this->getId()]) as $location) {
156         $this->locations[$location->type][$location->name] = $location->lid;
157       }
158     }
159     return isset($this->locations) ? $this->locations : [];
160   }
161
162   /**
163    * {@inheritdoc}
164    */
165   public function addLocation($type, $name) {
166     $this->locations[$type][$name] = TRUE;
167     return $this;
168   }
169
170   /**
171    * {@inheritdoc}
172    */
173   public function hasLocation($type, $name) {
174     $locations = $this->getLocations();
175     return isset($locations[$type]) ? !empty($locations[$type][$name]) : FALSE;
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   public function save() {
182     if ($storage = $this->getStorage()) {
183       $storage->save($this);
184     }
185     else {
186       throw new StringStorageException('The string cannot be saved because its not bound to a storage: ' . $this->getString());
187     }
188     return $this;
189   }
190
191   /**
192    * {@inheritdoc}
193    */
194   public function delete() {
195     if (!$this->isNew()) {
196       if ($storage = $this->getStorage()) {
197         $storage->delete($this);
198       }
199       else {
200         throw new StringStorageException('The string cannot be deleted because its not bound to a storage: ' . $this->getString());
201       }
202     }
203     return $this;
204   }
205
206 }