StringBase.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 parent string identifier for plural translations.
  21. *
  22. * @var integer
  23. */
  24. public $plid;
  25. /**
  26. * Plural index in case of plural string.
  27. *
  28. * @var integer
  29. */
  30. public $plural;
  31. /**
  32. * The string locations indexed by type.
  33. *
  34. * @var string
  35. */
  36. public $locations;
  37. /**
  38. * The source string.
  39. *
  40. * @var string
  41. */
  42. public $source;
  43. /**
  44. * The string context.
  45. *
  46. * @var string
  47. */
  48. public $context;
  49. /**
  50. * The string group.
  51. *
  52. * @var string
  53. */
  54. public $textgroup;
  55. /**
  56. * The string version.
  57. *
  58. * @var string
  59. */
  60. public $version;
  61. /**
  62. * The locale storage this string comes from or is to be saved to.
  63. *
  64. * @var StringStorageInterface
  65. */
  66. protected $storage;
  67. /**
  68. * Constructs a new locale string object.
  69. *
  70. * @param object|array $values
  71. * Object or array with initial values.
  72. */
  73. public function __construct($values = array()) {
  74. $this->setValues((array) $values);
  75. }
  76. /**
  77. * Implements StringInterface::getId().
  78. */
  79. public function getId() {
  80. return isset($this->lid) ? $this->lid : NULL;
  81. }
  82. /**
  83. * Implements StringInterface::setId().
  84. */
  85. public function setId($lid) {
  86. $this->lid = $lid;
  87. return $this;
  88. }
  89. /**
  90. * Implements StringInterface::getParentId().
  91. */
  92. public function getParentId() {
  93. return isset($this->plid) ? $this->plid : 0;
  94. }
  95. /**
  96. * Implements StringInterface::setParentId().
  97. */
  98. public function setParentId($plid) {
  99. $this->plid = $plid;
  100. return $this;
  101. }
  102. /**
  103. * Implements StringInterface::getVersion().
  104. */
  105. public function getVersion() {
  106. return isset($this->version) ? $this->version : NULL;
  107. }
  108. /**
  109. * Implements StringInterface::setVersion().
  110. */
  111. public function setVersion($version) {
  112. $this->version = $version;
  113. return $this;
  114. }
  115. /**
  116. * Implements StringInterface::getStorage().
  117. */
  118. public function getStorage() {
  119. return isset($this->storage) ? $this->storage : NULL;
  120. }
  121. /**
  122. * Implements StringInterface::setStorage().
  123. */
  124. public function setStorage(StringStorageInterface $storage) {
  125. $this->storage = $storage;
  126. return $this;
  127. }
  128. /**
  129. * Implements StringInterface::setValues().
  130. */
  131. public function setValues(array $values, $override = TRUE) {
  132. foreach ($values as $key => $value) {
  133. if (property_exists($this, $key) && ($override || !isset($this->$key))) {
  134. $this->$key = $value;
  135. }
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Implements StringInterface::getValues().
  141. */
  142. public function getValues(array $fields) {
  143. $values = array();
  144. foreach ($fields as $field) {
  145. if (isset($this->$field)) {
  146. $values[$field] = $this->$field;
  147. }
  148. }
  149. return $values;
  150. }
  151. /**
  152. * Implements StringInterface::getTextgroup().
  153. */
  154. public function getTextgroup() {
  155. return empty($this->textgroup) ? 'default' : $this->textgroup;
  156. }
  157. /**
  158. * Implements StringInterface::setTextgroup().
  159. */
  160. public function setTextgroup($textgroup) {
  161. $this->textgroup = $textgroup;
  162. }
  163. /**
  164. * Implements LocaleString::save().
  165. */
  166. public function save() {
  167. if ($storage = $this->getStorage()) {
  168. $storage->save($this);
  169. }
  170. else {
  171. throw new StringStorageException(format_string('The string cannot be saved because its not bound to a storage: @string', array(
  172. '@string' => $this->getString(),
  173. )));
  174. }
  175. return $this;
  176. }
  177. /**
  178. * Implements LocaleString::delete().
  179. */
  180. public function delete() {
  181. if (!$this->isNew()) {
  182. if ($storage = $this->getStorage()) {
  183. $storage->delete($this);
  184. }
  185. else {
  186. throw new StringStorageException(format_string('The string cannot be deleted because its not bound to a storage: @string', array(
  187. '@string' => $this->getString(),
  188. )));
  189. }
  190. }
  191. return $this;
  192. }
  193. }