Skip to content

Instantly share code, notes, and snippets.

@lolychank
Last active November 2, 2019 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lolychank/f41d0a87f8d578612ae74decf6d7636e to your computer and use it in GitHub Desktop.
Save lolychank/f41d0a87f8d578612ae74decf6d7636e to your computer and use it in GitHub Desktop.
<?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