Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
DateStringNormalizerFilter.inc.php
1 <?php
2 
17 import('lib.pkp.classes.filter.Filter');
18 import('lib.pkp.classes.validation.ValidatorDate');
19 
25  $this->setDisplayName('Date String Normalizer');
26 
27  parent::Filter('primitive::string', 'validator::date('.DATE_FORMAT_ISO.')');
28  }
29 
30 
31  //
32  // Implement abstract methods from Filter
33  //
40  function &process(&$input) {
41  // FIXME: We have to i18nize this when expanding citation parsing to other languages
42  static $monthNames = array(
43  'Jan' => '01', 'Feb' => '02', 'Mar' => '03', 'Apr' => '04', 'May' => '05', 'Jun' => '06',
44  'Jul' => '07', 'Aug' => '08', 'Sep' => '09', 'Oct' => '10', 'Nov' => '11', 'Dec' => '12'
45  );
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 
57  $month = '';
58  if (isset($parsedDate['monthName'])) {
59  $monthName = substr($parsedDate['monthName'], 0, 3);
60  if (isset($monthNames[$monthName])) {
61  // Convert the month name to a two digit numeric month representation.
62  $month = $monthNames[$monthName];
63  }
64  }
65 
66  if (isset($parsedDate['month'])) {
67  // Convert month to a two digit representation.
68  $month = str_pad($parsedDate['month'], 2, '0', STR_PAD_LEFT);
69  }
70 
71  if (!empty($month)) {
72  $normalizedDate .= '-'.$month;
73  if (isset($parsedDate['day'])) $normalizedDate .= '-'.str_pad($parsedDate['day'], 2, '0', STR_PAD_LEFT);
74  }
75  }
76  if (!empty($normalizedDate)) break;
77  }
78  }
79 
80  return $normalizedDate;
81  }
82 }
83 ?>
setDisplayName($displayName)
Definition: Filter.inc.php:140
Class that provides the basic template for a filter. Filters are generic data processors that take in...
Definition: Filter.inc.php:78
static regexp_match_get($pattern, $subject, &$matches)
Definition: String.inc.php:351
Filter that normalizes a date string to YYYY[-MM[-DD]].