Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#115 respecting cs:label position before or after cs:name #127

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Rendering/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function renderSingle($data, $citationNumber = null)
$margin = [];
foreach ($this->children as $key => $child) {
$rendered = $child->render($data, $citationNumber);
$this->getChildsAffixesAndDelimiter($child);
$this->getChildrenAffixesAndDelimiter($child);
if (CiteProc::getContext()->isModeBibliography()
&& $bibliographyOptions->getSecondFieldAlign() === "flush"
) {
Expand Down
48 changes: 43 additions & 5 deletions src/Rendering/Name/Names.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Seboettg\CiteProc\Rendering\HasParent;
use Seboettg\CiteProc\Rendering\Label;
use Seboettg\CiteProc\Rendering\Rendering;
use Seboettg\CiteProc\Rendering\Term\Punctuation;
use Seboettg\CiteProc\RenderingState;
use Seboettg\CiteProc\Style\InheritableNameAttributesTrait;
use Seboettg\CiteProc\Styles\AffixesTrait;
Expand Down Expand Up @@ -110,6 +111,11 @@ class Names implements Rendering, HasParent

private $parent;

/**
* @var bool
*/
private $renderLabelBeforeName = false;

/**
* Names constructor.
*
Expand All @@ -124,10 +130,14 @@ public function __construct(SimpleXMLElement $node, $parent)
/**
* @var SimpleXMLElement $child
*/

foreach ($node->children() as $child) {
switch ($child->getName()) {
case "name":
$this->name = Factory::create($child, $this);
if ($this->label !== null) {
$this->renderLabelBeforeName = true;
}
break;
case "label":
$this->label = Factory::create($child);
Expand Down Expand Up @@ -246,15 +256,27 @@ public function render($data, $citationNumber = null)
* @param $name
* @return string
*/
private function appendLabel($data, $var, $name)
private function appendLabel($data, $var, $name): string
{
$this->label->setVariable($var);
if (in_array($this->label->getForm(), ["verb", "verb-short"])) {
$name = $this->label->render($data).$name;
$renderedLabel = trim($this->label->render($data));
if (empty($renderedLabel)) {
return $name;
}
if ($this->renderLabelBeforeName) {
$delimiter = !in_array(
trim($this->label->renderSuffix()),
Punctuation::getAllPunctuations()
) ? " " : "";
$result = $renderedLabel . $delimiter . trim($name);
} else {
$name .= $this->label->render($data);
$delimiter = !in_array(
trim($this->label->renderPrefix()),
Punctuation::getAllPunctuations()
) ? " " : "";
$result = trim($name) . $delimiter . $renderedLabel;
}
return $name;
return $result;
}

/**
Expand Down Expand Up @@ -381,4 +403,20 @@ private function filterEmpty(array $results)
return !empty($item);
});
}

/**
* @return bool
*/
public function isRenderLabelBeforeName(): bool
{
return $this->renderLabelBeforeName;
}

/**
* @param bool $renderLabelBeforeName
*/
public function setRenderLabelBeforeName(bool $renderLabelBeforeName): void
{
$this->renderLabelBeforeName = $renderLabelBeforeName;
}
}
32 changes: 32 additions & 0 deletions src/Rendering/Term/Punctuation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Seboettg\CiteProc\Rendering\Term;

use MyCLabs\Enum\Enum;
use Seboettg\CiteProc\CiteProc;
use Seboettg\Collection\ArrayList;

class Punctuation extends Enum
{
public const OPEN_QUOTE = "open-quote";
public const CLOSE_QUOTE = "close-quote";
public const OPEN_INNER_QUOTE = "open-inner-quote";
public const CLOSE_INNER_QUOTE = "close-inner-quote";
public const PAGE_RANGE_DELIMITER = "page-range-delimiter";
public const COLON = "colon";
public const COMMA = "comma";
public const SEMICOLON = "semicolon";

public static function getAllPunctuations(): array
{
$values = new ArrayList();
return $values
->setArray(Punctuation::toArray())
->map(function (string $punctuation) {
return CiteProc::getContext()->getLocale()->filter("terms", $punctuation)->single;
})
->collect(function ($items) {
return array_values($items);
});
}
}
2 changes: 1 addition & 1 deletion src/Style/Macro.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function render($data, $citationNumber = null)
/** @var Rendering $child */
foreach ($this->children as $child) {
$res = $child->render($data, $citationNumber);
$this->getChildsAffixesAndDelimiter($child);
$this->getChildrenAffixesAndDelimiter($child);
if (!empty($res)) {
$ret[] = $res;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Style/Options/GlobalOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(SimpleXMLElement $node)
foreach ($node->attributes() as $attribute) {
switch ($attribute->getName()) {
case 'initialize-with-hyphen':
$this->initializeWithHyphen = "false" === (string) $attribute ? false : true;
$this->initializeWithHyphen = !("false" === (string) $attribute);
break;
case 'page-range-format':
$this->pageRangeFormat = new PageRangeFormats((string) $attribute);
Expand All @@ -59,7 +59,7 @@ public function __construct(SimpleXMLElement $node)
* “true”, default) or without (“J.L.”, value “false”).
* @return bool
*/
public function isInitializeWithHyphen()
public function isInitializeWithHyphen(): bool
{
return $this->initializeWithHyphen;
}
Expand All @@ -71,7 +71,7 @@ public function isInitializeWithHyphen()
* not set, page ranges are rendered without reformatting.
* @return PageRangeFormats
*/
public function getPageRangeFormat()
public function getPageRangeFormat(): ?PageRangeFormats
{
return $this->pageRangeFormat;
}
Expand All @@ -80,7 +80,7 @@ public function getPageRangeFormat()
* Sets the display and sorting behavior of the non-dropping-particle in inverted names (e.g. “Koning, W. de”).
* @return DemoteNonDroppingParticle
*/
public function getDemoteNonDroppingParticles()
public function getDemoteNonDroppingParticles(): ?DemoteNonDroppingParticle
{
return $this->demoteNonDroppingParticles;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Styles/ConsecutivePunctuationCharacterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait ConsecutivePunctuationCharacterTrait
* @param $subject
* @return string
*/
public function removeConsecutivePunctuation($punctuationSign, $subject)
public function removeConsecutivePunctuation($punctuationSign, $subject): string
{
if (empty($punctuationSign) || preg_match("/^\s+$/", $punctuationSign)) {
return $subject;
Expand All @@ -52,7 +52,7 @@ public function removeConsecutivePunctuation($punctuationSign, $subject)
/**
* @param $child
*/
protected function getChildsAffixesAndDelimiter($child)
protected function getChildrenAffixesAndDelimiter($child)
{
if (method_exists($child, "renderPrefix")) {
if (!empty($child->renderPrefix()) && !in_array($child->renderPrefix(), $this->childrenPrefixes)) {
Expand Down
147 changes: 147 additions & 0 deletions tests/src/Rendering/Name/NamesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
use PHPUnit\Framework\TestCase;
use Seboettg\CiteProc\CiteProc;
use Seboettg\CiteProc\Context;
use Seboettg\CiteProc\Exception\CiteProcException;
use Seboettg\CiteProc\Exception\InvalidStylesheetException;
use Seboettg\CiteProc\Locale\Locale;
use Seboettg\CiteProc\Rendering\Name\Names;
use Seboettg\CiteProc\Style\Options\DemoteNonDroppingParticle;
use Seboettg\CiteProc\Style\Options\GlobalOptions;
use Seboettg\CiteProc\Style\Options\PageRangeFormats;
use Seboettg\CiteProc\Test\TestSuiteTestCaseTrait;
use SimpleXMLElement;

Expand Down Expand Up @@ -48,4 +54,145 @@ public function testEtAlWithCombined()
//$this->_testRenderTestSuite("name_EtAlWithCombined");
}
*/

/**
* @throws Exception
*/
public function testRenderLabelBeforeNameShouldBeTrueIfLabelTagBeforeName()
{
$csl = <<<EOD
<names variable="translator">
<label form="verb-short" prefix=", "/>
<name name-as-sort-order="first" and="text" sort-separator=", " delimiter=", " delimiter-precedes-last="always"/>
</names>
EOD;
CiteProc::setContext(new Context());
$style = new SimpleXMLElement($csl);
$names = new Names($style, null);
$this->assertTrue($names->isRenderLabelBeforeName());
}

/**
* @throws InvalidStylesheetException
* @throws Exception
*/
public function testRenderLabelBeforeNameShouldBeFalseIfLabelTagAfterName()
{
$csl = <<<EOD
<names variable="translator">
<name name-as-sort-order="first" and="text" sort-separator=", " delimiter=", " delimiter-precedes-last="always"/>
<label form="verb-short" prefix=", "/>
</names>
EOD;
CiteProc::setContext(new Context());
$style = new SimpleXMLElement($csl);
$names = new Names($style, null);
$this->assertFalse($names->isRenderLabelBeforeName());
}

/**
* @throws CiteProcException
* @throws InvalidStylesheetException
* @throws Exception
*/
public function testLabelShouldAppearAfterNameIfLabelTagAfterNameTag()
{
$csl = <<<EOD
<names variable="translator">
<name name-as-sort-order="first" and="text" sort-separator=", " delimiter=", " delimiter-precedes-last="always"/>
<label form="verb-short" prefix=", " />
</names>
EOD;
$data = <<<EOD
{
"type": "book",
"issued": {
"date-parts": [
[
1546
]
]
},
"publisher": "Jaques Kerver",
"translator": [
{
"given": "Jean",
"family": "Martin"
}
]
}
EOD;
$expectedResult = "Jean Martin, trans. by";
$this->mockContext();

$style = new SimpleXMLElement($csl);
$names = new Names($style, null);
$actualResult = $names->render(json_decode($data));
$this->assertEquals($expectedResult, $actualResult);
}

/**
* @throws CiteProcException
* @throws InvalidStylesheetException
* @throws Exception
*/
public function testLabelShouldAppearBeforeNameIfLabelTagBeforeNameTag()
{
$csl = <<<EOD
<names variable="translator">
<label form="verb-short" prefix=", " />
<name name-as-sort-order="first" and="text" sort-separator=", " delimiter=", " delimiter-precedes-last="always"/>
</names>
EOD;
$data = <<<EOD
{
"type": "book",
"issued": {
"date-parts": [
[
1546
]
]
},
"publisher": "Jaques Kerver",
"translator": [
{
"given": "Jean",
"family": "Martin"
}
]
}
EOD;
$expectedResult = ", trans. by Jean Martin";
$this->mockContext();

$style = new SimpleXMLElement($csl);
$names = new Names($style, null);
$actualResult = $names->render(json_decode($data));
$this->assertEquals($expectedResult, $actualResult);
}

private function mockContext()
{
$globalOptionsMock = $this->createMock(GlobalOptions::class);
$globalOptionsMock
->method('getDemoteNonDroppingParticles')
->willReturn(DemoteNonDroppingParticle::NEVER());
$globalOptionsMock
->method('getPageRangeFormat')
->willReturn(PageRangeFormats::CHICAGO());
$globalOptionsMock
->method('isInitializeWithHyphen')
->willReturn(false);

$stub = $this->createMock(Context::class);
$stub
->method('getGlobalOptions')
->willReturn($globalOptionsMock);

$context = new Context();
$context->setLocale(new Locale());
$context->setGlobalOptions($globalOptionsMock);
CiteProc::setContext($context);
}
}