ComposerProperty.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace Dcat\Admin\Support;
  3. use Illuminate\Contracts\Support\Arrayable;
  4. use Illuminate\Support\Arr;
  5. /**
  6. * @property string $name
  7. * @property string $description
  8. * @property string $type
  9. * @property array $keywords
  10. * @property string $homepage
  11. * @property string $license
  12. * @property array $authors
  13. * @property array $require
  14. * @property array $require_dev
  15. * @property array $suggest
  16. * @property array $autoload
  17. * @property array $autoload_dev
  18. * @property array $scripts
  19. * @property array $extra
  20. * @property string $version
  21. */
  22. class ComposerProperty implements Arrayable
  23. {
  24. /**
  25. * @var array
  26. */
  27. protected $attributes = [];
  28. public function __construct(array $attributes = [])
  29. {
  30. $this->attributes = $attributes;
  31. }
  32. /**
  33. * @param $key
  34. * @param null $default
  35. *
  36. * @return mixed
  37. */
  38. public function get($key, $default = null)
  39. {
  40. return Arr::get($this->attributes, $key, $default);
  41. }
  42. /**
  43. * @param $key
  44. * @param $val
  45. *
  46. * @return $this
  47. */
  48. public function set($key, $val)
  49. {
  50. $new = $this->attributes;
  51. Arr::set($new, $key, $val);
  52. return new static($new);
  53. }
  54. /**
  55. * @param $key
  56. *
  57. * @return $this
  58. */
  59. public function delete($key)
  60. {
  61. $new = $this->attributes;
  62. Arr::forget($new, $key);
  63. return new static($new);
  64. }
  65. /**
  66. * @param $name
  67. *
  68. * @return mixed
  69. */
  70. public function __get($name)
  71. {
  72. return $this->get(str_replace('_', '-', $name));
  73. }
  74. public function toArray()
  75. {
  76. return $this->attributes;
  77. }
  78. public function toJson()
  79. {
  80. return json_encode($this->toArray());
  81. }
  82. }