DataInterface.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is a part of dflydev/dot-access-data.
  5. *
  6. * (c) Dragonfly Development Inc.
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Dflydev\DotAccessData;
  12. use Dflydev\DotAccessData\Exception\DataException;
  13. use Dflydev\DotAccessData\Exception\InvalidPathException;
  14. interface DataInterface
  15. {
  16. public const PRESERVE = 0;
  17. public const REPLACE = 1;
  18. public const MERGE = 2;
  19. /**
  20. * Append a value to a key (assumes key refers to an array value)
  21. *
  22. * If the key does not yet exist it will be created.
  23. * If the key references a non-array it's existing contents will be added into a new array before appending the new value.
  24. *
  25. * @param string $key
  26. * @param mixed $value
  27. *
  28. * @throws InvalidPathException if the given key is empty
  29. */
  30. public function append(string $key, $value = null): void;
  31. /**
  32. * Set a value for a key
  33. *
  34. * If the key does not yet exist it will be created.
  35. *
  36. * @param string $key
  37. * @param mixed $value
  38. *
  39. * @throws InvalidPathException if the given key is empty
  40. * @throws DataException if the given key does not target an array
  41. */
  42. public function set(string $key, $value = null): void;
  43. /**
  44. * Remove a key
  45. *
  46. * No exception will be thrown if the key does not exist
  47. *
  48. * @param string $key
  49. *
  50. * @throws InvalidPathException if the given key is empty
  51. */
  52. public function remove(string $key): void;
  53. /**
  54. * Get the raw value for a key
  55. *
  56. * If the key does not exist, an optional default value can be returned instead.
  57. * If no default is provided then an exception will be thrown instead.
  58. *
  59. * @param string $key
  60. * @param mixed $default
  61. *
  62. * @return mixed
  63. *
  64. * @throws InvalidPathException if the given key is empty
  65. * @throws InvalidPathException if the given key does not exist and no default value was given
  66. *
  67. * @psalm-mutation-free
  68. */
  69. public function get(string $key, $default = null);
  70. /**
  71. * Check if the key exists
  72. *
  73. * @param string $key
  74. *
  75. * @return bool
  76. *
  77. * @throws InvalidPathException if the given key is empty
  78. *
  79. * @psalm-mutation-free
  80. */
  81. public function has(string $key): bool;
  82. /**
  83. * Get a data instance for a key
  84. *
  85. * @param string $key
  86. *
  87. * @return DataInterface
  88. *
  89. * @throws InvalidPathException if the given key is empty
  90. * @throws DataException if the given key does not reference an array
  91. *
  92. * @psalm-mutation-free
  93. */
  94. public function getData(string $key): DataInterface;
  95. /**
  96. * Import data into existing data
  97. *
  98. * @param array<string, mixed> $data
  99. * @param self::PRESERVE|self::REPLACE|self::MERGE $mode
  100. */
  101. public function import(array $data, int $mode = self::REPLACE): void;
  102. /**
  103. * Import data from an external data into existing data
  104. *
  105. * @param DataInterface $data
  106. * @param self::PRESERVE|self::REPLACE|self::MERGE $mode
  107. */
  108. public function importData(DataInterface $data, int $mode = self::REPLACE): void;
  109. /**
  110. * Export data as raw data
  111. *
  112. * @return array<string, mixed>
  113. *
  114. * @psalm-mutation-free
  115. */
  116. public function export(): array;
  117. }