Skip to content

Commit 237cb18

Browse files
Ayeshnicolas-grekas
authored andcommitted
[PHP 8.1] Add CURLStringFile polyfill
1 parent 4d05b4a commit 237cb18

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This component provides features added to PHP 8.1 core:
77
- [`enum_exists`](https://php.net/enum-exists)
88
- [`MYSQLI_REFRESH_REPLICA`](https://php.net/mysqli.constants#constantmysqli-refresh-replica) constant
99
- [`ReturnTypeWillChange`](https://wiki.php.net/rfc/internal_method_return_types)
10+
- [`CURLStringFile`](https://php.net/CURLStringFile) (but only if PHP >= 7.4 is used)
1011

1112
More information can be found in the
1213
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).

Resources/stubs/CURLStringFile.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}

0 commit comments

Comments
 (0)