PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pkp/classes/metadata/DateStringNormalizerFilter.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 92 lines | 47 code | 11 blank | 34 comment | 13 complexity | 89fb71f28dc01c2ce5618cb5b7f15c8a MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/metadata/DateStringNormalizerFilter.inc.php
  4. *
  5. * Copyright (c) 2000-2012 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class DateStringNormalizerFilter
  9. * @ingroup metadata
  10. *
  11. * @brief Filter that normalizes a date string to
  12. * YYYY[-MM[-DD]].
  13. */
  14. // $Id$
  15. import('filter.Filter');
  16. class DateStringNormalizerFilter extends Filter {
  17. //
  18. // Implement template methods from Filter
  19. //
  20. /**
  21. * @see Filter::supports()
  22. * @param $input mixed
  23. * @param $output mixed
  24. * @return boolean
  25. */
  26. function supports(&$input, &$output) {
  27. // Check input type
  28. if(!is_string($input)) return false;
  29. // Check output type
  30. if(is_null($output)) return true;
  31. if(!is_string($output)) return false;
  32. // Check whether the output is correctly formatted
  33. return (boolean)String::regexp_match("/\d{4}(-\d{2}(-\d{2})?)?/", $output);
  34. }
  35. /**
  36. * Normalize incoming date string.
  37. * @see Filter::process()
  38. * @param $input string
  39. * @return string
  40. */
  41. function &process(&$input) {
  42. // FIXME: We have to i18nize this when expanding citation parsing to other languages
  43. static $monthNames = array(
  44. 'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06',
  45. 'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12'
  46. );
  47. $dateExpressions = array(
  48. '/(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})/',
  49. '/(?P<year>\d{4})\s*(?P<monthName>[a-z]\w+)?\s*(?P<day>\d+)?/i'
  50. );
  51. $normalizedDate = null;
  52. foreach($dateExpressions as $dateExpression) {
  53. if (String::regexp_match_get($dateExpression, $input, $parsedDate) ){
  54. if (isset($parsedDate['year'])) {
  55. $normalizedDate = $parsedDate['year'];
  56. $month = '';
  57. if (isset($parsedDate['monthName'])) {
  58. $monthName = substr($parsedDate['monthName'], 0, 3);
  59. if (isset($monthNames[$monthName])) {
  60. // Convert the month name to a two digit numeric month representation
  61. // before adding it to the normalized date string.
  62. $month = $monthNames[$monthName];
  63. }
  64. }
  65. if (isset($parsedDate['month'])) {
  66. $monthInt = (integer)$parsedDate['month'];
  67. if ($monthInt >=1 && $monthInt <= 12)
  68. $month = str_pad((string)$monthInt, 2, '0', STR_PAD_LEFT);
  69. }
  70. if (!empty($month)) {
  71. $normalizedDate .= '-'.$month;
  72. if (isset($parsedDate['day'])) $normalizedDate .= '-'.str_pad($parsedDate['day'], 2, '0', STR_PAD_LEFT);
  73. }
  74. }
  75. if (!empty($normalizedDate)) break;
  76. }
  77. }
  78. return $normalizedDate;
  79. }
  80. }
  81. ?>