StringBase.php 4.0 KB

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