I18nValue.php 1.6 KB

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