PageRenderTime 21ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/zendframework/zend-view/src/Helper/FlashMessenger.php

https://github.com/tmccormi/openemr
PHP | 353 lines | 174 code | 37 blank | 142 comment | 15 complexity | f6c83669b36ad01523d171545db2bd22 MD5 | raw file
  1. <?php
  2. /**
  3. * @link http://github.com/zendframework/zend-view for the canonical source repository
  4. * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
  5. * @license http://framework.zend.com/license/new-bsd New BSD License
  6. */
  7. namespace Zend\View\Helper;
  8. use Zend\Mvc\Controller\Plugin\FlashMessenger as V2PluginFlashMessenger;
  9. use Zend\Mvc\Plugin\FlashMessenger\FlashMessenger as PluginFlashMessenger;
  10. use Zend\View\Exception\InvalidArgumentException;
  11. /**
  12. * Helper to proxy the plugin flash messenger
  13. *
  14. * Duck-types against Zend\I18n\Translator\TranslatorAwareInterface.
  15. *
  16. * @deprecated This helper will be removed in version 3.0 of this component.
  17. * At that time, it will be available in zendframework/zend-mvc-plugin-flashmessenger.
  18. */
  19. class FlashMessenger extends AbstractHelper
  20. {
  21. use TranslatorAwareTrait;
  22. /**
  23. * Default attributes for the open format tag
  24. *
  25. * @todo For version 3, have the keys reference the class constants in the
  26. * FlashMessenger plugin.
  27. * @var array
  28. */
  29. protected $classMessages = [
  30. 'info' => 'info',
  31. 'error' => 'error',
  32. 'success' => 'success',
  33. 'default' => 'default',
  34. 'warning' => 'warning',
  35. ];
  36. /**
  37. * Templates for the open/close/separators for message tags
  38. *
  39. * @var string
  40. */
  41. protected $messageCloseString = '</li></ul>';
  42. protected $messageOpenFormat = '<ul%s><li>';
  43. protected $messageSeparatorString = '</li><li>';
  44. /**
  45. * Flag whether to escape messages
  46. *
  47. * @var bool
  48. */
  49. protected $autoEscape = true;
  50. /**
  51. * Html escape helper
  52. *
  53. * @var EscapeHtml
  54. */
  55. protected $escapeHtmlHelper;
  56. /**
  57. * Flash messenger plugin
  58. *
  59. * @var V2PluginFlashMessenger|PluginFlashMessenger
  60. */
  61. protected $pluginFlashMessenger;
  62. /**
  63. * Returns the flash messenger plugin controller
  64. *
  65. * @param string|null $namespace
  66. * @return FlashMessenger|V2PluginFlashMessenger|PluginFlashMessenger
  67. */
  68. public function __invoke($namespace = null)
  69. {
  70. if (null === $namespace) {
  71. return $this;
  72. }
  73. $flashMessenger = $this->getPluginFlashMessenger();
  74. return $flashMessenger->getMessagesFromNamespace($namespace);
  75. }
  76. /**
  77. * Proxy the flash messenger plugin controller
  78. *
  79. * @param string $method
  80. * @param array $argv
  81. * @return mixed
  82. */
  83. public function __call($method, $argv)
  84. {
  85. $flashMessenger = $this->getPluginFlashMessenger();
  86. return call_user_func_array([$flashMessenger, $method], $argv);
  87. }
  88. /**
  89. * Render Messages
  90. *
  91. * @param string $namespace
  92. * @param array $classes
  93. * @param null|bool $autoEscape
  94. * @return string
  95. */
  96. public function render($namespace = 'default', array $classes = [], $autoEscape = null)
  97. {
  98. $flashMessenger = $this->getPluginFlashMessenger();
  99. $messages = $flashMessenger->getMessagesFromNamespace($namespace);
  100. return $this->renderMessages($namespace, $messages, $classes, $autoEscape);
  101. }
  102. /**
  103. * Render Current Messages
  104. *
  105. * @param string $namespace
  106. * @param array $classes
  107. * @param bool|null $autoEscape
  108. * @return string
  109. */
  110. public function renderCurrent($namespace = 'default', array $classes = [], $autoEscape = null)
  111. {
  112. $flashMessenger = $this->getPluginFlashMessenger();
  113. $messages = $flashMessenger->getCurrentMessagesFromNamespace($namespace);
  114. return $this->renderMessages($namespace, $messages, $classes, $autoEscape);
  115. }
  116. /**
  117. * Render Messages
  118. *
  119. * @param string $namespace
  120. * @param array $messages
  121. * @param array $classes
  122. * @param bool|null $autoEscape
  123. * @return string
  124. */
  125. protected function renderMessages(
  126. $namespace = 'default',
  127. array $messages = [],
  128. array $classes = [],
  129. $autoEscape = null
  130. ) {
  131. if (empty($messages)) {
  132. return '';
  133. }
  134. // Prepare classes for opening tag
  135. if (empty($classes)) {
  136. if (isset($this->classMessages[$namespace])) {
  137. $classes = $this->classMessages[$namespace];
  138. } else {
  139. $classes = $this->classMessages['default'];
  140. }
  141. $classes = [$classes];
  142. }
  143. if (null === $autoEscape) {
  144. $autoEscape = $this->getAutoEscape();
  145. }
  146. // Flatten message array
  147. $escapeHtml = $this->getEscapeHtmlHelper();
  148. $messagesToPrint = [];
  149. $translator = $this->getTranslator();
  150. $translatorTextDomain = $this->getTranslatorTextDomain();
  151. array_walk_recursive(
  152. $messages,
  153. function ($item) use (& $messagesToPrint, $escapeHtml, $autoEscape, $translator, $translatorTextDomain) {
  154. if ($translator !== null) {
  155. $item = $translator->translate(
  156. $item,
  157. $translatorTextDomain
  158. );
  159. }
  160. if ($autoEscape) {
  161. $messagesToPrint[] = $escapeHtml($item);
  162. return;
  163. }
  164. $messagesToPrint[] = $item;
  165. }
  166. );
  167. if (empty($messagesToPrint)) {
  168. return '';
  169. }
  170. // Generate markup
  171. $markup = sprintf($this->getMessageOpenFormat(), ' class="' . implode(' ', $classes) . '"');
  172. $markup .= implode(
  173. sprintf($this->getMessageSeparatorString(), ' class="' . implode(' ', $classes) . '"'),
  174. $messagesToPrint
  175. );
  176. $markup .= $this->getMessageCloseString();
  177. return $markup;
  178. }
  179. /**
  180. * Set whether or not auto escaping should be used
  181. *
  182. * @param bool $autoEscape
  183. * @return self
  184. */
  185. public function setAutoEscape($autoEscape = true)
  186. {
  187. $this->autoEscape = (bool) $autoEscape;
  188. return $this;
  189. }
  190. /**
  191. * Return whether auto escaping is enabled or disabled
  192. *
  193. * return bool
  194. */
  195. public function getAutoEscape()
  196. {
  197. return $this->autoEscape;
  198. }
  199. /**
  200. * Set the string used to close message representation
  201. *
  202. * @param string $messageCloseString
  203. * @return FlashMessenger
  204. */
  205. public function setMessageCloseString($messageCloseString)
  206. {
  207. $this->messageCloseString = (string) $messageCloseString;
  208. return $this;
  209. }
  210. /**
  211. * Get the string used to close message representation
  212. *
  213. * @return string
  214. */
  215. public function getMessageCloseString()
  216. {
  217. return $this->messageCloseString;
  218. }
  219. /**
  220. * Set the formatted string used to open message representation
  221. *
  222. * @param string $messageOpenFormat
  223. * @return FlashMessenger
  224. */
  225. public function setMessageOpenFormat($messageOpenFormat)
  226. {
  227. $this->messageOpenFormat = (string) $messageOpenFormat;
  228. return $this;
  229. }
  230. /**
  231. * Get the formatted string used to open message representation
  232. *
  233. * @return string
  234. */
  235. public function getMessageOpenFormat()
  236. {
  237. return $this->messageOpenFormat;
  238. }
  239. /**
  240. * Set the string used to separate messages
  241. *
  242. * @param string $messageSeparatorString
  243. * @return FlashMessenger
  244. */
  245. public function setMessageSeparatorString($messageSeparatorString)
  246. {
  247. $this->messageSeparatorString = (string) $messageSeparatorString;
  248. return $this;
  249. }
  250. /**
  251. * Get the string used to separate messages
  252. *
  253. * @return string
  254. */
  255. public function getMessageSeparatorString()
  256. {
  257. return $this->messageSeparatorString;
  258. }
  259. /**
  260. * Set the flash messenger plugin
  261. *
  262. * @param V2PluginFlashMessenger|PluginFlashMessenger $pluginFlashMessenger
  263. * @return FlashMessenger
  264. * @throws InvalidArgumentException for an invalid $pluginFlashMessenger
  265. */
  266. public function setPluginFlashMessenger($pluginFlashMessenger)
  267. {
  268. if (! $pluginFlashMessenger instanceof V2PluginFlashMessenger
  269. && ! $pluginFlashMessenger instanceof PluginFlashMessenger
  270. ) {
  271. throw new InvalidArgumentException(sprintf(
  272. '%s expects either a %s or %s instance; received %s',
  273. __METHOD__,
  274. V2PluginFlashMessenger::class,
  275. PluginFlashMessenger::class,
  276. (is_object($pluginFlashMessenger) ? get_class($pluginFlashMessenger) : gettype($pluginFlashMessenger))
  277. ));
  278. }
  279. $this->pluginFlashMessenger = $pluginFlashMessenger;
  280. return $this;
  281. }
  282. /**
  283. * Get the flash messenger plugin
  284. *
  285. * @return V2PluginFlashMessenger|PluginFlashMessenger
  286. */
  287. public function getPluginFlashMessenger()
  288. {
  289. if (null === $this->pluginFlashMessenger) {
  290. $this->setPluginFlashMessenger(
  291. class_exists(PluginFlashMessenger::class)
  292. ? new PluginFlashMessenger()
  293. : new V2PluginFlashMessenger()
  294. );
  295. }
  296. return $this->pluginFlashMessenger;
  297. }
  298. /**
  299. * Retrieve the escapeHtml helper
  300. *
  301. * @return EscapeHtml
  302. */
  303. protected function getEscapeHtmlHelper()
  304. {
  305. if ($this->escapeHtmlHelper) {
  306. return $this->escapeHtmlHelper;
  307. }
  308. if (method_exists($this->getView(), 'plugin')) {
  309. $this->escapeHtmlHelper = $this->view->plugin('escapehtml');
  310. }
  311. if (! $this->escapeHtmlHelper instanceof EscapeHtml) {
  312. $this->escapeHtmlHelper = new EscapeHtml();
  313. }
  314. return $this->escapeHtmlHelper;
  315. }
  316. }