-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathTranscribeAudio.php
More file actions
52 lines (43 loc) · 1.5 KB
/
Copy pathTranscribeAudio.php
File metadata and controls
52 lines (43 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace App\AI\REST\Input\Parser;
use App\AI\DataType\Audio as AudioDataType;
use App\AI\REST\Value\TranscribeAudioAction;
use Ibexa\ConnectorAi\REST\Input\Parser\Action;
use Ibexa\Contracts\ConnectorAi\Action\RuntimeContext;
use Ibexa\Contracts\Rest\Input\ParsingDispatcher;
use Ibexa\Rest\Input\BaseParser;
final class TranscribeAudio extends BaseParser
{
public const string AUDIO_KEY = 'Audio';
public const string BASE64_KEY = 'base64';
/** @param array<mixed> $data */
public function parse(array $data, ParsingDispatcher $parsingDispatcher): TranscribeAudioAction
{
$this->assertInputIsValid($data);
$runtimeContext = $this->getRuntimeContext($data);
return new TranscribeAudioAction(
new AudioDataType([$data[self::AUDIO_KEY][self::BASE64_KEY]]),
$runtimeContext
);
}
/** @param array<mixed> $data */
private function assertInputIsValid(array $data): void
{
if (!array_key_exists(self::AUDIO_KEY, $data)) {
throw new \InvalidArgumentException('Missing audio key');
}
if (!array_key_exists(self::BASE64_KEY, $data[self::AUDIO_KEY])) {
throw new \InvalidArgumentException('Missing base64 key');
}
}
/**
* @param array<string, mixed> $data
*/
private function getRuntimeContext(array $data): RuntimeContext
{
return new RuntimeContext(
$data[Action::RUNTIME_CONTEXT_KEY] ?? []
);
}
}