Skip to content

Commit

Permalink
Merge pull request #109 from SRWieZ/fix_txt_parsing
Browse files Browse the repository at this point in the history
Fix: TXT parsing
  • Loading branch information
freekmurze authored Mar 24, 2024
2 parents 752cec3 + 204b502 commit 712d612
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/Records/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,18 @@ public function __call(string $name, array $arguments)

protected static function lineToArray(string $line, ?int $limit = null): array
{
return explode(
' ',
preg_replace('/\s+/', ' ', $line),
$limit
);
// Match non-space characters, escaped quotes within quotes, or characters within quotes
preg_match_all('/(?:\\\\["]|[^"\\s]+|"[^"\\\\]*(?:\\\\.[^"\\\\]*)*")+/u', $line, $matches);
$parts = $matches[0];

// If a limit is defined, handle it manually because preg_match_all doesn't support limit
if ($limit !== null && count($parts) > $limit) {
$lastPart = implode(' ', array_slice($parts, $limit - 1));
$parts = array_slice($parts, 0, $limit - 1);
$parts[] = $lastPart;
}

return $parts;
}

protected function cast(string $attribute, $value)
Expand All @@ -108,7 +115,11 @@ protected function prepareInt($value): int

protected function prepareText(string $value): string
{
return str_replace('" "', '', trim($value, '"'));
if (str_starts_with($value, '"') && str_ends_with($value, '"')) {
$value = substr($value, 1, -1);
}

return str_replace('" "', '', $value);
}

protected function castHost(string $value): string
Expand Down
2 changes: 1 addition & 1 deletion tests/DnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function it_can_fetch_ptr_record()
'class' => 'IN',
'ttl' => 3600,
'type' => 'PTR',
'target' => 'ae0.452.fra.as205948.creoline.net.',
'target' => 'ae0.452.fra1.de.creoline.net.',
]);

$this->assertSame(
Expand Down
22 changes: 22 additions & 0 deletions tests/Records/TXTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,26 @@ public function it_return_null_for_to_few_attributes()

$this->assertNull($record);
}
/** @test */
public function it_can_parse_a_string_with_double_space()
{
$record = TXT::parse('spatie.be. 594 IN TXT "test 2 7"');

$this->assertSame('spatie.be', $record->host());
$this->assertSame(594, $record->ttl());
$this->assertSame('IN', $record->class());
$this->assertSame('TXT', $record->type());
$this->assertSame('test 2 7', $record->txt());
}
/** @test */
public function it_can_parse_a_string_with_a_double_quote()
{
$record = TXT::parse('spatie.be. 594 IN TXT "test \""');

$this->assertSame('spatie.be', $record->host());
$this->assertSame(594, $record->ttl());
$this->assertSame('IN', $record->class());
$this->assertSame('TXT', $record->type());
$this->assertSame('test \\"', $record->txt());
}
}

0 comments on commit 712d612

Please sign in to comment.