AudioMedium.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * @package Grav.Common.Page
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Page\Medium;
  9. class AudioMedium extends Medium
  10. {
  11. use StaticResizeTrait;
  12. /**
  13. * Parsedown element for source display mode
  14. *
  15. * @param array $attributes
  16. * @param boolean $reset
  17. * @return array
  18. */
  19. protected function sourceParsedownElement(array $attributes, $reset = true)
  20. {
  21. $location = $this->url($reset);
  22. return [
  23. 'name' => 'audio',
  24. 'text' => '<source src="' . $location . '">Your browser does not support the audio tag.',
  25. 'attributes' => $attributes
  26. ];
  27. }
  28. /**
  29. * Allows to set or remove the HTML5 default controls
  30. *
  31. * @param bool $display
  32. * @return $this
  33. */
  34. public function controls($display = true)
  35. {
  36. if($display)
  37. {
  38. $this->attributes['controls'] = true;
  39. }
  40. else
  41. {
  42. unset($this->attributes['controls']);
  43. }
  44. return $this;
  45. }
  46. /**
  47. * Allows to set the preload behaviour
  48. *
  49. * @param $preload
  50. * @return $this
  51. */
  52. public function preload($preload)
  53. {
  54. $validPreloadAttrs = array('auto','metadata','none');
  55. if (in_array($preload, $validPreloadAttrs))
  56. {
  57. $this->attributes['preload'] = $preload;
  58. }
  59. return $this;
  60. }
  61. /**
  62. * Allows to set the controlsList behaviour
  63. * Separate multiple values with a hyphen
  64. *
  65. * @param $controlsList
  66. * @return $this
  67. */
  68. public function controlsList($controlsList)
  69. {
  70. $controlsList = str_replace('-', ' ', $controlsList);
  71. $this->attributes['controlsList'] = $controlsList;
  72. return $this;
  73. }
  74. /**
  75. * Allows to set the muted attribute
  76. *
  77. * @param bool $status
  78. * @return $this
  79. */
  80. public function muted($status = false)
  81. {
  82. if($status)
  83. {
  84. $this->attributes['muted'] = true;
  85. }
  86. else
  87. {
  88. unset($this->attributes['muted']);
  89. }
  90. return $this;
  91. }
  92. /**
  93. * Allows to set the loop attribute
  94. *
  95. * @param bool $status
  96. * @return $this
  97. */
  98. public function loop($status = false)
  99. {
  100. if($status)
  101. {
  102. $this->attributes['loop'] = true;
  103. }
  104. else
  105. {
  106. unset($this->attributes['loop']);
  107. }
  108. return $this;
  109. }
  110. /**
  111. * Allows to set the autoplay attribute
  112. *
  113. * @param bool $status
  114. * @return $this
  115. */
  116. public function autoplay($status = false)
  117. {
  118. if($status)
  119. {
  120. $this->attributes['autoplay'] = true;
  121. }
  122. else
  123. {
  124. unset($this->attributes['autoplay']);
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Reset medium.
  130. *
  131. * @return $this
  132. */
  133. public function reset()
  134. {
  135. parent::reset();
  136. $this->attributes['controls'] = true;
  137. return $this;
  138. }
  139. }