PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/utils.php

https://gitlab.com/aristath/maera
PHP | 174 lines | 95 code | 35 blank | 44 comment | 27 complexity | 1668ba9c295617f2291ac4ac152188b2 MD5 | raw file
  1. <?php
  2. /**
  3. * Utility functions
  4. */
  5. function is_element_empty( $element ) {
  6. $element = trim( $element );
  7. return empty( $element ) ? false : true;
  8. }
  9. /**
  10. * Return the value of an echo.
  11. * example: maera_get_echo( 'function' );
  12. */
  13. function maera_get_echo( $function, $args = '' ) {
  14. // Early exit if function does not exist
  15. if ( ! function_exists( $function ) ) {
  16. return;
  17. }
  18. ob_start();
  19. $function( $args );
  20. $get_echo = ob_get_clean();
  21. return $get_echo;
  22. }
  23. /**
  24. * Get the contents of a .twig file.
  25. * This is just a helper function used by the maera_get_twig() function
  26. */
  27. function _maera_get_twig( $file ) {
  28. $context = Maera()->cache->get_context();
  29. Timber::render( array( $file ), $context, Maera()->cache->cache_duration() );
  30. }
  31. /**
  32. * Return the rendered twig file.
  33. */
  34. function maera_get_twig( $file ) {
  35. $value = maera_get_echo( '_maera_get_twig', $file );
  36. return $value;
  37. }
  38. /**
  39. * This file is part of the array_column library
  40. *
  41. * For the full copyright and license information, please view the LICENSE
  42. * file that was distributed with this source code.
  43. *
  44. * @copyright Copyright (c) 2013 Ben Ramsey <http://benramsey.com>
  45. * @license http://opensource.org/licenses/MIT MIT
  46. */
  47. if (!function_exists('array_column')) {
  48. /**
  49. * Returns the values from a single column of the input array, identified by
  50. * the $columnKey.
  51. *
  52. * Optionally, you may provide an $indexKey to index the values in the returned
  53. * array by the values from the $indexKey column in the input array.
  54. *
  55. * @param array $input A multi-dimensional array (record set) from which to pull
  56. * a column of values.
  57. * @param mixed $columnKey The column of values to return. This value may be the
  58. * integer key of the column you wish to retrieve, or it
  59. * may be the string key name for an associative array.
  60. * @param mixed $indexKey (Optional.) The column to use as the index/keys for
  61. * the returned array. This value may be the integer key
  62. * of the column, or it may be the string key name.
  63. * @return array
  64. */
  65. function array_column($input = null, $columnKey = null, $indexKey = null)
  66. {
  67. // Using func_get_args() in order to check for proper number of
  68. // parameters and trigger errors exactly as the built-in array_column()
  69. // does in PHP 5.5.
  70. $argc = func_num_args();
  71. $params = func_get_args();
  72. if ($argc < 2) {
  73. trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
  74. return null;
  75. }
  76. if (!is_array($params[0])) {
  77. trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);
  78. return null;
  79. }
  80. if (!is_int($params[1])
  81. && !is_float($params[1])
  82. && !is_string($params[1])
  83. && $params[1] !== null
  84. && !(is_object($params[1]) && method_exists($params[1], '__toString'))
  85. ) {
  86. trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
  87. return false;
  88. }
  89. if (isset($params[2])
  90. && !is_int($params[2])
  91. && !is_float($params[2])
  92. && !is_string($params[2])
  93. && !(is_object($params[2]) && method_exists($params[2], '__toString'))
  94. ) {
  95. trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
  96. return false;
  97. }
  98. $paramsInput = $params[0];
  99. $paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
  100. $paramsIndexKey = null;
  101. if (isset($params[2])) {
  102. if (is_float($params[2]) || is_int($params[2])) {
  103. $paramsIndexKey = (int) $params[2];
  104. } else {
  105. $paramsIndexKey = (string) $params[2];
  106. }
  107. }
  108. $resultArray = array();
  109. foreach ($paramsInput as $row) {
  110. $key = $value = null;
  111. $keySet = $valueSet = false;
  112. if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
  113. $keySet = true;
  114. $key = (string) $row[$paramsIndexKey];
  115. }
  116. if ($paramsColumnKey === null) {
  117. $valueSet = true;
  118. $value = $row;
  119. } elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
  120. $valueSet = true;
  121. $value = $row[$paramsColumnKey];
  122. }
  123. if ($valueSet) {
  124. if ($keySet) {
  125. $resultArray[$key] = $value;
  126. } else {
  127. $resultArray[] = $value;
  128. }
  129. }
  130. }
  131. return $resultArray;
  132. }
  133. }
  134. function maera_return_0() { return 0; }
  135. function maera_return_1() { return 1; }
  136. function maera_return_2() { return 2; }
  137. function maera_return_3() { return 3; }
  138. function maera_return_4() { return 4; }
  139. function maera_return_5() { return 5; }