Browse Source

Add UploadedFileFactory.

ADmad 3 years ago
parent
commit
1eecdf8765
2 changed files with 115 additions and 0 deletions
  1. 58 0
      src/Http/UploadedFileFactory.php
  2. 57 0
      tests/TestCase/Http/UploadedFileFactoryTest.php

+ 58 - 0
src/Http/UploadedFileFactory.php

@@ -0,0 +1,58 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         5.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Http;
+
+use Laminas\Diactoros\UploadedFile;
+use Psr\Http\Message\StreamInterface;
+use Psr\Http\Message\UploadedFileFactoryInterface;
+use Psr\Http\Message\UploadedFileInterface;
+
+/**
+ * Factory class for creating uploaded file instances.
+ */
+class UploadedFileFactory implements UploadedFileFactoryInterface
+{
+    /**
+     * Create a new uploaded file.
+     *
+     * If a size is not provided it will be determined by checking the size of
+     * the stream.
+     *
+     * @link http://php.net/manual/features.file-upload.post-method.php
+     * @link http://php.net/manual/features.file-upload.errors.php
+     * @param \Psr\Http\Message\StreamInterface $stream The underlying stream representing the
+     *     uploaded file content.
+     * @param int|null $size The size of the file in bytes.
+     * @param int $error The PHP file upload error.
+     * @param string|null $clientFilename The filename as provided by the client, if any.
+     * @param string|null $clientMediaType The media type as provided by the client, if any.
+     * @throws \InvalidArgumentException If the file resource is not readable.
+     */
+    public function createUploadedFile(
+        StreamInterface $stream,
+        ?int $size = null,
+        int $error = UPLOAD_ERR_OK,
+        ?string $clientFilename = null,
+        ?string $clientMediaType = null
+    ): UploadedFileInterface {
+        if ($size === null) {
+            $size = $stream->getSize();
+        }
+
+        return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType);
+    }
+}

+ 57 - 0
tests/TestCase/Http/UploadedFileFactoryTest.php

@@ -0,0 +1,57 @@
+<?php
+declare(strict_types=1);
+
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         5.0.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\TestCase\Http;
+
+use Cake\Http\UploadedFileFactory;
+use Cake\TestSuite\TestCase;
+use Laminas\Diactoros\Stream;
+
+/**
+ * Test case for the uploaded file factory.
+ */
+class UploadedFileFactoryTest extends TestCase
+{
+    protected UploadedFileFactory $factory;
+
+    protected string $filename = TMP . 'uploadedfile-factory-file-test.txt';
+
+    protected function setUp(): void
+    {
+        parent::setUp();
+
+        $this->factory = new UploadedFileFactory();
+    }
+
+    protected function tearDown(): void
+    {
+        parent::tearDown();
+
+        // phpcs:disable
+        @unlink($this->filename);
+        // phpcs:enable
+    }
+
+    public function testCreateStreamResource(): void
+    {
+        file_put_contents($this->filename, 'it works');
+        $stream = new Stream($this->filename);
+
+        $uploadedFile = $this->factory->createUploadedFile($stream, null, UPLOAD_ERR_OK, 'my-name');
+        $this->assertSame('my-name', $uploadedFile->getClientFilename());
+        $this->assertSame($stream, $uploadedFile->getStream());
+    }
+}