/vendor/zendframework/zendframework/library/Zend/Feed/Writer/Extension/ITunes/Entry.php

https://bitbucket.org/juan_sanchez/aiyellow · PHP · 246 lines · 129 code · 19 blank · 98 comment · 17 complexity · 78803dfcc29cec0f47f0f5f215effd0c MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Feed\Writer\Extension\ITunes;
  10. use Zend\Feed\Writer;
  11. use Zend\Feed\Writer\Extension;
  12. use Zend\Stdlib\StringUtils;
  13. use Zend\Stdlib\StringWrapper\StringWrapperInterface;
  14. /**
  15. */
  16. class Entry
  17. {
  18. /**
  19. * Array of Feed data for rendering by Extension's renderers
  20. *
  21. * @var array
  22. */
  23. protected $data = array();
  24. /**
  25. * Encoding of all text values
  26. *
  27. * @var string
  28. */
  29. protected $encoding = 'UTF-8';
  30. /**
  31. * The used string wrapper supporting encoding
  32. *
  33. * @var StringWrapperInterface
  34. */
  35. protected $stringWrapper;
  36. public function __construct()
  37. {
  38. $this->stringWrapper = StringUtils::getWrapper($this->encoding);
  39. }
  40. /**
  41. * Set feed encoding
  42. *
  43. * @param string $enc
  44. * @return Entry
  45. */
  46. public function setEncoding($enc)
  47. {
  48. $this->stringWrapper = StringUtils::getWrapper($enc);
  49. $this->encoding = $enc;
  50. return $this;
  51. }
  52. /**
  53. * Get feed encoding
  54. *
  55. * @return string
  56. */
  57. public function getEncoding()
  58. {
  59. return $this->encoding;
  60. }
  61. /**
  62. * Set a block value of "yes" or "no". You may also set an empty string.
  63. *
  64. * @param string
  65. * @return Entry
  66. * @throws Writer\Exception\InvalidArgumentException
  67. */
  68. public function setItunesBlock($value)
  69. {
  70. if (!ctype_alpha($value) && strlen($value) > 0) {
  71. throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only'
  72. . ' contain alphabetic characters');
  73. }
  74. if ($this->stringWrapper->strlen($value) > 255) {
  75. throw new Writer\Exception\InvalidArgumentException('invalid parameter: "block" may only'
  76. . ' contain a maximum of 255 characters');
  77. }
  78. $this->data['block'] = $value;
  79. }
  80. /**
  81. * Add authors to itunes entry
  82. *
  83. * @param array $values
  84. * @return Entry
  85. */
  86. public function addItunesAuthors(array $values)
  87. {
  88. foreach ($values as $value) {
  89. $this->addItunesAuthor($value);
  90. }
  91. return $this;
  92. }
  93. /**
  94. * Add author to itunes entry
  95. *
  96. * @param string $value
  97. * @return Entry
  98. * @throws Writer\Exception\InvalidArgumentException
  99. */
  100. public function addItunesAuthor($value)
  101. {
  102. if ($this->stringWrapper->strlen($value) > 255) {
  103. throw new Writer\Exception\InvalidArgumentException('invalid parameter: any "author" may only'
  104. . ' contain a maximum of 255 characters each');
  105. }
  106. if (!isset($this->data['authors'])) {
  107. $this->data['authors'] = array();
  108. }
  109. $this->data['authors'][] = $value;
  110. return $this;
  111. }
  112. /**
  113. * Set duration
  114. *
  115. * @param int $value
  116. * @return Entry
  117. * @throws Writer\Exception\InvalidArgumentException
  118. */
  119. public function setItunesDuration($value)
  120. {
  121. $value = (string) $value;
  122. if (!ctype_digit($value)
  123. && !preg_match("/^\d+:[0-5]{1}[0-9]{1}$/", $value)
  124. && !preg_match("/^\d+:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/", $value)
  125. ) {
  126. throw new Writer\Exception\InvalidArgumentException('invalid parameter: "duration" may only'
  127. . ' be of a specified [[HH:]MM:]SS format');
  128. }
  129. $this->data['duration'] = $value;
  130. return $this;
  131. }
  132. /**
  133. * Set "explicit" flag
  134. *
  135. * @param bool $value
  136. * @return Entry
  137. * @throws Writer\Exception\InvalidArgumentException
  138. */
  139. public function setItunesExplicit($value)
  140. {
  141. if (!in_array($value, array('yes','no','clean'))) {
  142. throw new Writer\Exception\InvalidArgumentException('invalid parameter: "explicit" may only'
  143. . ' be one of "yes", "no" or "clean"');
  144. }
  145. $this->data['explicit'] = $value;
  146. return $this;
  147. }
  148. /**
  149. * Set keywords
  150. *
  151. * @param array $value
  152. * @return Entry
  153. * @throws Writer\Exception\InvalidArgumentException
  154. */
  155. public function setItunesKeywords(array $value)
  156. {
  157. if (count($value) > 12) {
  158. throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
  159. . ' contain a maximum of 12 terms');
  160. }
  161. $concat = implode(',', $value);
  162. if ($this->stringWrapper->strlen($concat) > 255) {
  163. throw new Writer\Exception\InvalidArgumentException('invalid parameter: "keywords" may only'
  164. . ' have a concatenated length of 255 chars where terms are delimited'
  165. . ' by a comma');
  166. }
  167. $this->data['keywords'] = $value;
  168. return $this;
  169. }
  170. /**
  171. * Set subtitle
  172. *
  173. * @param string $value
  174. * @return Entry
  175. * @throws Writer\Exception\InvalidArgumentException
  176. */
  177. public function setItunesSubtitle($value)
  178. {
  179. if ($this->stringWrapper->strlen($value) > 255) {
  180. throw new Writer\Exception\InvalidArgumentException('invalid parameter: "subtitle" may only'
  181. . ' contain a maximum of 255 characters');
  182. }
  183. $this->data['subtitle'] = $value;
  184. return $this;
  185. }
  186. /**
  187. * Set summary
  188. *
  189. * @param string $value
  190. * @return Entry
  191. * @throws Writer\Exception\InvalidArgumentException
  192. */
  193. public function setItunesSummary($value)
  194. {
  195. if ($this->stringWrapper->strlen($value) > 4000) {
  196. throw new Writer\Exception\InvalidArgumentException('invalid parameter: "summary" may only'
  197. . ' contain a maximum of 4000 characters');
  198. }
  199. $this->data['summary'] = $value;
  200. return $this;
  201. }
  202. /**
  203. * Overloading to itunes specific setters
  204. *
  205. * @param string $method
  206. * @param array $params
  207. * @throws Writer\Exception\BadMethodCallException
  208. * @return mixed
  209. */
  210. public function __call($method, array $params)
  211. {
  212. $point = lcfirst(substr($method, 9));
  213. if (!method_exists($this, 'setItunes' . ucfirst($point))
  214. && !method_exists($this, 'addItunes' . ucfirst($point))
  215. ) {
  216. throw new Writer\Exception\BadMethodCallException(
  217. 'invalid method: ' . $method
  218. );
  219. }
  220. if (!array_key_exists($point, $this->data)
  221. || empty($this->data[$point])
  222. ) {
  223. return null;
  224. }
  225. return $this->data[$point];
  226. }
  227. }