Commit b994a014 authored by Keven's avatar Keven
Browse files

Added subtitle related features

parent a7c6581f
<?php
namespace Libcast\Client\Command;
use Libcast\Client\Model\FileInterface;
class AddSubtitle extends FileCommand
{
/**
*
* @var string
*/
protected $method = 'POST';
/**
*
* @param FileInterface $file
* @param string $path Path to the subtitle file
* @param string $language 2-letters ISO 639-1 language code
*/
public function __construct(FileInterface $file, $path, $language)
{
if (!is_readable($path)) {
throw new \RuntimeException(sprintf('File "%s" does not exist or permission to read it is denied.', $path));
}
$extension = pathinfo($path, PATHINFO_EXTENSION);
if (!in_array($extension, $formats = ['srt', 'vtt'])) {
throw new \RuntimeException('The only subtitle formats recognized at the time are: '.implode(', ', $formats));
}
if (strlen($language) != 2) {
throw new \RuntimeException('Subtitle language must be a 2-letters ISO 639-1 language code.');
}
$this->fullPath = $file->href.'/subtitles';
$this->setPayload([
'language' => $language,
]);
$this->requestOptions[CURLOPT_POSTFIELDS] = [
'subtitle' => $subtitle,
'language' => $language,
];
}
}
......@@ -83,6 +83,18 @@ abstract class Command implements CommandInterface
*/
protected $url;
/**
*
* @var array cURL options
*/
protected $requestOptions = array();
/**
*
* @var array cURL headers
*/
protected $requestHeaders = array();
/**
*
* @param string $name
......@@ -205,7 +217,7 @@ abstract class Command implements CommandInterface
*/
protected function getRequestHeaders()
{
return array();
return $this->requestHeaders;
}
/**
......@@ -214,7 +226,7 @@ abstract class Command implements CommandInterface
*/
protected function getRequestOptions()
{
return array();
return $this->requestOptions;
}
/**
......
<?php
namespace Libcast\Client\Command;
use Libcast\Client\Entity\Subtitle;
class RemoveSubtitle extends Command
{
/**
*
* @var string
*/
protected $method = 'DELETE';
/**
*
* @param Subtitle $subtitle
*/
public function __construct($subtitle)
{
if ($subtitle instanceof Subtitle) {
$this->fullPath = $subtitle->href;
} elseif(is_string($subtitle)) {
$this->fullPath = $subtitle;
} else {
throw new \BadParameterException(__CLASS__.' command requires a Subtitle instance or a subtitle API URL.');
}
}
}
......@@ -10,12 +10,6 @@ use JMS\Serializer\SerializerInterface;
class SendFile extends FileCommand
{
/**
*
* @var array cURL options
*/
private $requestOptions = array();
/**
*
* @var array Command options
......
......@@ -8,6 +8,7 @@ use Libcast\Client\Model\StreamInterface;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlRoot;
use JMS\Serializer\Annotation\XmlList;
use JMS\Serializer\Annotation\SerializedName;
/**
......@@ -66,6 +67,13 @@ class File extends Entity implements FileInterface
*/
protected $metadata = [];
/**
* @Type("array<Libcast\Client\Entity\Subtitle>")
* @XmlList(inline = false, entry = "subtitle")
* @var array of Subtitle
*/
protected $subtitles = array();
/**
* URL of the thumbnail
*
......@@ -106,6 +114,25 @@ class File extends Entity implements FileInterface
return $this->client->createResource($resource, $stream);
}
/**
*
* @param string $path Path to the subtitle file
* @param string $language 2-letters ISO 639-1 language code
*/
public function addSubtitle($path, $language)
{
return $this->client->addSubtitle($this, $path, $language);
}
/**
*
* @param string $href Subtitle href or a Subtitle instance
*/
public function removeSubtitle($href)
{
return $this->client->removeSubtitle($href);
}
// Deprecated API
/**
......
......@@ -130,6 +130,13 @@ class Resource extends Entity implements ResourceInterface
*/
protected $widgets = [];
/**
* @Type("array<Libcast\Client\Entity\Subtitle>")
* @XmlList(inline = false, entry = "subtitle")
* @var array of Subtitle
*/
protected $subtitles = array();
/**
*
* @Exclude
......
<?php
namespace Libcast\Client\Entity;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlRoot;
use JMS\Serializer\Annotation\XmlAttribute;
/**
* Immutable subtitle entity
*
* @XmlRoot("subtitle")
*
* @property string $language Lower case ISO 639-1 language code
* @property string $href URL to download (in a Resource) or to delete (in a File) the subtitle file
*/
class Subtitle
{
/**
* Lower case ISO 639-1 code
*
* @XmlAttribute
* @Type("string")
* @var string
*/
protected $language;
/**
*
* @XmlAttribute
* @Type("string")
* @var string
*/
protected $href;
/**
*
* @param string $href
* @param string $language
*/
public function __construct($href = null, $language = null)
{
$this->href = $href;
$this->language = $language;
}
/**
*
* @param string $property
*
* @return mixed
*/
public function __get($property)
{
if (property_exists($this, $property)) {
return $this->$property;
}
}
}
......@@ -78,6 +78,8 @@ use Psr\Log\LogLevel;
* File
*
* @method \Libcast\Client\Entity\File sendFile(string $name, string $path) Upload a file to the user briefcase
* @method \Libcast\Client\Entity\File addSubtitle(\Libcast\Client\Model\FileInterface $path, string $language) Upload a subtitle file and attach it to the File
* @method \Libcast\Client\Entity\File removeSubtitle(\Libcast\Client\Entity\Subtitle $subtitle) Remove a subtitle from a File given a Subtitle instance or its URL
*
* Token
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment