class StringBase
Same name in other branches
- 9 core/modules/locale/src/StringBase.php \Drupal\locale\StringBase
- 8.9.x core/modules/locale/src/StringBase.php \Drupal\locale\StringBase
- 10 core/modules/locale/src/StringBase.php \Drupal\locale\StringBase
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.
Hierarchy
- class \Drupal\locale\StringBase implements \Drupal\locale\StringInterface
Expanded class hierarchy of StringBase
File
-
core/
modules/ locale/ src/ StringBase.php, line 13
Namespace
Drupal\localeView source
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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
StringBase::$context | public | property | The string context. | ||
StringBase::$lid | public | property | The string identifier. | ||
StringBase::$locations | public | property | The string locations indexed by type. | ||
StringBase::$source | public | property | The source string. | ||
StringBase::$storage | protected | property | The locale storage this string comes from or is to be saved to. | ||
StringBase::$version | public | property | The string version. | ||
StringBase::addLocation | public | function | Adds a location for this string. | Overrides StringInterface::addLocation | |
StringBase::delete | public | function | Deletes string object from storage. | Overrides StringInterface::delete | 1 |
StringBase::getId | public | function | Gets the string unique identifier. | Overrides StringInterface::getId | |
StringBase::getLocations | public | function | Gets location information for this string. | Overrides StringInterface::getLocations | |
StringBase::getPlurals | public | function | Splits string to work with plural values. | Overrides StringInterface::getPlurals | |
StringBase::getStorage | public | function | Gets the string storage. | Overrides StringInterface::getStorage | |
StringBase::getValues | public | function | Gets field values that are set for given field names. | Overrides StringInterface::getValues | |
StringBase::getVersion | public | function | Gets the string version. | Overrides StringInterface::getVersion | |
StringBase::hasLocation | public | function | Checks whether the string has a given location. | Overrides StringInterface::hasLocation | |
StringBase::save | public | function | Saves string object to storage. | Overrides StringInterface::save | 1 |
StringBase::setId | public | function | Sets the string unique identifier. | Overrides StringInterface::setId | |
StringBase::setPlurals | public | function | Sets this string using array of plural values. | Overrides StringInterface::setPlurals | |
StringBase::setStorage | public | function | Sets the string storage. | Overrides StringInterface::setStorage | |
StringBase::setValues | public | function | Sets an array of values as object properties. | Overrides StringInterface::setValues | |
StringBase::setVersion | public | function | Sets the string version. | Overrides StringInterface::setVersion | |
StringBase::__construct | public | function | Constructs a new locale string object. | 1 | |
StringInterface::getString | public | function | Gets plain string contained in this object. | 2 | |
StringInterface::isNew | public | function | Checks whether the object is not saved to storage yet. | 2 | |
StringInterface::isSource | public | function | Checks whether the object is a source string. | 2 | |
StringInterface::isTranslation | public | function | Checks whether the object is a translation string. | 2 | |
StringInterface::setString | public | function | Sets the string contained in this object. | 2 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.