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

/lib/rdfapi-php/api/sparql/FilterFunctions.php

https://github.com/komagata/plnet
PHP | 136 lines | 83 code | 15 blank | 38 comment | 19 complexity | 1893f2e0bde69d3ecccf748044d49936 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?PHP
  2. /**
  3. * List of functions used to evaluate the FILTER statements in
  4. * SPARQL queries.
  5. *
  6. * @package sparql
  7. * @author Tobias Gauss <tobias.gauss@web.de>
  8. * @version $Id$
  9. *
  10. */
  11. /**
  12. * Evaluates the regex() function. Returns true if the regex is matched false if not.
  13. *
  14. * @param String $string the string which has to be evaluated
  15. * @param String $pattern the regex pattern
  16. * @param String $flags additional flags like "i"
  17. * @return boolean
  18. */
  19. function regex($string,$pattern,$flags = ''){
  20. $string = trim($string);
  21. $pattern = trim($pattern);
  22. if(strpos($string,"str_")===0){
  23. $string = substr($string,4);
  24. $pattern = substr($pattern,4);
  25. $flags = substr($flags,4);
  26. }else{
  27. return false;
  28. }
  29. if(preg_match('/'.$pattern.'/'.$flags,$string))
  30. return true;
  31. else
  32. return false;
  33. }
  34. /**
  35. * Evaluates the dateTime() function.Tries to convert a date string into
  36. * a unix timestamp.
  37. *
  38. * @param String $string the date string
  39. * @return integer the corresponding unix timestamp
  40. */
  41. function dateTime($string){
  42. $string = trim($string);
  43. if(strpos($string,"str_")===0)
  44. $string = substr($string,4);
  45. $time = strtotime($string);
  46. if($time == -1)
  47. return $string;
  48. else
  49. return $time;
  50. }
  51. /**
  52. * Evaluates the langMatches() function. Return true if the lang tag matches false if not.
  53. *
  54. * @param String $lang_range the string.
  55. * @param String $lang_tag the regex pattern
  56. * @return boolean
  57. */
  58. function langMatches($lang_range,$lang_tag){
  59. if($lang_range == null)
  60. return false;
  61. if(strpos($lang_range,"str_")===0)
  62. $lang_range = substr($lang_range,4);
  63. if(strpos($lang_tag,"str_")===0)
  64. $lang_tag = substr($lang_tag,4);
  65. if(strtolower($lang_range) == strtolower($lang_tag))
  66. return true;
  67. $tags = preg_match_all("/[^\-\s].[^\-\s]*/",$lang_range,$hits);
  68. if($tags){
  69. if($lang_tag == '*')
  70. return true;
  71. foreach($hits[0] as $tag){
  72. if(strtolower($tag) == strtolower($lang_tag))
  73. return true;
  74. }
  75. return false;
  76. }else{
  77. return false;
  78. }
  79. }
  80. /**
  81. * Evaluates the str() function. Returns the string representation of a
  82. * variable or RDF term.
  83. *
  84. * @param String $string the string
  85. * @return boolean
  86. */
  87. function str($string){
  88. $str = preg_match("/\".[^\"]*\"|\'.[^\']*\'/",$string,$hits);
  89. if($str != 0){
  90. return "str_".$hits[0];
  91. }else{
  92. if(strpos($string,"str_")===0){
  93. return $string;
  94. }else{
  95. if(strpos($string,"uri_")===0)
  96. return "str_".substr($string,4);
  97. if(strpos($string,'<')==0)
  98. return "str_".substr($string,1,-1);
  99. }
  100. }
  101. return false;
  102. }
  103. /**
  104. * Evaluates the lang() function. Returns lang tag of a Literal.
  105. *
  106. * @param String $string the string.
  107. * @return String the lang tag or false if there is no language tag.
  108. */
  109. function lang($string){
  110. $str = preg_match("/\".[^\"]*\"@(.[^\s]*)|\'.[^\']*\'@(.[^\s]*)/",$string,$hits);
  111. if($str){
  112. if($hits[1] != null)
  113. return $hits[1];
  114. else
  115. return $hits[2];
  116. }else{
  117. return false;
  118. }
  119. }
  120. ?>