Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/FileChooser/FileChooser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Playwright\FileChooser;

use Playwright\FileChooser\Options\SetFilesOptions;
use Playwright\Page\PageInterface;
use Playwright\Transport\TransportInterface;

Expand Down Expand Up @@ -50,10 +51,11 @@ public function page(): PageInterface

/**
* @param string|array<string>|array{name: string, mimeType: string, buffer: string}|array<array{name: string, mimeType: string, buffer: string}> $files
* @param array{noWaitAfter?: bool, timeout?: int} $options
* @param array<string, mixed>|SetFilesOptions $options
*/
public function setFiles(string|array $files, array $options = []): void
public function setFiles(string|array $files, array|SetFilesOptions $options = []): void
{
$options = SetFilesOptions::from($options);
$normalizedFiles = $this->normalizeFiles($files);

$payload = [
Expand All @@ -62,8 +64,9 @@ public function setFiles(string|array $files, array $options = []): void
'files' => $normalizedFiles,
];

if (!empty($options)) {
$payload['options'] = $options;
$optionsArray = $options->toArray();
if (!empty($optionsArray)) {
$payload['options'] = $optionsArray;
}

$fileChooserId = $this->data['fileChooserId'] ?? null;
Expand Down
7 changes: 3 additions & 4 deletions src/FileChooser/FileChooserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Playwright\FileChooser;

use Playwright\FileChooser\Options\SetFilesOptions;
use Playwright\Page\PageInterface;

/**
Expand Down Expand Up @@ -42,9 +43,7 @@ public function page(): PageInterface;
* For empty array, clears the selected files.
*
* @param string|array<string>|array{name: string, mimeType: string, buffer: string}|array<array{name: string, mimeType: string, buffer: string}> $files
* @param array{noWaitAfter?: bool, timeout?: int} $options
* @param string|string[]|array{name: string, mimeType: string, buffer: string}|array<array{name: string, mimeType: string, buffer: string}> $files
* @param array<string, mixed> $options
* @param array<string, mixed>|SetFilesOptions $options
*/
public function setFiles(string|array $files, array $options = []): void;
public function setFiles(string|array $files, array|SetFilesOptions $options = []): void;
}
58 changes: 58 additions & 0 deletions src/FileChooser/Options/SetFilesOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Playwright\FileChooser\Options;

final readonly class SetFilesOptions
{
public function __construct(
public ?bool $noWaitAfter = null,
public ?float $timeout = null,
) {
}

/**
* @return array<string, mixed>
*/
public function toArray(): array
{
$options = [];
if (null !== $this->noWaitAfter) {
$options['noWaitAfter'] = $this->noWaitAfter;
}
if (null !== $this->timeout) {
$options['timeout'] = $this->timeout;
}

return $options;
}

/**
* @param array<string, mixed>|self $options
*/
public static function from(array|self $options = []): self
{
if ($options instanceof self) {
return $options;
}

/** @var bool|null $noWaitAfter */
$noWaitAfter = $options['noWaitAfter'] ?? null;
/** @var float|int|null $timeoutValue */
$timeoutValue = $options['timeout'] ?? null;
$timeout = null !== $timeoutValue ? (float) $timeoutValue : null;

return new self($noWaitAfter, $timeout);
}
}
Loading