Value.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. declare(strict_types=1);
  3. namespace Dotenv\Parser;
  4. use Dotenv\Util\Str;
  5. final class Value
  6. {
  7. /**
  8. * The string representation of the parsed value.
  9. *
  10. * @var string
  11. */
  12. private $chars;
  13. /**
  14. * The locations of the variables in the value.
  15. *
  16. * @var int[]
  17. */
  18. private $vars;
  19. /**
  20. * Internal constructor for a value.
  21. *
  22. * @param string $chars
  23. * @param int[] $vars
  24. *
  25. * @return void
  26. */
  27. private function __construct(string $chars, array $vars)
  28. {
  29. $this->chars = $chars;
  30. $this->vars = $vars;
  31. }
  32. /**
  33. * Create an empty value instance.
  34. *
  35. * @return \Dotenv\Parser\Value
  36. */
  37. public static function blank()
  38. {
  39. return new self('', []);
  40. }
  41. /**
  42. * Create a new value instance, appending the characters.
  43. *
  44. * @param string $chars
  45. * @param bool $var
  46. *
  47. * @return \Dotenv\Parser\Value
  48. */
  49. public function append(string $chars, bool $var)
  50. {
  51. return new self(
  52. $this->chars.$chars,
  53. $var ? \array_merge($this->vars, [Str::len($this->chars)]) : $this->vars
  54. );
  55. }
  56. /**
  57. * Get the string representation of the parsed value.
  58. *
  59. * @return string
  60. */
  61. public function getChars()
  62. {
  63. return $this->chars;
  64. }
  65. /**
  66. * Get the locations of the variables in the value.
  67. *
  68. * @return int[]
  69. */
  70. public function getVars()
  71. {
  72. $vars = $this->vars;
  73. \rsort($vars);
  74. return $vars;
  75. }
  76. }