MacroableClass.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of the Carbon package.
  5. *
  6. * (c) Brian Nesbitt <brian@nesbot.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Tests\CarbonPeriod\Fixtures;
  12. use BadMethodCallException;
  13. use Carbon\Traits\Macro;
  14. use Closure;
  15. class MacroableClass
  16. {
  17. use Macro;
  18. public $endDate;
  19. protected $localMacros = [];
  20. public function getEndDate()
  21. {
  22. return $this->endDate;
  23. }
  24. public function setEndDate($endDate)
  25. {
  26. $this->endDate = $endDate;
  27. return $this;
  28. }
  29. public function cast($className)
  30. {
  31. $new = new $className();
  32. return $new->setEndDate($this->endDate);
  33. }
  34. public function __call($method, $parameters)
  35. {
  36. $macro = static::$globalMacros[$method] ?? null;
  37. if ($macro instanceof Closure) {
  38. return $macro->call($this);
  39. }
  40. throw new BadMethodCallException("$method not found");
  41. }
  42. }