1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AudioManager\Adapter; |
4
|
|
|
|
5
|
|
|
use AudioManager\Adapter\Options\Ivona as Options; |
6
|
|
|
use AudioManager\Adapter\Ivona\Payload; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Adapter for Ivona TTS |
10
|
|
|
* @package AudioManager\Adapter |
11
|
|
|
* @method Options getOptions() |
12
|
|
|
*/ |
13
|
|
|
class Ivona extends AbstractAdapter implements AdapterInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Payload |
17
|
|
|
*/ |
18
|
|
|
protected $payload; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Init options |
22
|
|
|
* @return Options |
23
|
|
|
*/ |
24
|
3 |
|
protected function initOptions() |
25
|
|
|
{ |
26
|
3 |
|
return new Options; |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Read audio from Ivona service |
31
|
|
|
* @param string $text |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
1 |
|
public function read($text) |
35
|
|
|
{ |
36
|
1 |
|
$payload = $this->getPayload($text); |
37
|
|
|
|
38
|
1 |
|
$handle = $this->getHandle(); |
39
|
1 |
|
$handle->setUrl($payload->getServiceUrl()); |
40
|
1 |
|
$handle->setOption(CURLOPT_RETURNTRANSFER, 1); |
41
|
1 |
|
$handle->setOption(CURLOPT_POST, true); |
|
|
|
|
42
|
|
|
|
43
|
1 |
|
$handle->setOption(CURLOPT_POSTFIELDS, $payload->getPayload()); |
|
|
|
|
44
|
1 |
|
$handle->setOption(CURLOPT_HTTPHEADER, $payload->getHeaders()); |
|
|
|
|
45
|
1 |
|
$handle->setOption(CURLOPT_SSL_VERIFYPEER, false); |
|
|
|
|
46
|
|
|
|
47
|
1 |
|
$response = $handle->execute(); |
48
|
1 |
|
$this->setHeaders($handle->getInfo()); |
49
|
1 |
|
return $response; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Initialize payload |
54
|
|
|
* @param $text |
55
|
|
|
* @return Payload |
56
|
|
|
*/ |
57
|
|
|
private function initPayload($text) |
58
|
|
|
{ |
59
|
|
|
$payload = new Payload(); |
60
|
|
|
$payload->setOptions($this->getOptions()); |
61
|
|
|
$payload->setQueryText($text); |
62
|
|
|
return $payload->createPayload(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param $text |
67
|
|
|
* @return Payload |
68
|
|
|
*/ |
69
|
1 |
|
public function getPayload($text) |
70
|
|
|
{ |
71
|
1 |
|
if (!$this->payload instanceof Payload) { |
72
|
|
|
$this->payload = $this->initPayload($text); |
73
|
|
|
} |
74
|
1 |
|
return $this->payload; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param Payload $payload |
79
|
|
|
*/ |
80
|
1 |
|
public function setPayload(Payload $payload) |
81
|
|
|
{ |
82
|
1 |
|
$this->payload = $payload; |
83
|
1 |
|
} |
84
|
|
|
} |
85
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.