summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/locale/src/StringBase.php
blob: 832d1965b0fa51ce38d08a48915fc551b4dd2270 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php

namespace Drupal\locale;

use Drupal\Component\Gettext\PoItem;

/**
 * Defines the locale string base class.
 *
 * This is the base class to be used for locale string objects and contains
 * the common properties and methods for source and translation strings.
 */
abstract class StringBase implements StringInterface {
  /**
   * The string identifier.
   *
   * @var int
   */
  public $lid;

  /**
   * The string locations indexed by type.
   *
   * @var string
   */
  public $locations;

  /**
   * The source string.
   *
   * @var string
   */
  public $source;

  /**
   * The string context.
   *
   * @var string
   */
  public $context;

  /**
   * The string version.
   *
   * @var string
   */
  public $version;

  /**
   * The locale storage this string comes from or is to be saved to.
   *
   * @var \Drupal\locale\StringStorageInterface
   */
  protected $storage;

  /**
   * Constructs a new locale string object.
   *
   * @param object|array $values
   *   Object or array with initial values.
   */
  public function __construct($values = []) {
    $this->setValues((array) $values);
  }

  /**
   * {@inheritdoc}
   */
  public function getId() {
    return $this->lid ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setId($lid) {
    $this->lid = $lid;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getVersion() {
    return $this->version ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setVersion($version) {
    $this->version = $version;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getPlurals() {
    return explode(PoItem::DELIMITER, $this->getString());
  }

  /**
   * {@inheritdoc}
   */
  public function setPlurals($plurals) {
    $this->setString(implode(PoItem::DELIMITER, $plurals));
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getStorage() {
    return $this->storage ?? NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function setStorage($storage) {
    $this->storage = $storage;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function setValues(array $values, $override = TRUE) {
    foreach ($values as $key => $value) {
      if (property_exists($this, $key) && ($override || !isset($this->$key))) {
        $this->$key = $value;
      }
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getValues(array $fields) {
    $values = [];
    foreach ($fields as $field) {
      if (isset($this->$field)) {
        $values[$field] = $this->$field;
      }
    }
    return $values;
  }

  /**
   * {@inheritdoc}
   */
  public function getLocations($check_only = FALSE) {
    if (!isset($this->locations) && !$check_only) {
      $this->locations = [];
      foreach ($this->getStorage()->getLocations(['sid' => $this->getId()]) as $location) {
        $this->locations[$location->type][$location->name] = $location->lid;
      }
    }
    return $this->locations ?? [];
  }

  /**
   * {@inheritdoc}
   */
  public function addLocation($type, $name) {
    $this->locations[$type][$name] = TRUE;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function hasLocation($type, $name) {
    $locations = $this->getLocations();
    return isset($locations[$type]) ? !empty($locations[$type][$name]) : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function save() {
    if ($storage = $this->getStorage()) {
      $storage->save($this);
    }
    else {
      throw new StringStorageException('The string cannot be saved because its not bound to a storage: ' . $this->getString());
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function delete() {
    if (!$this->isNew()) {
      if ($storage = $this->getStorage()) {
        $storage->delete($this);
      }
      else {
        throw new StringStorageException('The string cannot be deleted because its not bound to a storage: ' . $this->getString());
      }
    }
    return $this;
  }

}