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.
*/namespaceSwap\Service;useExchanger\Service\HttpService;useHttp\Client\HttpClient;useHttp\Discovery\HttpClientDiscovery;useHttp\Discovery\Psr17FactoryDiscovery;usePsr\Http\Client\ClientInterface;useHttp\Message\RequestFactory;usePsr\Http\Message\RequestFactoryInterface;/**
* Helps building services.
*
* @author Florian Voutzinos <[email protected]>
*/finalclassFactory{/**
* 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
*/publicfunction __construct($httpClient =null,RequestFactoryInterface $requestFactory =null){if(null=== $httpClient){
$httpClient =HttpClientDiscovery::find();}else{if(!$httpClient instanceofClientInterface&&!$httpClient instanceofHttpClient){thrownew \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 =newRegistry();}/**
* Sets the http client.
*
* @param HttpClient|ClientInterface $httpClient
*/publicfunction setHttpClient($httpClient):void{if(!$httpClient instanceofClientInterface&&!$httpClient instanceofHttpClient){thrownew \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
*/publicfunction setRequestFactory(RequestFactoryInterface $requestFactory):void{
$this->requestFactory = $requestFactory;}/**
* Creates a new service.
*
* @param string $serviceName
* @param array $args
*
* @return \Exchanger\Contract\ExchangeRateService
*/publicfunction create(string $serviceName, array $args =[]){if(!$this->registry->has($serviceName)){thrownew \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)){returnnew $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.
*/namespaceSwap\Service;useExchanger\Service\Registry asExchangerRegistry;/**
* Holds services.
*
* @author Florian Voutzinos <[email protected]>
*/finalclassRegistry{/**
* The registered services.
*
* @var array
*/privatestatic $services =[];/**
* Constructor.
*/publicfunction __construct(){
$this->registerServices();}/**
* Tells of the registry has the given service.
*
* @param string $name The service name
*
* @return bool
*/publicfunction has(string $name):bool{return isset(self::$services[$name]);}/**
* Gets a service.
*
* @param string $name The service name
*
* @return string|null
*/publicfunctionget(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
*/publicstaticfunctionregister(string $name, $classOrCallable):void{self::$services[$name]= $classOrCallable;}/**
* Registers the core services.
*/privatefunction 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.
*/namespaceSwap;useExchanger\Contract\ExchangeRateService;useExchanger\Exchanger;useExchanger\Service\Chain;useHttp\Client\HttpClient;usePsr\SimpleCache\CacheInterface;useSwap\Service\Factory;usePsr\Http\Client\ClientInterface;usePsr\Http\Message\RequestFactoryInterface;/**
* Helps building Swap.
*
* @author Florian Voutzinos <[email protected]>
*/finalclassBuilder{/**
* 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
*/publicfunction __construct(array $options =[]){
$this->options = $options;}/**
* Adds a service by service name.
*
* @param string $serviceName
* @param array $options
*
* @return Builder
*/publicfunctionadd(string $serviceName, array $options =[]):self{
$this->services[$serviceName]= $options;return $this;}/**
* Add a service by service instance.
*
* @param ExchangeRateService $exchangeRateService
*
* @return Builder
*/publicfunction addExchangeRateService(ExchangeRateService $exchangeRateService):self{
$this->services[spl_object_hash($exchangeRateService)]= $exchangeRateService;return $this;}/**
* Uses the given http client.
*
* @param HttpClient|ClientInterface $httpClient
*
* @return Builder
*/publicfunction useHttpClient($httpClient):self{if(!$httpClient instanceofClientInterface&&!$httpClient instanceofHttpClient){thrownew \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
*/publicfunction useRequestFactory(RequestFactoryInterface $requestFactory):self{
$this->requestFactory = $requestFactory;return $this;}/**
* Uses the given simple cache.
*
* @param CacheInterface $cache
*
* @return Builder
*/publicfunction useSimpleCache(CacheInterface $cache):self{
$this->cache = $cache;return $this;}/**
* Builds Swap.
*
* @return Swap
*/publicfunction build():Swap{
$serviceFactory =newFactory($this->httpClient, $this->requestFactory);/** @var ExchangeRateService[] $services */
$services =[];foreach($this->services as $name => $optionsOrService){/** @var array|ExchangeRateService $optionsOrService */if($optionsOrService instanceofExchangeRateService){
$services[]= $optionsOrService;}else{
$services[]= $serviceFactory->create($name, $optionsOrService);}}
$service =newChain($services);
$exchanger =newExchanger($service, $this->cache, $this->options);returnnewSwap($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.
*/namespaceSwap;useExchanger\Contract\ExchangeRateProvider;useExchanger\ExchangeRateQueryBuilder;useExchanger\Contract\ExchangeRate asExchangeRateContract;/**
* Swap is an easy to use facade to retrieve exchange rates from various services.
*
* @author Florian Voutzinos <[email protected]>
*/classSwap{/**
* The exchange rate provider.
*
* @var ExchangeRateProvider
*/private $exchangeRateProvider;/**
* Creates a new Swap.
*
* @param ExchangeRateProvider $exchangeRateProvider
*/publicfunction __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
*/publicfunction 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
*/publicfunction 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
*/privatefunction quote(string $currencyPair, \DateTimeInterface $date =null, array $options =[]):ExchangeRateContract{
$exchangeQueryBuilder =newExchangeRateQueryBuilder($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.
Valūtas kursi
in Gatavi skripti
Posted
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
Registry.php
Tagad laižam tālāk!
Tagad mums atkal vajadzēs divus failus: Builder.php / Swap.php ->
Builder.php
Swap.php
Viss ir izdarīts un tālāk jau notiek ievades un izvades! We are done.