/** * Composes a single name based on name parts and flags * * @param boolean $namesort = whether the surname appears first * @return string = composed name */ public function render( WCStyle $style, WCCitationLengthEnum $citationLength, $endSeparator = '', $namesort = False ) { $surname = isset( $this->parts[ WCNamePartEnum::surname ] ) ? $this->parts[ WCNamePartEnum::surname ] : Null; $given = isset( $this->parts[ WCNamePartEnum::given ] ) ? $this->transformGivenNames( $this->parts[ WCNamePartEnum::given ] ) : Null; $link = isset( $this->parts[ WCNamePartEnum::nameLink ] ) ? $this->parts[ WCNamePartEnum::nameLink ] : Null; $dp = isset( $this->parts[ WCNamePartEnum::droppingParticle ] ) ? $this->parts[ WCNamePartEnum::droppingParticle ] : Null; $dpSpace = $ndpSpace = $suffixSpace = ''; if ( $dp ) { # if the dropping particle ends in punctuation, there is no space between it and the surname or dropping particle. if ( preg_match( '/\p{P}$/u', $dp, $matches ) ) { $dpSpace = ''; } else { $dpSpace = $style->nameWhitespace; } } $ndp = isset( $this->parts[ WCNamePartEnum::nonDroppingParticle ] ) ? $this->parts[ WCNamePartEnum::nonDroppingParticle ] : Null; if ( $ndp ) { # if the non-dropping particle ends in punctuation, there is no space between it and the surname. if ( preg_match( '/\p{P}$/u', $ndp, $matches ) ) { $ndpSpace = ''; } else { $ndpSpace = $style->nameWhitespace; } } $suffix = isset( $this->parts[ WCNamePartEnum::suffix ] ) ? $this->parts[ WCNamePartEnum::suffix ] : Null; if ( $suffix ) { $suffixSpace = $style->nameSuffixDelimiter; } $literal = isset( $this->parts[ WCNamePartEnum::literal ] ) ? $this->parts[ WCNamePartEnum::literal ] : Null; # literal/organizational name if ( $literal ) { # Trim redundant separator characters $chrL = mb_substr( $literal, -1, 1 ); $chrR = mb_substr( $endSeparator, 0, 1 ); if ( $chrL == $chrR ) { $endSeparator = ltrim( $endSeparator, $chrR ); } return WCStyle::makeWikiLink( $link, $literal, WCStyle::nameHTML ) . $endSeparator; }; $pre = $post = ''; # short form if ( $citationLength->key == WCCitationLengthEnum::short ) { if ( $surname ) { if ( $ndp ) { $surname = $ndp . $ndpSpace . $surname; } } else { if ( $link ) { $post = $link; } elseif ( $given ) { $post = $given; } else { $post = $dp . $dpSpace . $ndp . $ndpSpace . $style->missingName . $suffixSpace . $suffix; } } } # long form, name sort order elseif ( $namesort ) { if ( $surname ) { if ( !$style->demoteNonDroppingParticle && $ndp ) { $pre = $ndp . $ndpSpace; } if ( $given ) { $post = $style->sortOrderSurnameDelimiter . $given; if ( $dp ) { $post .= $style->nameWhitespace . $dp; } if ( $style->demoteNonDroppingParticle && $ndp ) { if ( $dp ) { $post .= $dpSpace; } else { $post .= $style->nameWhitespace; } $post .= $ndp; } } else { if ( $dp ) { $post .= $style->sortOrderSurnameDelimiter . $dp; } if ( $style->demoteNonDroppingParticle && $ndp ) { if ( $dp ) { $post .= $dpSpace; } else { $post .= $style->nameWhitespace; } $post .= $ndp; } } if ( $suffix ) { $post .= $suffixSpace . $suffix; } } elseif ( $link ) { $post = $link; } elseif ( $given ) { if ( $dp || $ndp || $suffix ) { if ( !$style->demoteNonDroppingParticle && $ndp ) { $post = $ndp . $ndpSpace; } $post = $style->missingName . $style->sortOrderSurnameDelimiter . $given; if ( $dp ) { $post .= $style->nameWhitespace . $dp; } if ( $style->demoteNonDroppingParticle && $ndp ) { if ( $dp ) { $post .= $dpSpace; } else { $post .= $style->nameWhitespace; } $post .= $ndp; } if ( $suffix ) { $post .= $suffixSpace . $suffix; } } else { $post = $given; } } else { if ( !$style->demoteNonDroppingParticle && $ndp ) { $post = $ndp . $ndpSpace; } else { $post = ''; } $post = $style->missingName . $style->sortOrderSurnameDelimiter . $style->missingName; if ( $dp ) { $post .= $style->nameWhitespace . $dp; } if ( $style->demoteNonDroppingParticle && $ndp ) { if ( $dp ) { $post .= $dpSpace; } else { $post .= $style->nameWhitespace; } $post .= $ndp; } if ( $suffix ) { $post .= $suffixSpace . $suffix; } } } # long form, non-name sort order else { if ( $surname ) { if ( $given ) { $pre = $given . $style->nameWhitespace; } $pre .= $dp . $dpSpace . $ndp . $ndpSpace; $post = $suffixSpace . $suffix; } elseif ( $link ) { $post = $link; } elseif ( $given ) { $post = $given . $style->nameWhitespace; if ( $dp || $ndp || $suffix ) { $post .= $dp . $dpSpace . $ndp . $ndpSpace . $style->missingName . $suffixSpace . $suffix; } } else { $post = $style->missingName . $style->nameWhitespace; if ( $dp ) { $post .= $dp . $dpSpace . $ndp . $ndpSpace . $style->missingName . $suffixSpace . $suffix; } } } $htmlClass = WCStyle::nameHTML; if ( $namesort ) { $surnameClass = WCStyle::surnameSortOrderHTML; } else { $surnameClass = WCStyle::surnameHTML; } # Trim redundant separator characters if ( $post ) { $chrL = mb_substr( $post, -1, 1 ); $chrR = mb_substr( $endSeparator, 0, 1 ); if ( $chrL == $chrR ) { $endSeparator = ltrim( $endSeparator, $chrR ); } if ( $surname) { $nameText = $pre . WCStyle::wrapHTMLSpan( $surname, $surnameClass ) . $post; } else { $nameText = $pre . $post; } } elseif ( $surname) { $chrL = mb_substr( $surname, -1, 1 ); $chrR = mb_substr( $endSeparator, 0, 1 ); if ( $chrL == $chrR ) { $endSeparator = ltrim( $endSeparator, $chrR ); } if ( $pre ) { $nameText = $pre . WCStyle::wrapHTMLSpan( $surname, $surnameClass ); } else { $nameText = $surname; $htmlClass .= ' ' . $surnameClass; } } else { $chrL = mb_substr( $pre, -1, 1 ); $chrR = mb_substr( $endSeparator, 0, 1 ); if ( $chrL == $chrR ) { $endSeparator = ltrim( $endSeparator, $chrR ); } $nameText = $pre; } return WCStyle::makeWikiLink( $link, $nameText, $htmlClass ) . $endSeparator; }