Carbon.php 998B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Illuminate\Support;
  3. use JsonSerializable;
  4. use Carbon\Carbon as BaseCarbon;
  5. use Illuminate\Support\Traits\Macroable;
  6. class Carbon extends BaseCarbon implements JsonSerializable
  7. {
  8. use Macroable;
  9. /**
  10. * The custom Carbon JSON serializer.
  11. *
  12. * @var callable|null
  13. */
  14. protected static $serializer;
  15. /**
  16. * Prepare the object for JSON serialization.
  17. *
  18. * @return array|string
  19. */
  20. public function jsonSerialize()
  21. {
  22. if (static::$serializer) {
  23. return call_user_func(static::$serializer, $this);
  24. }
  25. $carbon = $this;
  26. return call_user_func(function () use ($carbon) {
  27. return get_object_vars($carbon);
  28. });
  29. }
  30. /**
  31. * JSON serialize all Carbon instances using the given callback.
  32. *
  33. * @param callable $callback
  34. * @return void
  35. */
  36. public static function serializeUsing($callback)
  37. {
  38. static::$serializer = $callback;
  39. }
  40. }