/wp-content/plugins/wpforms-lite/src/Helpers/Chain.php

https://gitlab.com/ebrjose/comcebu · PHP · 412 lines · 167 code · 34 blank · 211 comment · 3 complexity · 41eee95c6d6dcc7b95aa0a84b773da45 MD5 · raw file

  1. <?php
  2. namespace WPForms\Helpers;
  3. /**
  4. * Chain monad, useful for chaining certain array or string related functions.
  5. *
  6. * @since 1.5.6
  7. *
  8. * @method Chain array_change_key_case()
  9. * @method Chain array_chunk()
  10. * @method Chain array_column()
  11. * @method Chain array_combine()
  12. * @method Chain array_count_values()
  13. * @method Chain array_diff_assoc()
  14. * @method Chain array_diff_key()
  15. * @method Chain array_diff_uassoc()
  16. * @method Chain array_diff_ukey()
  17. * @method Chain array_diff(array $var)
  18. * @method Chain array_fill_keys()
  19. * @method Chain array_fill()
  20. * @method Chain array_filter()
  21. * @method Chain array_flip()
  22. * @method Chain array_intersect_assoc()
  23. * @method Chain array_intersect_key()
  24. * @method Chain array_intersect_uassoc()
  25. * @method Chain array_intersect_ukey()
  26. * @method Chain array_intersect(array $var)
  27. * @method Chain array_key_first()
  28. * @method Chain array_key_last()
  29. * @method Chain array_keys()
  30. * @method Chain array_map()
  31. * @method Chain array_merge_recursive()
  32. * @method Chain array_merge(array $var)
  33. * @method Chain array_pad()
  34. * @method Chain array_pop()
  35. * @method Chain array_product()
  36. * @method Chain array_rand()
  37. * @method Chain array_reduce()
  38. * @method Chain array_replace_recursive()
  39. * @method Chain array_replace()
  40. * @method Chain array_reverse()
  41. * @method Chain array_shift()
  42. * @method Chain array_slice()
  43. * @method Chain array_splice()
  44. * @method Chain array_sum()
  45. * @method Chain array_udiff_assoc()
  46. * @method Chain array_udiff_uassoc()
  47. * @method Chain array_udiff()
  48. * @method Chain array_uintersect_assoc()
  49. * @method Chain array_uintersect_uassoc()
  50. * @method Chain array_uintersect()
  51. * @method Chain array_unique()
  52. * @method Chain array_values()
  53. * @method Chain count()
  54. * @method Chain current()
  55. * @method Chain end()
  56. * @method Chain key()
  57. * @method Chain next()
  58. * @method Chain prev()
  59. * @method Chain range()
  60. * @method Chain reset()
  61. * @method Chain ltrim()
  62. * @method Chain rtrim()
  63. * @method Chain md5()
  64. * @method Chain str_getcsv()
  65. * @method Chain str_ireplace()
  66. * @method Chain str_pad()
  67. * @method Chain str_repeat()
  68. * @method Chain str_rot13()
  69. * @method Chain str_shuffle()
  70. * @method Chain str_split()
  71. * @method Chain str_word_count()
  72. * @method Chain strcasecmp()
  73. * @method Chain strchr()
  74. * @method Chain strcmp()
  75. * @method Chain strcoll()
  76. * @method Chain strcspn()
  77. * @method Chain strip_tags()
  78. * @method Chain stripcslashes()
  79. * @method Chain stripos()
  80. * @method Chain stripslashes()
  81. * @method Chain stristr()
  82. * @method Chain strlen()
  83. * @method Chain strnatcasecmp()
  84. * @method Chain strnatcmp()
  85. * @method Chain strncasecmp()
  86. * @method Chain strncmp()
  87. * @method Chain strpbrk()
  88. * @method Chain strpos()
  89. * @method Chain strrchr()
  90. * @method Chain strrev()
  91. * @method Chain strripos()
  92. * @method Chain strrpos()
  93. * @method Chain strspn()
  94. * @method Chain strstr()
  95. * @method Chain strtok()
  96. * @method Chain strtolower()
  97. * @method Chain strtoupper()
  98. * @method Chain strtr()
  99. * @method Chain substr_compare()
  100. * @method Chain substr_count()
  101. * @method Chain substr_replace()
  102. * @method Chain substr()
  103. * @method Chain trim()
  104. * @method Chain ucfirst()
  105. * @method Chain ucwords()
  106. * @method Chain vfprintf()
  107. * @method Chain vprintf()
  108. * @method Chain vsprintf()
  109. * @method Chain wordwrap()
  110. */
  111. class Chain {
  112. /**
  113. * Current value.
  114. *
  115. * @since 1.5.6
  116. *
  117. * @var mixed
  118. */
  119. private $value;
  120. /**
  121. * Class constructor.
  122. *
  123. * @since 1.5.6
  124. *
  125. * @param mixed $value Current value to start working with.
  126. */
  127. public function __construct( $value ) {
  128. $this->value = $value;
  129. }
  130. /**
  131. * Bind some function to value.
  132. *
  133. * @since 1.5.6
  134. *
  135. * @param mixed $fn Some function.
  136. *
  137. * @return Chain
  138. */
  139. public function bind( $fn ) {
  140. $this->value = $fn( $this->value );
  141. return $this;
  142. }
  143. /**
  144. * Get value.
  145. *
  146. * @since 1.5.6
  147. *
  148. * @return mixed
  149. */
  150. public function value() {
  151. return $this->value;
  152. }
  153. /**
  154. * Magic call.
  155. *
  156. * @since 1.5.6
  157. *
  158. * @param string $name Method name.
  159. * @param array $params Parameters.
  160. *
  161. * @throws \BadFunctionCallException Invalid function is called.
  162. *
  163. * @return Chain
  164. */
  165. public function __call( $name, $params ) {
  166. if ( in_array( $name, $this->allowed_methods(), true ) ) {
  167. $params = null === $params ? array() : $params;
  168. array_unshift( $params, $this->value );
  169. $this->value = call_user_func_array( $name, array_values( $params ) );
  170. return $this;
  171. }
  172. throw new \BadFunctionCallException( "Provided function { $name } is not allowed. See Chain::allowed_methods()." );
  173. }
  174. /**
  175. * Join array elements with a string.
  176. *
  177. * @since 1.5.6
  178. *
  179. * @param string $glue Defaults to an empty string.
  180. *
  181. * @return Chain
  182. */
  183. public function implode( $glue = '' ) {
  184. $this->value = implode( $glue, $this->value );
  185. return $this;
  186. }
  187. /**
  188. * Split a string by a string.
  189. *
  190. * @since 1.5.6
  191. *
  192. * @param string $delimiter The boundary string.
  193. *
  194. * @return Chain
  195. */
  196. public function explode( $delimiter ) {
  197. $this->value = explode( $delimiter, $this->value );
  198. return $this;
  199. }
  200. /**
  201. * Apply the callback to the elements of the given arrays.
  202. *
  203. * @since 1.5.6
  204. *
  205. * @param callable $cb Callback.
  206. *
  207. * @return Chain
  208. */
  209. public function map( $cb ) {
  210. $this->value = array_map( $cb, $this->value );
  211. return $this;
  212. }
  213. /**
  214. * Pop array.
  215. *
  216. * @since 1.5.6
  217. *
  218. * @return Chain
  219. */
  220. public function pop() {
  221. $this->value = array_pop( $this->value );
  222. return $this;
  223. }
  224. /**
  225. * Run first or second callback based on a condition.
  226. *
  227. * @since 1.5.6
  228. *
  229. * @param callable $condition Condition function.
  230. * @param callable $true_result If condition will return true we run this function.
  231. * @param callable $false_result If condition will return false we run this function.
  232. *
  233. * @return Chain
  234. */
  235. public function iif( $condition, $true_result, $false_result = null ) {
  236. if ( ! is_callable( $false_result ) ) {
  237. $false_result = function() {
  238. return '';
  239. };
  240. }
  241. $this->value = array_map(
  242. function( $el ) use ( $condition, $true_result, $false_result ) {
  243. if ( call_user_func( $condition, $el ) ) {
  244. return call_user_func( $true_result, $el );
  245. }
  246. return call_user_func( $false_result, $el );
  247. },
  248. $this->value
  249. );
  250. return $this;
  251. }
  252. /**
  253. * All allowed methods to work with data.
  254. *
  255. * @since 1.5.6
  256. *
  257. * @return array
  258. */
  259. public function allowed_methods() {
  260. return [
  261. 'array_change_key_case',
  262. 'array_chunk',
  263. 'array_column',
  264. 'array_combine',
  265. 'array_count_values',
  266. 'array_diff_assoc',
  267. 'array_diff_key',
  268. 'array_diff_uassoc',
  269. 'array_diff_ukey',
  270. 'array_diff',
  271. 'array_fill_keys',
  272. 'array_fill',
  273. 'array_filter',
  274. 'array_flip',
  275. 'array_intersect_assoc',
  276. 'array_intersect_key',
  277. 'array_intersect_uassoc',
  278. 'array_intersect_ukey',
  279. 'array_intersect',
  280. 'array_key_first',
  281. 'array_key_last',
  282. 'array_keys',
  283. 'array_map',
  284. 'array_merge_recursive',
  285. 'array_merge',
  286. 'array_pad',
  287. 'array_pop',
  288. 'array_product',
  289. 'array_rand',
  290. 'array_reduce',
  291. 'array_replace_recursive',
  292. 'array_replace',
  293. 'array_reverse',
  294. 'array_shift',
  295. 'array_slice',
  296. 'array_splice',
  297. 'array_sum',
  298. 'array_udiff_assoc',
  299. 'array_udiff_uassoc',
  300. 'array_udiff',
  301. 'array_uintersect_assoc',
  302. 'array_uintersect_uassoc',
  303. 'array_uintersect',
  304. 'array_unique',
  305. 'array_values',
  306. 'count',
  307. 'current',
  308. 'end',
  309. 'key',
  310. 'next',
  311. 'prev',
  312. 'range',
  313. 'reset',
  314. 'implode',
  315. 'ltrim',
  316. 'rtrim',
  317. 'md5',
  318. 'str_getcsv',
  319. 'str_ireplace',
  320. 'str_pad',
  321. 'str_repeat',
  322. 'str_rot13',
  323. 'str_shuffle',
  324. 'str_split',
  325. 'str_word_count',
  326. 'strcasecmp',
  327. 'strchr',
  328. 'strcmp',
  329. 'strcoll',
  330. 'strcspn',
  331. 'strip_tags',
  332. 'stripcslashes',
  333. 'stripos',
  334. 'stripslashes',
  335. 'stristr',
  336. 'strlen',
  337. 'strnatcasecmp',
  338. 'strnatcmp',
  339. 'strncasecmp',
  340. 'strncmp',
  341. 'strpbrk',
  342. 'strpos',
  343. 'strrchr',
  344. 'strrev',
  345. 'strripos',
  346. 'strrpos',
  347. 'strspn',
  348. 'strstr',
  349. 'strtok',
  350. 'strtolower',
  351. 'strtoupper',
  352. 'strtr',
  353. 'substr_compare',
  354. 'substr_count',
  355. 'substr_replace',
  356. 'substr',
  357. 'trim',
  358. 'ucfirst',
  359. 'ucwords',
  360. 'vfprintf',
  361. 'vprintf',
  362. 'vsprintf',
  363. 'wordwrap',
  364. ];
  365. }
  366. /**
  367. * Create myself.
  368. *
  369. * @since 1.5.6
  370. *
  371. * @param mixed $value Current.
  372. *
  373. * @return Chain
  374. */
  375. public static function of( $value = null ) {
  376. return new self( $value );
  377. }
  378. }