Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
interface IpFinderInterface
{
public function supports(string $type): bool;
public function find(string $uip): ?string;
}
class IpInfoFinder implements IpFinderInterface
{
public function supports(string $type)
{
return 'one' === $type;
}
public function find(string $ip)
{
return 'one';
}
}
class OtherIpInfoFinder implements IpFinderInterface
{
public function supports(string $type)
{
return 'two' === $type;
}
public function find(string $ip)
{
return 'two';
}
}
class IpResolver
{
private $ipProviders = [];
public function __construct(IpFinderInterface ...$ipFinders)
{
$this->ipProviders = $ipFinders;
}
public function provide(string $type, string $ip)
{
foreach ($this->ipProviders as $ipProvider)
if ($ipProvider->supports($type) {
return $ipProvider->find($ip);
}
throw new Exception('');
}
}
}
$resolver = new IpResolver(
new IpInfoFinder(),
new OtherIpInfoFinder(),
);
$resolver->provide('one', string $ip);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment