Skip to content

Commit

Permalink
add custom provider support
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Apr 6, 2024
1 parent 16f90cb commit a57028f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var_dump(

```
var_dump(
\Yggverse\Net\Dig::records('yo.index', ['A', 'AAAA'])
\Yggverse\Net\Dig::records('yo.index', ['A', 'AAAA'], &$result = [], &$error = [], $provider = null)
);
```

Expand Down
41 changes: 34 additions & 7 deletions src/Dig.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,59 @@ private static function _records(): array
];
}

public static function isProvider(mixed $value): bool
{
return
(
is_string($value) &&
false !== filter_var($value, FILTER_VALIDATE_IP)
);
}

public static function isHostName(mixed $value, array $find = ['_'], array $replace = []): bool
{
return is_string($value) && false !== filter_var(str_replace($find, $replace, $value), FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
return (
is_string($value) &&
false !== filter_var(str_replace($find, $replace, $value), FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)
);
}

public static function isRecord(mixed $value): bool
{
return is_string($value) && isset(self::_records()[$value]);
return
(
is_string($value) &&
isset(self::_records()[$value])
);
}

public static function isRecordValue(mixed $record, mixed $value): bool
{
return is_string($record) &&
is_string($value) &&
isset(self::_records()[$record]) && self::_records()[$record]($value);
return
(
is_string($record) &&
is_string($value) &&
isset(self::_records()[$record]) && self::_records()[$record]($value)
);
}

public static function records(string $hostname, array $records, array &$result = [], array &$error = []): array
public static function records(string $hostname, array $records, array &$result = [], array &$error = [], ?string $provider = null): array
{
if (self::isProvider($provider))
{
$provider = sprintf(
'@%s',
$provider
);
}

if (self::isHostName($hostname))
{
foreach ($records as $record)
{
if (self::isRecord($record))
{
if ($values = exec(sprintf('dig %s %s +short', $record, $hostname)))
if ($values = exec(sprintf('dig %s %s %s +short', (string) $provider, (string) $record, (string) $hostname)))
{
foreach (explode(PHP_EOL, $values) as $value)
{
Expand Down

0 comments on commit a57028f

Please sign in to comment.