Files
Heimdall/vendor/phrity/util-transformer/src/FirstMatchResolver.php
T
KodeStar d16b7f60f3 Vendor phrity/websocket dependency and fix v3 exception namespace
The WebSocket support added for TrueNAS JSON-RPC 2.0 requires the
phrity/websocket library to actually be available at runtime. This repo
commits the vendor/ tree (CI does not run composer install), so the
dependency and composer.lock must be committed for class_exists() checks
in the TrueNAS app to succeed.

- composer require phrity/websocket:^3.6 (resolves to 3.7.3) with lock
  and vendor/ committed
- Fix TrueNASWebSocketClient to catch WebSocket\Exception\Exception
  (phrity/websocket v3 namespace) instead of the non-existent
  WebSocket\ConnectionException from the old textalk/websocket v1/v2 API,
  so connection/call failures are logged and wrapped as intended
2026-07-09 22:01:18 +01:00

55 lines
1.7 KiB
PHP

<?php
namespace Phrity\Util\Transformer;
use InvalidArgumentException;
class FirstMatchResolver implements TransformerInterface
{
/** @var array<TransformerInterface> $transformers */
private array $transformers;
private string|null $default;
/**
* @param array<TransformerInterface> $transformers
* @param string|null $default
*/
public function __construct(array $transformers, string|null $default = null)
{
foreach ($transformers as $transformer) {
if (!$transformer instanceof TransformerInterface) {
throw new InvalidArgumentException(sprintf(
"'%s' is not implementing %s",
get_debug_type($transformer),
TransformerInterface::class
));
}
}
$this->transformers = $transformers;
$this->default = $default;
}
public function canTransform(mixed $subject, string|null $type = null): bool
{
$type ??= $this->default;
foreach ($this->transformers as $transformer) {
if ($transformer->canTransform($subject, $type)) {
return true;
}
}
return false;
}
public function transform(mixed $subject, string|null $type = null): mixed
{
$type ??= $this->default;
foreach ($this->transformers as $transformer) {
if ($transformer->canTransform($subject, $type)) {
return $transformer->transform($subject, $type);
}
}
$subjectType = get_debug_type($subject);
throw new TransformerException("Could not find transformer for '{$subjectType}'.");
}
}