ComposerProperty.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. * @return mixed
  36. */
  37. public function get($key, $default = null)
  38. {
  39. return Arr::get($this->attributes, $key, $default);
  40. }
  41. /**
  42. * @param $key
  43. * @param $val
  44. * @return $this
  45. */
  46. public function set($key, $val)
  47. {
  48. $new = $this->attributes;
  49. Arr::set($new, $key, $val);
  50. return new static($new);
  51. }
  52. /**
  53. * @param $key
  54. * @return $this
  55. */
  56. public function delete($key)
  57. {
  58. $new = $this->attributes;
  59. Arr::forget($new, $key);
  60. return new static($new);
  61. }
  62. /**
  63. * @param $name
  64. * @return mixed
  65. */
  66. public function __get($name)
  67. {
  68. return $this->get(str_replace('_', '-', $name));
  69. }
  70. public function toArray()
  71. {
  72. return $this->attributes;
  73. }
  74. public function toJson()
  75. {
  76. return json_encode($this->toArray());
  77. }
  78. }