3 namespace Drupal\locale;
6 * Defines the locale string base class.
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.
11 abstract class StringBase implements StringInterface {
13 * The string identifier.
20 * The string locations indexed by type.
48 * The locale storage this string comes from or is to be saved to.
50 * @var \Drupal\locale\StringStorageInterface
55 * Constructs a new locale string object.
57 * @param object|array $values
58 * Object or array with initial values.
60 public function __construct($values = []) {
61 $this->setValues((array) $values);
67 public function getId() {
68 return isset($this->lid) ? $this->lid : NULL;
74 public function setId($lid) {
82 public function getVersion() {
83 return isset($this->version) ? $this->version : NULL;
89 public function setVersion($version) {
90 $this->version = $version;
97 public function getPlurals() {
98 return explode(LOCALE_PLURAL_DELIMITER, $this->getString());
104 public function setPlurals($plurals) {
105 $this->setString(implode(LOCALE_PLURAL_DELIMITER, $plurals));
112 public function getStorage() {
113 return isset($this->storage) ? $this->storage : NULL;
119 public function setStorage($storage) {
120 $this->storage = $storage;
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;
139 public function getValues(array $fields) {
141 foreach ($fields as $field) {
142 if (isset($this->$field)) {
143 $values[$field] = $this->$field;
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;
159 return isset($this->locations) ? $this->locations : [];
165 public function addLocation($type, $name) {
166 $this->locations[$type][$name] = TRUE;
173 public function hasLocation($type, $name) {
174 $locations = $this->getLocations();
175 return isset($locations[$type]) ? !empty($locations[$type][$name]) : FALSE;
181 public function save() {
182 if ($storage = $this->getStorage()) {
183 $storage->save($this);
186 throw new StringStorageException('The string cannot be saved because its not bound to a storage: ' . $this->getString());
194 public function delete() {
195 if (!$this->isNew()) {
196 if ($storage = $this->getStorage()) {
197 $storage->delete($this);
200 throw new StringStorageException('The string cannot be deleted because its not bound to a storage: ' . $this->getString());