StringBase.php 4.0 KB

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