ComposerProperty.php 1.7 KB

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