Skip to content

Commit 0a92e0b

Browse files
authored
Add Options classes (Page, Locator, Selector, Tracing, Websocket, ...) (#56)
Introduces a set of new options classes under the `Playwright\**\Options` namespaces to encapsulate and validate options parameters.
1 parent debeb06 commit 0a92e0b

File tree

109 files changed

+6889
-318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+6889
-318
lines changed

src/FileChooser/FileChooser.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Playwright\FileChooser;
1616

17+
use Playwright\FileChooser\Options\SetFilesOptions;
1718
use Playwright\Page\PageInterface;
1819
use Playwright\Transport\TransportInterface;
1920

@@ -50,10 +51,11 @@ public function page(): PageInterface
5051

5152
/**
5253
* @param string|array<string>|array{name: string, mimeType: string, buffer: string}|array<array{name: string, mimeType: string, buffer: string}> $files
53-
* @param array{noWaitAfter?: bool, timeout?: int} $options
54+
* @param array<string, mixed>|SetFilesOptions $options
5455
*/
55-
public function setFiles(string|array $files, array $options = []): void
56+
public function setFiles(string|array $files, array|SetFilesOptions $options = []): void
5657
{
58+
$options = SetFilesOptions::from($options);
5759
$normalizedFiles = $this->normalizeFiles($files);
5860

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

65-
if (!empty($options)) {
66-
$payload['options'] = $options;
67+
$optionsArray = $options->toArray();
68+
if (!empty($optionsArray)) {
69+
$payload['options'] = $optionsArray;
6770
}
6871

6972
$fileChooserId = $this->data['fileChooserId'] ?? null;

src/FileChooser/FileChooserInterface.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
namespace Playwright\FileChooser;
1616

17+
use Playwright\FileChooser\Options\SetFilesOptions;
1718
use Playwright\Page\PageInterface;
1819

1920
/**
@@ -42,9 +43,7 @@ public function page(): PageInterface;
4243
* For empty array, clears the selected files.
4344
*
4445
* @param string|array<string>|array{name: string, mimeType: string, buffer: string}|array<array{name: string, mimeType: string, buffer: string}> $files
45-
* @param array{noWaitAfter?: bool, timeout?: int} $options
46-
* @param string|string[]|array{name: string, mimeType: string, buffer: string}|array<array{name: string, mimeType: string, buffer: string}> $files
47-
* @param array<string, mixed> $options
46+
* @param array<string, mixed>|SetFilesOptions $options
4847
*/
49-
public function setFiles(string|array $files, array $options = []): void;
48+
public function setFiles(string|array $files, array|SetFilesOptions $options = []): void;
5049
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the community-maintained Playwright PHP project.
7+
* It is not affiliated with or endorsed by Microsoft.
8+
*
9+
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Playwright\FileChooser\Options;
16+
17+
final readonly class SetFilesOptions
18+
{
19+
public function __construct(
20+
public ?bool $noWaitAfter = null,
21+
public ?float $timeout = null,
22+
) {
23+
}
24+
25+
/**
26+
* @return array<string, mixed>
27+
*/
28+
public function toArray(): array
29+
{
30+
$options = [];
31+
if (null !== $this->noWaitAfter) {
32+
$options['noWaitAfter'] = $this->noWaitAfter;
33+
}
34+
if (null !== $this->timeout) {
35+
$options['timeout'] = $this->timeout;
36+
}
37+
38+
return $options;
39+
}
40+
41+
/**
42+
* @param array<string, mixed>|self $options
43+
*/
44+
public static function from(array|self $options = []): self
45+
{
46+
if ($options instanceof self) {
47+
return $options;
48+
}
49+
50+
/** @var bool|null $noWaitAfter */
51+
$noWaitAfter = $options['noWaitAfter'] ?? null;
52+
/** @var float|int|null $timeoutValue */
53+
$timeoutValue = $options['timeout'] ?? null;
54+
$timeout = null !== $timeoutValue ? (float) $timeoutValue : null;
55+
56+
return new self($noWaitAfter, $timeout);
57+
}
58+
}

0 commit comments

Comments
 (0)