Last active
November 2, 2019 20:31
-
-
Save lolychank/f41d0a87f8d578612ae74decf6d7636e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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