Data.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace Grav\Common\Data;
  3. use RocketTheme\Toolbox\ArrayTraits\ArrayAccessWithGetters;
  4. use RocketTheme\Toolbox\ArrayTraits\Countable;
  5. use RocketTheme\Toolbox\ArrayTraits\Export;
  6. use RocketTheme\Toolbox\File\File;
  7. use RocketTheme\Toolbox\File\FileInterface;
  8. /**
  9. * Recursive data object
  10. *
  11. * @author RocketTheme
  12. * @license MIT
  13. */
  14. class Data implements DataInterface
  15. {
  16. use ArrayAccessWithGetters, Countable, Export, DataMutatorTrait;
  17. protected $gettersVariable = 'items';
  18. protected $items;
  19. /**
  20. * @var Blueprints
  21. */
  22. protected $blueprints;
  23. /**
  24. * @var File
  25. */
  26. protected $storage;
  27. /**
  28. * @param array $items
  29. * @param Blueprint $blueprints
  30. */
  31. public function __construct(array $items = array(), Blueprint $blueprints = null)
  32. {
  33. $this->items = $items;
  34. $this->blueprints = $blueprints;
  35. }
  36. /**
  37. * Get value by using dot notation for nested arrays/objects.
  38. *
  39. * @example $value = $data->value('this.is.my.nested.variable');
  40. *
  41. * @param string $name Dot separated path to the requested value.
  42. * @param mixed $default Default value (or null).
  43. * @param string $separator Separator, defaults to '.'
  44. * @return mixed Value.
  45. */
  46. public function value($name, $default = null, $separator = '.')
  47. {
  48. return $this->get($name, $default, $separator);
  49. }
  50. /**
  51. * Set default value by using dot notation for nested arrays/objects.
  52. *
  53. * @example $data->def('this.is.my.nested.variable', 'default');
  54. *
  55. * @param string $name Dot separated path to the requested value.
  56. * @param mixed $default Default value (or null).
  57. * @param string $separator Separator, defaults to '.'
  58. */
  59. public function def($name, $default = null, $separator = '.')
  60. {
  61. $this->set($name, $this->get($name, $default, $separator), $separator);
  62. }
  63. /**
  64. * Join two values together by using blueprints if available.
  65. *
  66. * @param string $name Dot separated path to the requested value.
  67. * @param mixed $value Value to be joined.
  68. * @param string $separator Separator, defaults to '.'
  69. */
  70. public function join($name, $value, $separator = '.')
  71. {
  72. $old = $this->get($name, null, $separator);
  73. if ($old === null) {
  74. // Variable does not exist yet: just use the incoming value.
  75. } elseif ($this->blueprints) {
  76. // Blueprints: join values by using blueprints.
  77. $value = $this->blueprints->mergeData($old, $value, $name, $separator);
  78. } else {
  79. // No blueprints: replace existing top level variables with the new ones.
  80. $value = array_merge($old, $value);
  81. }
  82. $this->set($name, $value, $separator);
  83. }
  84. /**
  85. * Join two values together by using blueprints if available.
  86. *
  87. * @param string $name Dot separated path to the requested value.
  88. * @param mixed $value Value to be joined.
  89. * @param string $separator Separator, defaults to '.'
  90. */
  91. public function joinDefaults($name, $value, $separator = '.')
  92. {
  93. $old = $this->get($name, null, $separator);
  94. if ($old === null) {
  95. // Variable does not exist yet: just use the incoming value.
  96. } elseif ($this->blueprints) {
  97. // Blueprints: join values by using blueprints.
  98. $value = $this->blueprints->mergeData($value, $old, $name, $separator);
  99. } else {
  100. // No blueprints: replace existing top level variables with the new ones.
  101. $value = array_merge($value, $old);
  102. }
  103. $this->set($name, $value, $separator);
  104. }
  105. /**
  106. * Merge two sets of data together.
  107. *
  108. * @param array $data
  109. * @return void
  110. */
  111. public function merge(array $data)
  112. {
  113. if ($this->blueprints) {
  114. $this->items = $this->blueprints->mergeData($this->items, $data);
  115. } else {
  116. $this->items = array_merge($this->items, $data);
  117. }
  118. }
  119. /**
  120. * Add default data to the set.
  121. *
  122. * @param array $data
  123. * @return void
  124. */
  125. public function setDefaults(array $data)
  126. {
  127. if ($this->blueprints) {
  128. $this->items = $this->blueprints->mergeData($data, $this->items);
  129. } else {
  130. $this->items = array_merge($data, $this->items);
  131. }
  132. }
  133. /**
  134. * Return blueprints.
  135. *
  136. * @return Blueprint
  137. */
  138. public function blueprints()
  139. {
  140. return $this->blueprints;
  141. }
  142. /**
  143. * Validate by blueprints.
  144. *
  145. * @throws \Exception
  146. */
  147. public function validate()
  148. {
  149. if ($this->blueprints) {
  150. $this->blueprints->validate($this->items);
  151. }
  152. }
  153. /**
  154. * Filter all items by using blueprints.
  155. */
  156. public function filter()
  157. {
  158. if ($this->blueprints) {
  159. $this->items = $this->blueprints->filter($this->items);
  160. }
  161. }
  162. /**
  163. * Get extra items which haven't been defined in blueprints.
  164. *
  165. * @return array
  166. */
  167. public function extra()
  168. {
  169. return $this->blueprints ? $this->blueprints->extra($this->items) : array();
  170. }
  171. /**
  172. * Save data if storage has been defined.
  173. */
  174. public function save()
  175. {
  176. $file = $this->file();
  177. if ($file) {
  178. $file->save($this->items);
  179. }
  180. }
  181. /**
  182. * Returns whether the data already exists in the storage.
  183. *
  184. * NOTE: This method does not check if the data is current.
  185. *
  186. * @return bool
  187. */
  188. public function exists()
  189. {
  190. return $this->file()->exists();
  191. }
  192. /**
  193. * Return unmodified data as raw string.
  194. *
  195. * NOTE: This function only returns data which has been saved to the storage.
  196. *
  197. * @return string
  198. */
  199. public function raw()
  200. {
  201. return $this->file()->raw();
  202. }
  203. /**
  204. * Set or get the data storage.
  205. *
  206. * @param FileInterface $storage Optionally enter a new storage.
  207. * @return FileInterface
  208. */
  209. public function file(FileInterface $storage = null)
  210. {
  211. if ($storage) {
  212. $this->storage = $storage;
  213. }
  214. return $this->storage;
  215. }
  216. }