Same filename and directory in other branches
- 8.9.x core/modules/locale/src/StringBase.php
- 9 core/modules/locale/src/StringBase.php
Namespace
Drupal\locale
File
core/modules/locale/src/StringBase.php
View source
<?php
namespace Drupal\locale;
use Drupal\Component\Gettext\PoItem;
abstract class StringBase implements StringInterface {
public $lid;
public $locations;
public $source;
public $context;
public $version;
protected $storage;
public function __construct($values = []) {
$this
->setValues((array) $values);
}
public function getId() {
return $this->lid ?? NULL;
}
public function setId($lid) {
$this->lid = $lid;
return $this;
}
public function getVersion() {
return $this->version ?? NULL;
}
public function setVersion($version) {
$this->version = $version;
return $this;
}
public function getPlurals() {
return explode(PoItem::DELIMITER, $this
->getString());
}
public function setPlurals($plurals) {
$this
->setString(implode(PoItem::DELIMITER, $plurals));
return $this;
}
public function getStorage() {
return $this->storage ?? NULL;
}
public function setStorage($storage) {
$this->storage = $storage;
return $this;
}
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;
}
public function getValues(array $fields) {
$values = [];
foreach ($fields as $field) {
if (isset($this->{$field})) {
$values[$field] = $this->{$field};
}
}
return $values;
}
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 ?? [];
}
public function addLocation($type, $name) {
$this->locations[$type][$name] = TRUE;
return $this;
}
public function hasLocation($type, $name) {
$locations = $this
->getLocations();
return isset($locations[$type]) ? !empty($locations[$type][$name]) : FALSE;
}
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;
}
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;
}
}
Classes