Context.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Dcat\Admin\Support;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Fluent;
  5. /**
  6. * Class Context.
  7. *
  8. * @property string $favicon
  9. * @property string $metaTitle
  10. * @property string $pjaxContainerId
  11. * @property array|null $html
  12. * @property array|null $ignoreQueries
  13. * @property array|null $jsVariables
  14. */
  15. class Context extends Fluent
  16. {
  17. public function set($key, $value = null)
  18. {
  19. $data = is_array($key) ? $key : [$key => $value];
  20. foreach ($data as $key => $value) {
  21. Arr::set($this->attributes, $key, $value);
  22. }
  23. return $this;
  24. }
  25. public function get($key, $default = null)
  26. {
  27. return Arr::get($this->attributes, $key, $default);
  28. }
  29. public function getArray($key, $default = null)
  30. {
  31. return Helper::array($this->get($key, $default));
  32. }
  33. public function add($key, $value, $k = null)
  34. {
  35. $results = $this->getArray($key);
  36. if ($k !== null) {
  37. $results[] = $value;
  38. } else {
  39. $results[$k] = $value;
  40. }
  41. return $this->set($key, $results);
  42. }
  43. public function addMany($key, array $value)
  44. {
  45. $results = $this->getArray($key);
  46. return $this->set($key, array_merge($results, $value));
  47. }
  48. public function forget($keys)
  49. {
  50. Arr::forget($this->attributes, $keys);
  51. }
  52. public function flush()
  53. {
  54. $this->attributes = [];
  55. }
  56. }