Attribute.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Illuminate\Database\Eloquent\Casts;
  3. class Attribute
  4. {
  5. /**
  6. * The attribute accessor.
  7. *
  8. * @var callable
  9. */
  10. public $get;
  11. /**
  12. * The attribute mutator.
  13. *
  14. * @var callable
  15. */
  16. public $set;
  17. /**
  18. * Indicates if caching is enabled for this attribute.
  19. *
  20. * @var bool
  21. */
  22. public $withCaching = false;
  23. /**
  24. * Indicates if caching of objects is enabled for this attribute.
  25. *
  26. * @var bool
  27. */
  28. public $withObjectCaching = true;
  29. /**
  30. * Create a new attribute accessor / mutator.
  31. *
  32. * @param callable|null $get
  33. * @param callable|null $set
  34. * @return void
  35. */
  36. public function __construct(?callable $get = null, ?callable $set = null)
  37. {
  38. $this->get = $get;
  39. $this->set = $set;
  40. }
  41. /**
  42. * Create a new attribute accessor / mutator.
  43. *
  44. * @param callable|null $get
  45. * @param callable|null $set
  46. * @return static
  47. */
  48. public static function make(?callable $get = null, ?callable $set = null): static
  49. {
  50. return new static($get, $set);
  51. }
  52. /**
  53. * Create a new attribute accessor.
  54. *
  55. * @param callable $get
  56. * @return static
  57. */
  58. public static function get(callable $get)
  59. {
  60. return new static($get);
  61. }
  62. /**
  63. * Create a new attribute mutator.
  64. *
  65. * @param callable $set
  66. * @return static
  67. */
  68. public static function set(callable $set)
  69. {
  70. return new static(null, $set);
  71. }
  72. /**
  73. * Disable object caching for the attribute.
  74. *
  75. * @return static
  76. */
  77. public function withoutObjectCaching()
  78. {
  79. $this->withObjectCaching = false;
  80. return $this;
  81. }
  82. /**
  83. * Enable caching for the attribute.
  84. *
  85. * @return static
  86. */
  87. public function shouldCache()
  88. {
  89. $this->withCaching = true;
  90. return $this;
  91. }
  92. }