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

Fix: TXT parsing #109

Merged
merged 2 commits into from
Mar 24, 2024
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
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());
}
}
Loading