|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\SerializableClosure; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use Laravel\SerializableClosure\Exceptions\PhpVersionNotSupportedException; |
| 7 | + |
| 8 | +class UnsignedSerializableClosure |
| 9 | +{ |
| 10 | + /** |
| 11 | + * The closure's serializable. |
| 12 | + * |
| 13 | + * @var \Laravel\SerializableClosure\Contracts\Serializable |
| 14 | + */ |
| 15 | + protected $serializable; |
| 16 | + |
| 17 | + /** |
| 18 | + * Creates a new serializable closure instance. |
| 19 | + * |
| 20 | + * @param \Closure $closure |
| 21 | + * @return void |
| 22 | + */ |
| 23 | + public function __construct(Closure $closure) |
| 24 | + { |
| 25 | + if (\PHP_VERSION_ID < 70400) { |
| 26 | + throw new PhpVersionNotSupportedException(); |
| 27 | + } |
| 28 | + |
| 29 | + $this->serializable = new Serializers\Native($closure); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Resolve the closure with the given arguments. |
| 34 | + * |
| 35 | + * @return mixed |
| 36 | + */ |
| 37 | + public function __invoke() |
| 38 | + { |
| 39 | + if (\PHP_VERSION_ID < 70400) { |
| 40 | + throw new PhpVersionNotSupportedException(); |
| 41 | + } |
| 42 | + |
| 43 | + return call_user_func_array($this->serializable, func_get_args()); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Gets the closure. |
| 48 | + * |
| 49 | + * @return \Closure |
| 50 | + */ |
| 51 | + public function getClosure() |
| 52 | + { |
| 53 | + if (\PHP_VERSION_ID < 70400) { |
| 54 | + throw new PhpVersionNotSupportedException(); |
| 55 | + } |
| 56 | + |
| 57 | + return $this->serializable->getClosure(); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the serializable representation of the closure. |
| 62 | + * |
| 63 | + * @return array |
| 64 | + */ |
| 65 | + public function __serialize() |
| 66 | + { |
| 67 | + return [ |
| 68 | + 'serializable' => $this->serializable, |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Restore the closure after serialization. |
| 74 | + * |
| 75 | + * @param array $data |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + public function __unserialize($data) |
| 79 | + { |
| 80 | + $this->serializable = $data['serializable']; |
| 81 | + } |
| 82 | +} |
0 commit comments