germania-kg/tracking

1.1.0 2023-01-04 07:39 UTC

This package is auto-updated.

Last update: 2024-09-04 11:13:16 UTC


README

处理运输跟踪信息的类、接口和特性

Packagist PHP version Tests

使用 Composer 安装

$ composer require germania-kg/tracking

用法

接口和特性

TrackingInfoInterface

TrackingInfoInterface 提供了两个方法,getTrackingIDgetTrackingLink

TrackingInfoProviderInterface

TrackingInfoProviderInterface 提供一个 getTrackingInfo 方法,返回 TrackingInfoInterfacenull

TrackingInfoProviderTrait

TrackingInfoProviderTrait 引入了一个公共属性 $tracking_info 和一个公共方法 getTrackingInfo,该方法符合 TrackingInfoProviderInterface

TrackingInfoAwareInterface

TrackingInfoAwareInterface 扩展了 TrackingInfoProviderInterface 并提供了一个接受 TrackingInfoInterfacesetTrackingInfo 方法。

TrackingInfoProviderTrait

TrackingInfoProviderTrait 使用 TrackingInfoProviderTrait 并额外提供了一个符合 TrackingInfoAwareInterface 的公共 setTrackingInfo 方法。

TrackingInfo 类

TrackingInfo 类扩展了 TrackingInfoAbstract,实现了 TrackingInfoInterface。它还提供了设置方法 setTrackingIDsetTrackingLink:

<?php
use Germania\Tracking\TrackingInfo;

$ti = new TrackingInfo;
$ti->setTrackingID( "123456" );
$ti->setTrackingLink( "https://parcels.test.com?id=123456" );

echo $ti->getTrackingID();   // "123456"
echo $ti->getTrackingLink(); // "https://parcels.test.com?id=123456"

TrackingInfo 也符合 TrackingInfoProviderInterface,因为其 getTrackingInfo 方法返回自身。该类还实现了 JsonSerializable:

$ti = new TrackingInfo;
$ti->setTrackingID( "123456" );
$ti->setTrackingLink( "https://parcels.test.com?id=123456" );

// As array:
$array = $ti->jsonSerialize();
echo $array['id'];   // "123456"
echo $array['href']; // "https://parcels.test.com?id=123456"

// StdClass:
$encoded = json_encode( $ti );
$decoded = json_decode( $encoded );

echo $decoded->id;   // "123456"
echo $decoded->href; // "https://parcels.test.com?id=123456"

开发

$ git clone https://github.com/GermaniaKG/Tracking.git
$ cd Tracking
$ composer install

单元测试

可以复制 phpunit.xml.distphpunit.xml 并根据您的需求进行调整,或者保持不变。运行 PhpUnit 测试或 composer 脚本,如下所示

$ composer test
# or
$ vendor/bin/phpunit