vonage-php-sdk-core icon indicating copy to clipboard operation
vonage-php-sdk-core copied to clipboard

Vonage\Voice\NCCO\NCCO does not correctly parse valid NCCOs with streamUrls

Open itssamtaylor opened this issue 1 year ago • 0 comments

Expected Behavior

Vonage\Voice\NCCO\NCCO class should be able to correctly parse an NCCO array.

use Vonage\Voice\NCCO\NCCO;

$ncco1 = new NCCO();
$ncco2 = new NCCO();

$ncco2->fromArray($ncco1->toArray());

Should always work

Current Behavior

Given an NCCO array with a stream action, we get a TypeError

\Vonage\Voice\NCCO\Action\Stream::factory(): Argument #1 ($streamUrl) must be of type string, array given

Possible Solution

Add an array as possible type in the Stream factory method.

Vonage\Voice\NCCO\Action\Stream

class Stream implements ActionInterface
{
  // ...

  public static function factory(string|array $streamUrl, array $data): Stream
  {
    $stream = new Stream(is_string($streamUrl) ? $streamUrl : $streamUrl[0]);

    // ...

    return $stream;
  }

  // ...
}

Steps to Reproduce (for bugs)

use Vonage\Voice\NCCO\NCCO;
use Vonage\Voice\NCCO\Action\Stream;

$ncco1 = new NCCO();
$ncco2 = new NCCO();

$ncco1->addAction(Stream::factory('https://www.example.com/some-stream.mp3'));

$ncco1->toArray(); 
/* Returns:
 * [
 *   [
 *     'action' => 'stream',
 *     'streamUrl' => [
 *       'https://www.example.com/some-stream.mp3'
 *     ]    
 *   ]
 * ]
 */

$ncco2->fromArray($ncco1->toArray());
/* Fails with TypeError */

Context

Using $ncco->fromArray($data) is useful for validating NCCOs

Your Environment

  • Version used: 4.8.6
  • Environment name and version (e.g. PHP 7.2 on nginx 1.19.1): PHP 8.2
  • Operating System and version: macOS 14.2.1

itssamtaylor avatar Aug 09 '24 20:08 itssamtaylor