-
-
Notifications
You must be signed in to change notification settings - Fork 43
Change BinaryColumn::phpTypecast() to return StringableSteam|string|null
#1013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7b857f6
Change `BinaryColumn::phpTypecast()` to return `StringableSteam` inst…
Tigrov 8c52e70
Apply fixes from StyleCI
StyleCIBot e18492a
Apply Rector changes (CI)
Tigrov 4e99a37
Improve
Tigrov 85eff2a
Fix psalm
Tigrov b355836
Change `InvalidArgumentException` to `LogicException`
Tigrov 3b8190d
Update
Tigrov c5865a2
Apply Rector changes (CI)
Tigrov 27d57ad
Fix rector
Tigrov 4594cb2
Add line to CHANGELOG.md [skip ci]
Tigrov 0520a0f
Update
Tigrov 652c3aa
Apply Rector changes (CI)
Tigrov 277453a
Fix rector
Tigrov a853a38
Merge branch 'master' into binary-to-stringable
Tigrov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Schema\Data; | ||
|
|
||
| use LogicException; | ||
| use Stringable; | ||
| use Yiisoft\Db\Constant\GettypeResult; | ||
|
|
||
| use function fclose; | ||
| use function gettype; | ||
| use function is_resource; | ||
| use function stream_get_contents; | ||
|
|
||
| /** | ||
| * Represents a resource stream to be used as a {@see Stringable} value. | ||
| * | ||
| * ```php | ||
| * use Yiisoft\Db\Schema\Data\ResourceStream; | ||
| * | ||
| * // @var resource $resource | ||
| * $stream = new StringableStream($resource); | ||
| * | ||
| * echo $stream; | ||
| * ``` | ||
| */ | ||
| final class StringableStream implements Stringable | ||
| { | ||
| /** | ||
| * @var resource|string $value The resource stream or the result of reading the stream. | ||
| */ | ||
| private mixed $value; | ||
|
|
||
| /** | ||
| * @param resource $value The open resource stream. | ||
| */ | ||
| public function __construct(mixed $value) | ||
| { | ||
| $this->value = $value; | ||
| } | ||
|
|
||
| /** | ||
| * Closes the resource. | ||
| */ | ||
| public function __destruct() | ||
| { | ||
| if (is_resource($this->value)) { | ||
| fclose($this->value); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] Prepared values for serialization. | ||
| */ | ||
| public function __serialize(): array | ||
| { | ||
| return ['value' => $this->__toString()]; | ||
| } | ||
|
|
||
| /** | ||
| * @return string The result of reading the resource stream. | ||
| */ | ||
| public function __toString(): string | ||
| { | ||
| /** | ||
| * @psalm-suppress PossiblyFalsePropertyAssignmentValue, PossiblyInvalidArgument | ||
| * @var string | ||
| */ | ||
| return match (gettype($this->value)) { | ||
vjik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| GettypeResult::RESOURCE => $this->value = stream_get_contents($this->value), | ||
| GettypeResult::RESOURCE_CLOSED => throw new LogicException('Resource is closed.'), | ||
| default => $this->value, | ||
Tigrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * @return resource|string The resource stream or the result of reading the stream. | ||
| */ | ||
| public function getValue(): mixed | ||
Tigrov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| return $this->value; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Yiisoft\Db\Tests\Db\Schema\Data; | ||
|
|
||
| use LogicException; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Yiisoft\Db\Constant\GettypeResult; | ||
| use Yiisoft\Db\Schema\Data\StringableStream; | ||
|
|
||
| use function fclose; | ||
| use function fopen; | ||
| use function gettype; | ||
| use function serialize; | ||
| use function unserialize; | ||
|
|
||
| /** | ||
| * @group db | ||
| */ | ||
| final class ResourceStreamTest extends TestCase | ||
| { | ||
| public function testConstruct(): void | ||
| { | ||
| $resource = fopen(__DIR__ . '/../../../Support/string.txt', 'rb'); | ||
| $stringableSteam = new StringableStream($resource); | ||
|
|
||
| $this->assertSame($resource, $stringableSteam->getValue()); | ||
| } | ||
|
|
||
| public function testDestruct(): void | ||
| { | ||
| $resource = fopen(__DIR__ . '/../../../Support/string.txt', 'rb'); | ||
| $stringableSteam = new StringableStream($resource); | ||
|
|
||
| $this->assertSame(GettypeResult::RESOURCE, gettype($resource)); | ||
|
|
||
| unset($stringableSteam); | ||
|
|
||
| $this->assertSame(GettypeResult::RESOURCE_CLOSED, gettype($resource)); | ||
| } | ||
|
|
||
| public function testSerialize(): void | ||
| { | ||
| $resource = fopen(__DIR__ . '/../../../Support/string.txt', 'rb'); | ||
| $stringableSteam = new StringableStream($resource); | ||
| $serialized = serialize($stringableSteam); | ||
|
|
||
| $this->assertSame('O:39:"Yiisoft\Db\Schema\Data\StringableStream":1:{s:5:"value";s:6:"string";}', $serialized); | ||
| $this->assertEquals($stringableSteam, unserialize($serialized)); | ||
| } | ||
|
|
||
| public function testToString(): void | ||
| { | ||
| $resource = fopen(__DIR__ . '/../../../Support/string.txt', 'rb'); | ||
| $stringableSteam = new StringableStream($resource); | ||
|
|
||
| // Can be read twice and more | ||
| $this->assertSame('string', (string) $stringableSteam); | ||
| $this->assertSame('string', (string) $stringableSteam); | ||
| } | ||
|
|
||
| public function testToStringClosedResource(): void | ||
| { | ||
| $resource = fopen(__DIR__ . '/../../../Support/string.txt', 'rb'); | ||
| $stringableSteam = new StringableStream($resource); | ||
|
|
||
| fclose($resource); | ||
|
|
||
| $this->expectException(LogicException::class); | ||
| $this->expectExceptionMessage('Resource is closed.'); | ||
|
|
||
| (string) $stringableSteam; | ||
| } | ||
|
|
||
| public function testGetValue(): void | ||
| { | ||
| $resource = fopen(__DIR__ . '/../../../Support/string.txt', 'rb'); | ||
| $stringableSteam = new StringableStream($resource); | ||
|
|
||
| $this->assertSame($resource, $stringableSteam->getValue()); | ||
| $this->assertSame('string', (string) $stringableSteam); | ||
| $this->assertSame('string', $stringableSteam->getValue()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.