StringBase.php 3.7 KB

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