PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pkp/classes/metadata/nlm/NlmNameSchemaPersonStringFilter.inc.php

https://github.com/lib-uoguelph-ca/ocs
PHP | 171 lines | 71 code | 25 blank | 75 comment | 10 complexity | 77232fd0177a42043d907dd8a16c99e3 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * @file classes/metadata/NlmNameSchemaPersonStringFilter.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 NlmNameSchemaPersonStringFilter
  9. * @ingroup metadata_nlm
  10. * @see NlmNameSchema
  11. *
  12. * @brief Filter that converts from NLM name to
  13. * a string.
  14. */
  15. // $Id$
  16. import('metadata.nlm.NlmPersonStringFilter');
  17. class NlmNameSchemaPersonStringFilter extends NlmPersonStringFilter {
  18. /** @var string */
  19. var $_template;
  20. /** @var string */
  21. var $_delimiter;
  22. /**
  23. * Constructor
  24. * @param $filterMode integer
  25. * @param $template string default: DRIVER guidelines 2.0 name template
  26. * Possible template variables are %surname%, %suffix%, %prefix%, %initials%, %firstname%
  27. */
  28. function NlmNameSchemaPersonStringFilter($filterMode = PERSON_STRING_FILTER_SINGLE, $template = '%surname%%suffix%,%initials% (%firstname%)%prefix%', $delimiter = '; ') {
  29. assert(!empty($template) && is_string($template));
  30. $this->_template = $template;
  31. assert(is_string($delimiter));
  32. $this->_delimiter = $delimiter;
  33. parent::NlmPersonStringFilter($filterMode);
  34. }
  35. //
  36. // Getters and Setters
  37. //
  38. /**
  39. * Get the output template
  40. * @return string
  41. */
  42. function getTemplate() {
  43. return $this->_template;
  44. }
  45. /**
  46. * Set the output template
  47. * @param $template string
  48. */
  49. function setTemplate($template) {
  50. $this->_template = $template;
  51. }
  52. /**
  53. * Get the author delimiter (for multiple mode)
  54. * @return string
  55. */
  56. function getDelimiter() {
  57. return $this->_delimiter;
  58. }
  59. /**
  60. * Set the author delimiter (for multiple mode)
  61. * @param $delimiter string
  62. */
  63. function setDelimiter($delimiter) {
  64. $this->_delimiter = $delimiter;
  65. }
  66. //
  67. // Implement template methods from Filter
  68. //
  69. /**
  70. * @see Filter::supports()
  71. * @param $input mixed
  72. * @param $output mixed
  73. * @return boolean
  74. */
  75. function supports(&$input, &$output) {
  76. // Check input type
  77. if (!$this->isValidPersonDescription($input)) return false;
  78. // Check output type
  79. if (is_null($output)) return true;
  80. return is_string($output);
  81. }
  82. /**
  83. * @see Filter::process()
  84. * @param $input mixed a(n array of) MetadataDescription(s)
  85. * @return string
  86. */
  87. function &process(&$input) {
  88. switch ($this->getFilterMode()) {
  89. case PERSON_STRING_FILTER_MULTIPLE:
  90. $personDescription = $this->_flattenPersonDescriptions($input);
  91. break;
  92. case PERSON_STRING_FILTER_SINGLE:
  93. $personDescription = $this->_flattenPersonDescription($input);
  94. break;
  95. default:
  96. assert(false);
  97. }
  98. return $personDescription;
  99. }
  100. //
  101. // Private helper methods
  102. //
  103. /**
  104. * Transform an NLM name description array to a person string.
  105. * NB: We use ; as name separator.
  106. * @param $personDescriptions array an array of MetadataDescriptions
  107. * @return string
  108. */
  109. function _flattenPersonDescriptions(&$personDescriptions) {
  110. assert(is_array($personDescriptions));
  111. $personDescriptionStrings = array_map(array($this, '_flattenPersonDescription'), $personDescriptions);
  112. $personString = implode($this->getDelimiter(), $personDescriptionStrings);
  113. return $personString;
  114. }
  115. /**
  116. * Transform a single NLM name description to a person string.
  117. * NB: We use the style: surname suffix, initials (first-name) prefix
  118. * which is relatively easy to parse back.
  119. * @param $personDescription MetadataDescription
  120. * @return string
  121. */
  122. function _flattenPersonDescription(&$personDescription) {
  123. $nameVars['%surname%'] = (string)$personDescription->getStatement('surname');
  124. $givenNames = $personDescription->getStatement('given-names');
  125. $nameVars['%firstname%'] = $nameVars['%initials%'] = '';
  126. if(is_array($givenNames) && count($givenNames)) {
  127. if (String::strlen($givenNames[0]) > 1) {
  128. $nameVars['%firstname%'] = array_shift($givenNames);
  129. }
  130. foreach($givenNames as $givenName) {
  131. $nameVars['%initials%'] .= String::substr($givenName, 0, 1).'.';
  132. }
  133. }
  134. if (!empty($nameVars['%initials%'])) $nameVars['%initials%'] = ' '.$nameVars['%initials%'];
  135. $nameVars['%prefix%'] = (string)$personDescription->getStatement('prefix');
  136. if (!empty($nameVars['%prefix%'])) $nameVars['%prefix%'] = ' '.$nameVars['%prefix%'];
  137. $nameVars['%suffix%'] = (string)$personDescription->getStatement('suffix');
  138. if (!empty($nameVars['%suffix%'])) $nameVars['%suffix%'] = ' '.$nameVars['%suffix%'];
  139. // Fill placeholders in person template.
  140. $personString = str_replace(array_keys($nameVars), array_values($nameVars), $this->getTemplate());
  141. // Remove empty brackets and trailing/leading whitespace
  142. $personString = trim(str_replace('()', '', $personString));
  143. return $personString;
  144. }
  145. }
  146. ?>