定制 ufo-tech/petrol-prices 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ufo-tech/petrol-prices

最新稳定版本:1.0.0

Composer 安装命令:

composer require ufo-tech/petrol-prices

包简介

Library to fetch and cache petrol prices from public resources.

README 文档

README

Description

Fuel Price Service is a PHP library for fetching and caching up-to-date fuel prices from various providers.
It supports PSR-compliant interfaces for better integration into modern PHP projects.

Features

  • Fetch fuel prices from providers via API.
  • Automatic caching with customizable TTL.
  • Easy addition of new data providers through interfaces.
  • Exception handling for errors during data retrieval.

Installation

Install the library via Composer:

composer require ufo/petrol-prices

Usage

Basic Example

use Ufo\PetrolPrices\FuelPriceService;
use Ufo\PetrolPrices\Providers\AutoRiaFuelPriceProvider;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\HttpClient\HttpClient;

$httpClient = HttpClient::create();
$cache = new FilesystemAdapter();
$provider = new AutoRiaFuelPriceProvider($httpClient);

$service = new FuelPriceService($provider, $cache);

try {
    $fuelPrices = $service->getFuelPrices();
    echo "Date: {$fuelPrices->date}\n";
    echo "A-95 Premium: {$fuelPrices->a95Plus} UAH\n";
    echo "A-95: {$fuelPrices->a95} UAH\n";
    echo "A-92: {$fuelPrices->a92} UAH\n";
    echo "Diesel: {$fuelPrices->diesel} UAH\n";
    echo "Gas: {$fuelPrices->gas} UAH\n";
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

Usage Without Caching

$service = new FuelPriceService($provider);
$fuelPrices = $service->getFuelPrices();

Usage with Custom TTL

$service = new FuelPriceService($provider, $cache, cacheTtl: 7200); // Cache for 2 hours
$fuelPrices = $service->getFuelPrices();

Extending

To add a new provider, implement the IFuelPriceProvider interface:

namespace Ufo\PetrolPrices\Interfaces;

use Ufo\PetrolPrices\DTO\FuelPrice;

interface IFuelPriceProvider
{
    public function getFuelPrices(): FuelPrice;
}

Testing

Run tests using PHPUnit:

vendor/bin/phpunit tests/

License

This project is licensed under the MIT License. See the LICENSE file for details.

统计信息

  • 总下载量: 53
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-14