AudioMedium.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @package Grav\Common\Page
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 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 bool $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. $this->attributes['controls'] = true;
  38. } else {
  39. unset($this->attributes['controls']);
  40. }
  41. return $this;
  42. }
  43. /**
  44. * Allows to set the preload behaviour
  45. *
  46. * @param string $preload
  47. * @return $this
  48. */
  49. public function preload($preload)
  50. {
  51. $validPreloadAttrs = ['auto', 'metadata', 'none'];
  52. if (\in_array($preload, $validPreloadAttrs, true)) {
  53. $this->attributes['preload'] = $preload;
  54. }
  55. return $this;
  56. }
  57. /**
  58. * Allows to set the controlsList behaviour
  59. * Separate multiple values with a hyphen
  60. *
  61. * @param string $controlsList
  62. * @return $this
  63. */
  64. public function controlsList($controlsList)
  65. {
  66. $controlsList = str_replace('-', ' ', $controlsList);
  67. $this->attributes['controlsList'] = $controlsList;
  68. return $this;
  69. }
  70. /**
  71. * Allows to set the muted attribute
  72. *
  73. * @param bool $status
  74. * @return $this
  75. */
  76. public function muted($status = false)
  77. {
  78. if($status) {
  79. $this->attributes['muted'] = true;
  80. } else {
  81. unset($this->attributes['muted']);
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Allows to set the loop attribute
  87. *
  88. * @param bool $status
  89. * @return $this
  90. */
  91. public function loop($status = false)
  92. {
  93. if($status) {
  94. $this->attributes['loop'] = true;
  95. } else {
  96. unset($this->attributes['loop']);
  97. }
  98. return $this;
  99. }
  100. /**
  101. * Allows to set the autoplay attribute
  102. *
  103. * @param bool $status
  104. * @return $this
  105. */
  106. public function autoplay($status = false)
  107. {
  108. if($status) {
  109. $this->attributes['autoplay'] = true;
  110. } else {
  111. unset($this->attributes['autoplay']);
  112. }
  113. return $this;
  114. }
  115. /**
  116. * Reset medium.
  117. *
  118. * @return $this
  119. */
  120. public function reset()
  121. {
  122. parent::reset();
  123. $this->attributes['controls'] = true;
  124. return $this;
  125. }
  126. }