PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zend-config/src/Processor/Token.php

https://github.com/tmccormi/openemr
PHP | 273 lines | 126 code | 35 blank | 112 comment | 16 complexity | df39f206c7116b402709602ce58da8db 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\Config\Processor;
  10. use Traversable;
  11. use Zend\Config\Config;
  12. use Zend\Config\Exception;
  13. class Token implements ProcessorInterface
  14. {
  15. /**
  16. * Token prefix.
  17. *
  18. * @var string
  19. */
  20. protected $prefix = '';
  21. /**
  22. * Token suffix.
  23. *
  24. * @var string
  25. */
  26. protected $suffix = '';
  27. /**
  28. * The registry of tokens
  29. *
  30. * @var array
  31. */
  32. protected $tokens = [];
  33. /**
  34. * Replacement map
  35. *
  36. * @var array
  37. */
  38. protected $map = null;
  39. /**
  40. * Token Processor walks through a Config structure and replaces all
  41. * occurrences of tokens with supplied values.
  42. *
  43. * @param array|Config|Traversable $tokens Associative array of TOKEN => value
  44. * to replace it with
  45. * @param string $prefix
  46. * @param string $suffix
  47. * @return Token
  48. */
  49. public function __construct($tokens = [], $prefix = '', $suffix = '')
  50. {
  51. $this->setTokens($tokens);
  52. $this->setPrefix($prefix);
  53. $this->setSuffix($suffix);
  54. }
  55. /**
  56. * @param string $prefix
  57. * @return Token
  58. */
  59. public function setPrefix($prefix)
  60. {
  61. // reset map
  62. $this->map = null;
  63. $this->prefix = $prefix;
  64. return $this;
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getPrefix()
  70. {
  71. return $this->prefix;
  72. }
  73. /**
  74. * @param string $suffix
  75. * @return Token
  76. */
  77. public function setSuffix($suffix)
  78. {
  79. // reset map
  80. $this->map = null;
  81. $this->suffix = $suffix;
  82. return $this;
  83. }
  84. /**
  85. * @return string
  86. */
  87. public function getSuffix()
  88. {
  89. return $this->suffix;
  90. }
  91. /**
  92. * Set token registry.
  93. *
  94. * @param array|Config|Traversable $tokens Associative array of TOKEN => value
  95. * to replace it with
  96. * @return Token
  97. * @throws Exception\InvalidArgumentException
  98. */
  99. public function setTokens($tokens)
  100. {
  101. if (is_array($tokens)) {
  102. $this->tokens = $tokens;
  103. } elseif ($tokens instanceof Config) {
  104. $this->tokens = $tokens->toArray();
  105. } elseif ($tokens instanceof Traversable) {
  106. $this->tokens = [];
  107. foreach ($tokens as $key => $val) {
  108. $this->tokens[$key] = $val;
  109. }
  110. } else {
  111. throw new Exception\InvalidArgumentException('Cannot use ' . gettype($tokens) . ' as token registry.');
  112. }
  113. // reset map
  114. $this->map = null;
  115. return $this;
  116. }
  117. /**
  118. * Get current token registry.
  119. *
  120. * @return array
  121. */
  122. public function getTokens()
  123. {
  124. return $this->tokens;
  125. }
  126. /**
  127. * Add new token.
  128. *
  129. * @param string $token
  130. * @param mixed $value
  131. * @return Token
  132. * @throws Exception\InvalidArgumentException
  133. */
  134. public function addToken($token, $value)
  135. {
  136. if (!is_scalar($token)) {
  137. throw new Exception\InvalidArgumentException('Cannot use ' . gettype($token) . ' as token name.');
  138. }
  139. $this->tokens[$token] = $value;
  140. // reset map
  141. $this->map = null;
  142. return $this;
  143. }
  144. /**
  145. * Add new token.
  146. *
  147. * @param string $token
  148. * @param mixed $value
  149. * @return Token
  150. */
  151. public function setToken($token, $value)
  152. {
  153. return $this->addToken($token, $value);
  154. }
  155. /**
  156. * Build replacement map
  157. *
  158. * @return array
  159. */
  160. protected function buildMap()
  161. {
  162. if (null === $this->map) {
  163. if (!$this->suffix && !$this->prefix) {
  164. $this->map = $this->tokens;
  165. } else {
  166. $this->map = [];
  167. foreach ($this->tokens as $token => $value) {
  168. $this->map[$this->prefix . $token . $this->suffix] = $value;
  169. }
  170. }
  171. foreach (array_keys($this->map) as $key) {
  172. if (empty($key)) {
  173. unset($this->map[$key]);
  174. }
  175. }
  176. }
  177. return $this->map;
  178. }
  179. /**
  180. * Process
  181. *
  182. * @param Config $config
  183. * @return Config
  184. * @throws Exception\InvalidArgumentException
  185. */
  186. public function process(Config $config)
  187. {
  188. return $this->doProcess($config, $this->buildMap());
  189. }
  190. /**
  191. * Process a single value
  192. *
  193. * @param $value
  194. * @return mixed
  195. */
  196. public function processValue($value)
  197. {
  198. return $this->doProcess($value, $this->buildMap());
  199. }
  200. /**
  201. * Applies replacement map to the given value by modifying the value itself
  202. *
  203. * @param mixed $value
  204. * @param array $replacements
  205. *
  206. * @return mixed
  207. *
  208. * @throws Exception\InvalidArgumentException if the provided value is a read-only {@see Config}
  209. */
  210. private function doProcess($value, array $replacements)
  211. {
  212. if ($value instanceof Config) {
  213. if ($value->isReadOnly()) {
  214. throw new Exception\InvalidArgumentException('Cannot process config because it is read-only');
  215. }
  216. foreach ($value as $key => $val) {
  217. $value->$key = $this->doProcess($val, $replacements);
  218. }
  219. return $value;
  220. }
  221. if ($value instanceof Traversable || is_array($value)) {
  222. foreach ($value as & $val) {
  223. $val = $this->doProcess($val, $replacements);
  224. }
  225. return $value;
  226. }
  227. if (!is_string($value) && (is_bool($value) || is_numeric($value))) {
  228. $stringVal = (string) $value;
  229. $changedVal = strtr($stringVal, $this->map);
  230. // replace the value only if a string replacement occurred
  231. if ($changedVal !== $stringVal) {
  232. return $changedVal;
  233. }
  234. return $value;
  235. }
  236. return strtr((string) $value, $this->map);
  237. }
  238. }