Skip to content

Commit

Permalink
add AlternativeText::fromRawString (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikbosch authored May 25, 2021
1 parent 0d00c1e commit 82f6e54
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/AlternativeText.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ final class AlternativeText
*/
private $text;

/**
* @var bool
*/
private $shouldNormalizeSpace = true;

/**
* @param string $text
*/
Expand Down Expand Up @@ -41,7 +46,11 @@ public function getRaw(): string
*/
public function __toString(): string
{
return $this->normalizeSpace($this->text);
if ($this->shouldNormalizeSpace) {
return $this->normalizeSpace($this->text);
}

return $this->text;
}

/**
Expand Down Expand Up @@ -85,6 +94,17 @@ public static function fromEncodedText(string $text, string $charset): Alternati
return new self($converted);
}

/**
* @param string $text
* @return AlternativeText
*/
public static function fromRawString(string $text): AlternativeText
{
$self = new self($text);
$self->shouldNormalizeSpace = false;
return $self;
}

/**
* @param string $html
* @return AlternativeText
Expand Down
10 changes: 10 additions & 0 deletions test/Unit/AlternativeTextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ public function it_strips_tags_on_dom_exception(): void
$this->assertEquals($text, (string) $alternativeText);
}

/**
* @test
*/
public function it_does_not_normalize_space_for_raw_strings(): void
{
$text = ' test ';
$alternativeText = AlternativeText::fromRawString($text);
$this->assertEquals($text, (string) $alternativeText);
}

/**
* @return array
*/
Expand Down

0 comments on commit 82f6e54

Please sign in to comment.