/vendor/zendframework/zend-feed/src/Writer/Extension/ITunes/Entry.php

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