I18nValue.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. class I18nValue {
  3. private const STATE_DIRTY = 'dirty';
  4. public const STATE_IGNORE = 'ignore';
  5. private const STATE_TODO = 'todo';
  6. private const STATES = [
  7. self::STATE_DIRTY,
  8. self::STATE_IGNORE,
  9. self::STATE_TODO,
  10. ];
  11. /** @var string */
  12. private $value;
  13. /** @var string|null */
  14. private $state;
  15. public function __construct(string $data) {
  16. $data = explode(' -> ', $data);
  17. $this->value = array_shift($data);
  18. if (count($data) === 0) {
  19. return;
  20. }
  21. $state = array_shift($data);
  22. if (in_array($state, self::STATES, true)) {
  23. $this->state = $state;
  24. }
  25. }
  26. public function __clone() {
  27. $this->markAsTodo();
  28. }
  29. public function equal(I18nValue $value): bool {
  30. return $this->value === $value->getValue();
  31. }
  32. public function isIgnore(): bool {
  33. return $this->state === self::STATE_IGNORE;
  34. }
  35. public function isTodo(): bool {
  36. return $this->state === self::STATE_TODO;
  37. }
  38. public function markAsDirty(): void {
  39. $this->state = self::STATE_DIRTY;
  40. }
  41. public function markAsIgnore(): void {
  42. $this->state = self::STATE_IGNORE;
  43. }
  44. public function markAsTodo(): void {
  45. $this->state = self::STATE_TODO;
  46. }
  47. public function unmarkAsIgnore(): void {
  48. if ($this->state === self::STATE_IGNORE) {
  49. $this->state = null;
  50. }
  51. }
  52. public function __toString(): string {
  53. if ($this->state === null) {
  54. return $this->value;
  55. }
  56. return "{$this->value} -> {$this->state}";
  57. }
  58. public function getValue(): string {
  59. return $this->value;
  60. }
  61. }