PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-filter/src/AbstractDateDropdown.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 155 lines | 70 code | 19 blank | 66 comment | 9 complexity | 79c7449eb9ca2e79acb129c882a78d67 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\Filter;
  10. use Traversable;
  11. abstract class AbstractDateDropdown extends AbstractFilter
  12. {
  13. /**
  14. * If true, the filter will return null if any date field is empty
  15. *
  16. * @var bool
  17. */
  18. protected $nullOnEmpty = false;
  19. /**
  20. * If true, the filter will return null if all date fields are empty
  21. *
  22. * @var bool
  23. */
  24. protected $nullOnAllEmpty = false;
  25. /**
  26. * Sprintf format string to use for formatting the date, fields will be used in alphabetical order.
  27. *
  28. * @var string
  29. */
  30. protected $format = '';
  31. /**
  32. * @var int
  33. */
  34. protected $expectedInputs;
  35. /**
  36. * @param mixed $options If array or Traversable, passes value to
  37. * setOptions().
  38. */
  39. public function __construct($options = null)
  40. {
  41. if (is_array($options) || $options instanceof Traversable) {
  42. $this->setOptions($options);
  43. }
  44. }
  45. /**
  46. * @param bool $nullOnAllEmpty
  47. * @return self
  48. */
  49. public function setNullOnAllEmpty($nullOnAllEmpty)
  50. {
  51. $this->nullOnAllEmpty = $nullOnAllEmpty;
  52. return $this;
  53. }
  54. /**
  55. * @return bool
  56. */
  57. public function isNullOnAllEmpty()
  58. {
  59. return $this->nullOnAllEmpty;
  60. }
  61. /**
  62. * @param bool $nullOnEmpty
  63. * @return self
  64. */
  65. public function setNullOnEmpty($nullOnEmpty)
  66. {
  67. $this->nullOnEmpty = $nullOnEmpty;
  68. return $this;
  69. }
  70. /**
  71. * @return bool
  72. */
  73. public function isNullOnEmpty()
  74. {
  75. return $this->nullOnEmpty;
  76. }
  77. /**
  78. * Attempts to filter an array of date/time information to a formatted
  79. * string.
  80. *
  81. * @param mixed $value
  82. * @return mixed
  83. * @throws Exception\RuntimeException If filtering $value is impossible
  84. */
  85. public function filter($value)
  86. {
  87. if (! is_array($value)) {
  88. // nothing to do
  89. return $value;
  90. }
  91. // Convert the date to a specific format
  92. if ($this->isNullOnEmpty()
  93. && array_reduce($value, __CLASS__ . '::reduce', false)
  94. ) {
  95. return;
  96. }
  97. if ($this->isNullOnAllEmpty()
  98. && array_reduce($value, __CLASS__ . '::reduce', true)
  99. ) {
  100. return;
  101. }
  102. $this->filterable($value);
  103. ksort($value);
  104. $value = vsprintf($this->format, $value);
  105. return $value;
  106. }
  107. /**
  108. * Ensures there are enough inputs in the array to properly format the date.
  109. *
  110. * @param $value
  111. * @throws Exception\RuntimeException
  112. */
  113. protected function filterable($value)
  114. {
  115. if (count($value) !== $this->expectedInputs) {
  116. throw new Exception\RuntimeException(
  117. sprintf(
  118. 'There are not enough values in the array to filter this date (Required: %d, Received: %d)',
  119. $this->expectedInputs,
  120. count($value)
  121. )
  122. );
  123. }
  124. }
  125. /**
  126. * Reduce to a single value
  127. *
  128. * @param string $soFar
  129. * @param string $value
  130. * @return bool
  131. */
  132. public static function reduce($soFar, $value)
  133. {
  134. return $soFar || empty($value);
  135. }
  136. }