CURLStringFile.php
52 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.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 | |
| 12 | if (\PHP_VERSION_ID >= 70400 && extension_loaded('curl')) { |
| 13 | /** |
| 14 | * @property string $data |
| 15 | */ |
| 16 | class CURLStringFile extends CURLFile |
| 17 | { |
| 18 | private $data; |
| 19 | |
| 20 | public function __construct(string $data, string $postname, string $mime = 'application/octet-stream') |
| 21 | { |
| 22 | $this->data = $data; |
| 23 | parent::__construct('data://application/octet-stream;base64,'.base64_encode($data), $mime, $postname); |
| 24 | } |
| 25 | |
| 26 | public function __set(string $name, $value): void |
| 27 | { |
| 28 | if ('data' !== $name) { |
| 29 | $this->$name = $value; |
| 30 | |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | if (is_object($value) ? !method_exists($value, '__toString') : !is_scalar($value)) { |
| 35 | throw new TypeError('Cannot assign '.gettype($value).' to property CURLStringFile::$data of type string'); |
| 36 | } |
| 37 | |
| 38 | $this->name = 'data://application/octet-stream;base64,'.base64_encode($value); |
| 39 | } |
| 40 | |
| 41 | public function __isset(string $name): bool |
| 42 | { |
| 43 | return isset($this->$name); |
| 44 | } |
| 45 | |
| 46 | public function &__get(string $name) |
| 47 | { |
| 48 | return $this->$name; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 |