Tātad Tev vajadzēs kaut ko tādu iekš HTDOCS mapes uztaisīt zem hosta mapi ar nosaukumu: Service tālāk taisam iekš mapes: Factory.php / Registry.php
Factory.php
<?php
declare(strict_types=1);
/*
* This file is part of Swap.
*
* (c) Florian Voutzinos <
[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swap\Service;
use Exchanger\Service\HttpService;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\Psr17FactoryDiscovery;
use Psr\Http\Client\ClientInterface;
use Http\Message\RequestFactory;
use Psr\Http\Message\RequestFactoryInterface;
/**
* Helps building services.
*
* @author Florian Voutzinos <
[email protected]>
*/
final class Factory
{
/**
* The client.
*
* @var HttpClient
*/
private $httpClient;
/**
* The request factory.
*
* @var RequestFactory
*/
private $requestFactory;
/**
* The service registry.
*
* @var Registry
*/
private $registry;
/**
* @param HttpClient|ClientInterface|null $httpClient
* @param RequestFactoryInterface|null $requestFactory
*/
public function __construct($httpClient = null, RequestFactoryInterface $requestFactory = null)
{
if (null === $httpClient) {
$httpClient = HttpClientDiscovery::find();
} else {
if (!$httpClient instanceof ClientInterface && !$httpClient instanceof HttpClient) {
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
}
}
$this->httpClient = $httpClient;
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
$this->registry = new Registry();
}
/**
* Sets the http client.
*
* @param HttpClient|ClientInterface $httpClient
*/
public function setHttpClient($httpClient): void
{
if (!$httpClient instanceof ClientInterface && !$httpClient instanceof HttpClient) {
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
}
$this->httpClient = $httpClient;
}
/**
* Sets the request factory.
*
* @param RequestFactoryInterface $requestFactory
*/
public function setRequestFactory(RequestFactoryInterface $requestFactory): void
{
$this->requestFactory = $requestFactory;
}
/**
* Creates a new service.
*
* @param string $serviceName
* @param array $args
*
* @return \Exchanger\Contract\ExchangeRateService
*/
public function create(string $serviceName, array $args = [])
{
if (!$this->registry->has($serviceName)) {
throw new \LogicException(sprintf('The service "%s" is not registered.', $serviceName));
}
$classOrCallable = $this->registry->get($serviceName);
if (is_callable($classOrCallable)) {
return call_user_func($classOrCallable);
}
if (is_subclass_of($classOrCallable, HttpService::class)) {
return new $classOrCallable($this->httpClient, $this->requestFactory, $args);
}
$r = new \ReflectionClass($classOrCallable);
return $r->newInstanceArgs($args);
}
}
?>
Registry.php
<?php
declare(strict_types=1);
/*
* This file is part of Swap.
*
* (c) Florian Voutzinos <
[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swap\Service;
use Exchanger\Service\Registry as ExchangerRegistry;
/**
* Holds services.
*
* @author Florian Voutzinos <
[email protected]>
*/
final class Registry
{
/**
* The registered services.
*
* @var array
*/
private static $services = [];
/**
* Constructor.
*/
public function __construct()
{
$this->registerServices();
}
/**
* Tells of the registry has the given service.
*
* @param string $name The service name
*
* @return bool
*/
public function has(string $name): bool
{
return isset(self::$services[$name]);
}
/**
* Gets a service.
*
* @param string $name The service name
*
* @return string|null
*/
public function get(string $name)
{
return isset(self::$services[$name]) ? self::$services[$name] : null;
}
/**
* Registers a new service.
*
* @param string $name The service name
* @param string|callable $classOrCallable The class name or a callable
*
* @throws \InvalidArgumentException
*/
public static function register(string $name, $classOrCallable): void
{
self::$services[$name] = $classOrCallable;
}
/**
* Registers the core services.
*/
private function registerServices(): void
{
$services = ExchangerRegistry::getServices();
foreach ($services as $name => $class) {
self::register($name, $class);
}
}
}
?>
Tagad laižam tālāk!
Tagad mums atkal vajadzēs divus failus: Builder.php / Swap.php ->
Builder.php
<?php
declare(strict_types=1);
/*
* This file is part of Swap.
*
* (c) Florian Voutzinos <
[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swap;
use Exchanger\Contract\ExchangeRateService;
use Exchanger\Exchanger;
use Exchanger\Service\Chain;
use Http\Client\HttpClient;
use Psr\SimpleCache\CacheInterface;
use Swap\Service\Factory;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
/**
* Helps building Swap.
*
* @author Florian Voutzinos <
[email protected]>
*/
final class Builder
{
/**
* The services.
*
* @var array
*/
private $services = [];
/**
* The options.
*
* @var array
*/
private $options = [];
/**
* The http client.
*
* @var HttpClient
*/
private $httpClient;
/**
* The request factory.
*
* @var RequestFactoryInterface
*/
private $requestFactory;
/**
* The cache.
*
* @var CacheInterface
*/
private $cache;
/**
* Constructor.
*
* @param array $options
*/
public function __construct(array $options = [])
{
$this->options = $options;
}
/**
* Adds a service by service name.
*
* @param string $serviceName
* @param array $options
*
* @return Builder
*/
public function add(string $serviceName, array $options = []): self
{
$this->services[$serviceName] = $options;
return $this;
}
/**
* Add a service by service instance.
*
* @param ExchangeRateService $exchangeRateService
*
* @return Builder
*/
public function addExchangeRateService(ExchangeRateService $exchangeRateService): self
{
$this->services[spl_object_hash($exchangeRateService)] = $exchangeRateService;
return $this;
}
/**
* Uses the given http client.
*
* @param HttpClient|ClientInterface $httpClient
*
* @return Builder
*/
public function useHttpClient($httpClient): self
{
if (!$httpClient instanceof ClientInterface && !$httpClient instanceof HttpClient) {
throw new \LogicException('Client must be an instance of Http\\Client\\HttpClient or Psr\\Http\\Client\\ClientInterface');
}
$this->httpClient = $httpClient;
return $this;
}
/**
* Uses the given request factory.
*
* @param RequestFactoryInterface $requestFactory
*
* @return Builder
*/
public function useRequestFactory(RequestFactoryInterface $requestFactory): self
{
$this->requestFactory = $requestFactory;
return $this;
}
/**
* Uses the given simple cache.
*
* @param CacheInterface $cache
*
* @return Builder
*/
public function useSimpleCache(CacheInterface $cache): self
{
$this->cache = $cache;
return $this;
}
/**
* Builds Swap.
*
* @return Swap
*/
public function build(): Swap
{
$serviceFactory = new Factory($this->httpClient, $this->requestFactory);
/** @var ExchangeRateService[] $services */
$services = [];
foreach ($this->services as $name => $optionsOrService) {
/** @var array|ExchangeRateService $optionsOrService */
if ($optionsOrService instanceof ExchangeRateService) {
$services[] = $optionsOrService;
} else {
$services[] = $serviceFactory->create($name, $optionsOrService);
}
}
$service = new Chain($services);
$exchanger = new Exchanger($service, $this->cache, $this->options);
return new Swap($exchanger);
}
}
?>
Swap.php
<?php
declare(strict_types=1);
/*
* This file is part of Swap.
*
* (c) Florian Voutzinos <
[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Swap;
use Exchanger\Contract\ExchangeRateProvider;
use Exchanger\ExchangeRateQueryBuilder;
use Exchanger\Contract\ExchangeRate as ExchangeRateContract;
/**
* Swap is an easy to use facade to retrieve exchange rates from various services.
*
* @author Florian Voutzinos <
[email protected]>
*/
class Swap
{
/**
* The exchange rate provider.
*
* @var ExchangeRateProvider
*/
private $exchangeRateProvider;
/**
* Creates a new Swap.
*
* @param ExchangeRateProvider $exchangeRateProvider
*/
public function __construct(ExchangeRateProvider $exchangeRateProvider)
{
$this->exchangeRateProvider = $exchangeRateProvider;
}
/**
* Quotes a currency pair.
*
* @param string $currencyPair The currency pair like "EUR/USD"
* @param array $options An array of query options
*
* @return ExchangeRateContract
*/
public function latest(string $currencyPair, array $options = []): ExchangeRateContract
{
return $this->quote($currencyPair, null, $options);
}
/**
* Quotes a currency pair.
*
* @param string $currencyPair The currency pair like "EUR/USD"
* @param \DateTimeInterface $date An optional date for historical rates
* @param array $options An array of query options
*
* @return ExchangeRateContract
*/
public function historical(string $currencyPair, \DateTimeInterface $date, array $options = []): ExchangeRateContract
{
return $this->quote($currencyPair, $date, $options);
}
/**
* Quotes a currency pair.
*
* @param string $currencyPair The currency pair like "EUR/USD"
* @param \DateTimeInterface $date An optional date for historical rates
* @param array $options An array of query options
*
* @return ExchangeRateContract
*/
private function quote(string $currencyPair, \DateTimeInterface $date = null, array $options = []): ExchangeRateContract
{
$exchangeQueryBuilder = new ExchangeRateQueryBuilder($currencyPair);
if (null !== $date) {
$exchangeQueryBuilder->setDate($date);
}
foreach ($options as $name => $value) {
$exchangeQueryBuilder->addOption($name, $value);
}
$query = $exchangeQueryBuilder->build();
return $this->exchangeRateProvider->getExchangeRate($query);
}
}
?>
Viss ir izdarīts un tālāk jau notiek ievades un izvades! We are done.