@@ -34,20 +34,28 @@ final class Platform implements PlatformInterface
3434 */
3535 private readonly array $ resultConverters ;
3636
37+ /**
38+ * @var VoiceListenerInterface[]
39+ */
40+ private iterable $ voiceListeners ;
41+
3742 /**
3843 * @param iterable<ModelClientInterface> $modelClients
3944 * @param iterable<ResultConverterInterface> $resultConverters
45+ * @param iterable<VoiceListenerInterface> $voiceListeners
4046 */
4147 public function __construct (
4248 iterable $ modelClients ,
4349 iterable $ resultConverters ,
50+ iterable $ voiceListeners ,
4451 private readonly ModelCatalogInterface $ modelCatalog ,
4552 private ?Contract $ contract = null ,
4653 private readonly ?EventDispatcherInterface $ eventDispatcher = null ,
4754 ) {
4855 $ this ->contract = $ contract ?? Contract::create ();
4956 $ this ->modelClients = $ modelClients instanceof \Traversable ? iterator_to_array ($ modelClients ) : $ modelClients ;
5057 $ this ->resultConverters = $ resultConverters instanceof \Traversable ? iterator_to_array ($ resultConverters ) : $ resultConverters ;
58+ $ this ->voiceListeners = $ voiceListeners instanceof \Traversable ? iterator_to_array ($ voiceListeners ) : $ voiceListeners ;
5159 }
5260
5361 public function invoke (string $ model , array |string |object $ input , array $ options = []): DeferredResult
@@ -65,8 +73,9 @@ public function invoke(string $model, array|string|object $input, array $options
6573 }
6674
6775 $ result = $ this ->convertResult ($ model , $ this ->doInvoke ($ model , $ payload , $ options ), $ options );
76+ $ finalResult = $ this ->listen ($ result , $ payload , $ options );
6877
69- $ event = new ResultEvent ($ model , $ result , $ options );
78+ $ event = new ResultEvent ($ model , $ finalResult , $ options );
7079 $ this ->eventDispatcher ?->dispatch($ event );
7180
7281 return $ event ->getDeferredResult ();
@@ -105,4 +114,17 @@ private function convertResult(Model $model, RawResultInterface $result, array $
105114
106115 throw new RuntimeException (\sprintf ('No ResultConverter registered for model "%s" with given input. ' , $ model ::class));
107116 }
117+
118+ private function listen (DeferredResult $ result , array |string $ payload , array $ options ): DeferredResult
119+ {
120+ foreach ($ this ->voiceListeners as $ voiceListener ) {
121+ if ($ voiceListener ->supportListening ($ payload , $ options )) {
122+ $ result ->setVoice ($ voiceListener ->listen ($ payload , $ options ));
123+
124+ return $ result ;
125+ }
126+ }
127+
128+ throw new RuntimeException ('No VoiceListener registered. ' );
129+ }
108130}
0 commit comments