-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial work on chaining Exporter implementations
- Loading branch information
1 parent
6a7c780
commit aeb0007
Showing
3 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,60 @@ | ||
<?php declare(strict_types=1); | ||
/* | ||
* This file is part of PHPUnit. | ||
* | ||
* (c) Sebastian Bergmann <sebastian@phpunit.de> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace PHPUnit\Util; | ||
|
||
use function assert; | ||
|
||
/** | ||
* @internal This class is not covered by the backward compatibility promise for PHPUnit | ||
*/ | ||
final readonly class ExporterChain implements Exporter | ||
{ | ||
/** | ||
* @psalm-var non-empty-list<Exporter> | ||
*/ | ||
private array $exporter; | ||
|
||
/** | ||
* @psalm-param non-empty-list<Exporter> $exporter | ||
*/ | ||
public static function buildWith(array $exporter): self | ||
{ | ||
$exporter[] = new DefaultExporter; | ||
|
||
return new self($exporter); | ||
} | ||
|
||
/** | ||
* @psalm-param non-empty-list<Exporter> $exporter | ||
*/ | ||
private function __construct(array $exporter) | ||
{ | ||
$this->exporter = $exporter; | ||
} | ||
|
||
public function handles(mixed $value): true | ||
{ | ||
return true; | ||
} | ||
|
||
public function export(mixed $value): string | ||
{ | ||
foreach ($this->exporter as $exporter) { | ||
if (!$exporter->handles($value)) { | ||
/** @noinspection PhpUnnecessaryStopStatementInspection */ | ||
continue; | ||
} | ||
} | ||
|
||
assert(isset($exporter)); | ||
|
||
return $exporter->export($value); | ||
} | ||
} |