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

https://gitlab.com/yousafsyed/easternglamor · PHP · 210 lines · 110 code · 20 blank · 80 comment · 15 complexity · 0851677985e93ec5fb400d02a465da08 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. use Zend\Stdlib\ArrayUtils;
  12. /**
  13. * Compresses a given string
  14. */
  15. class Compress extends AbstractFilter
  16. {
  17. /**
  18. * Compression adapter
  19. */
  20. protected $adapter = 'Gz';
  21. /**
  22. * Compression adapter constructor options
  23. */
  24. protected $adapterOptions = array();
  25. /**
  26. * Class constructor
  27. *
  28. * @param string|array|Traversable $options (Optional) Options to set
  29. */
  30. public function __construct($options = null)
  31. {
  32. if ($options instanceof Traversable) {
  33. $options = ArrayUtils::iteratorToArray($options);
  34. }
  35. if (is_string($options)) {
  36. $this->setAdapter($options);
  37. } elseif ($options instanceof Compress\CompressionAlgorithmInterface) {
  38. $this->setAdapter($options);
  39. } elseif (is_array($options)) {
  40. $this->setOptions($options);
  41. }
  42. }
  43. /**
  44. * Set filter setate
  45. *
  46. * @param array $options
  47. * @throws Exception\InvalidArgumentException if options is not an array or Traversable
  48. * @return self
  49. */
  50. public function setOptions($options)
  51. {
  52. if (!is_array($options) && !$options instanceof Traversable) {
  53. throw new Exception\InvalidArgumentException(sprintf(
  54. '"%s" expects an array or Traversable; received "%s"',
  55. __METHOD__,
  56. (is_object($options) ? get_class($options) : gettype($options))
  57. ));
  58. }
  59. foreach ($options as $key => $value) {
  60. if ($key == 'options') {
  61. $key = 'adapterOptions';
  62. }
  63. $method = 'set' . ucfirst($key);
  64. if (method_exists($this, $method)) {
  65. $this->$method($value);
  66. }
  67. }
  68. return $this;
  69. }
  70. /**
  71. * Returns the current adapter, instantiating it if necessary
  72. *
  73. * @throws Exception\RuntimeException
  74. * @throws Exception\InvalidArgumentException
  75. * @return Compress\CompressionAlgorithmInterface
  76. */
  77. public function getAdapter()
  78. {
  79. if ($this->adapter instanceof Compress\CompressionAlgorithmInterface) {
  80. return $this->adapter;
  81. }
  82. $adapter = $this->adapter;
  83. $options = $this->getAdapterOptions();
  84. if (!class_exists($adapter)) {
  85. $adapter = 'Zend\\Filter\\Compress\\' . ucfirst($adapter);
  86. if (!class_exists($adapter)) {
  87. throw new Exception\RuntimeException(sprintf(
  88. '%s unable to load adapter; class "%s" not found',
  89. __METHOD__,
  90. $this->adapter
  91. ));
  92. }
  93. }
  94. $this->adapter = new $adapter($options);
  95. if (!$this->adapter instanceof Compress\CompressionAlgorithmInterface) {
  96. throw new Exception\InvalidArgumentException("Compression adapter '" . $adapter . "' does not implement Zend\\Filter\\Compress\\CompressionAlgorithmInterface");
  97. }
  98. return $this->adapter;
  99. }
  100. /**
  101. * Retrieve adapter name
  102. *
  103. * @return string
  104. */
  105. public function getAdapterName()
  106. {
  107. return $this->getAdapter()->toString();
  108. }
  109. /**
  110. * Sets compression adapter
  111. *
  112. * @param string|Compress\CompressionAlgorithmInterface $adapter Adapter to use
  113. * @return self
  114. * @throws Exception\InvalidArgumentException
  115. */
  116. public function setAdapter($adapter)
  117. {
  118. if ($adapter instanceof Compress\CompressionAlgorithmInterface) {
  119. $this->adapter = $adapter;
  120. return $this;
  121. }
  122. if (!is_string($adapter)) {
  123. throw new Exception\InvalidArgumentException('Invalid adapter provided; must be string or instance of Zend\\Filter\\Compress\\CompressionAlgorithmInterface');
  124. }
  125. $this->adapter = $adapter;
  126. return $this;
  127. }
  128. /**
  129. * Retrieve adapter options
  130. *
  131. * @return array
  132. */
  133. public function getAdapterOptions()
  134. {
  135. return $this->adapterOptions;
  136. }
  137. /**
  138. * Set adapter options
  139. *
  140. * @param array $options
  141. * @return self
  142. */
  143. public function setAdapterOptions(array $options)
  144. {
  145. $this->adapterOptions = $options;
  146. return $this;
  147. }
  148. /**
  149. * Get individual or all options from underlying adapter
  150. *
  151. * @param null|string $option
  152. * @return mixed
  153. */
  154. public function getOptions($option = null)
  155. {
  156. $adapter = $this->getAdapter();
  157. return $adapter->getOptions($option);
  158. }
  159. /**
  160. * Calls adapter methods
  161. *
  162. * @param string $method Method to call
  163. * @param string|array $options Options for this method
  164. * @return mixed
  165. * @throws Exception\BadMethodCallException
  166. */
  167. public function __call($method, $options)
  168. {
  169. $adapter = $this->getAdapter();
  170. if (!method_exists($adapter, $method)) {
  171. throw new Exception\BadMethodCallException("Unknown method '{$method}'");
  172. }
  173. return call_user_func_array(array($adapter, $method), $options);
  174. }
  175. /**
  176. * Defined by Zend\Filter\FilterInterface
  177. *
  178. * Compresses the content $value with the defined settings
  179. *
  180. * @param string $value Content to compress
  181. * @return string The compressed content
  182. */
  183. public function filter($value)
  184. {
  185. if (!is_string($value)) {
  186. return $value;
  187. }
  188. return $this->getAdapter()->compress($value);
  189. }
  190. }