lokeland/shipping-value-objects

在处理产品和运输时经常使用的值对象的集合。

0.1.1 2023-02-14 08:35 UTC

README

Latest Version on Packagist Tests Total Downloads

此库包含了一组在处理产品和运输时经常使用的值对象。除此之外,它还包含了一些从Illuminate\Collections(来自Laravel)扩展的定制集合。

注意事项

  • 值对象的所有操作都是不可变的。
  • 只添加最相关的(度量)单位,但欢迎提交pull request。

安装

您可以通过composer安装此包

composer require lokeland/shipping-value-objects

使用

值对象

重量

use Lokeland\SVO\Weight;

$weight = Weight::fromGrams(100);
$weight = Weight::fromKilos(100);
$weight = Weight::ofZero();

$weight->toGrams();
$weight->toKilos();

$weight->add(Weight::fromGrams(50));
$weight->subtract(Weight::fromGrams(50));
$weight->multiply(2);

$weight->isZero();
$weight->equal(Weight::fromGrams(50));
$weight->equalOrGreaterThan(Weight::fromGrams(50));
$weight->equalOrLessThan(Weight::fromGrams(50));
$weight->greaterThan(Weight::fromGrams(50));
$weight->lessThan(Weight::fromGrams(50));
$weight->isBetween(
    Weight::fromGrams(10),
    Weight::fromGrams(70)
);

注意值内部以克存储

测量

use Lokeland\SVO\Measurement;

$measurement = Measurement::fromMillimeters(100);
$measurement = Measurement::fromCentimeters(100);
$measurement = Measurement::fromDecimeter(100);
$measurement = Measurement::fromMeters(100);

$measurement->toMillimeters();
$measurement->toCentimeters();
$measurement->toDecimeter();
$measurement->toMeters();

$measurement->isZero();
$measurement->add(Measurement::fromCentimeters(50));
$measurement->subtract(Measurement::fromCentimeters(50));
$measurement->equal(Measurement::fromCentimeters(50));
$measurement->equalOrGreaterThan(Measurement::fromCentimeters(50));
$measurement->equalOrLessThan(Measurement::fromCentimeters(50));
$measurement->greaterThan(Measurement::fromCentimeters(50));
$measurement->lessThan(Measurement::fromCentimeters(50));
$measurement->multiply(2);
$measurement->isBetween(
    Measurement::fromCentimeters(10),
    Measurement::fromCentimeters(70)
);

注意值内部以毫米存储

尺寸

use Lokeland\SVO\Dimensions;
use Lokeland\SVO\Measurement;

$dimensions = Dimensions::make(
    height: Measurement::fromMillimeters(100),
    width: Measurement::fromMillimeters(100),
    length: Measurement::fromMillimeters(100),
);
$dimensions = Dimensions::ofZero();

$dimensions->height;
$dimensions->width;
$dimensions->length;

$dimensions->isZero();

$dimensions->longest();
$dimensions->shortest();

$dimensions->toVolume();
$dimensions->toArray();

运输属性

use Lokeland\SVO\ShippingAttributes;
use Lokeland\SVO\Weight;
use Lokeland\SVO\Dimensions;
use Lokeland\SVO\Measurement;

$shippingAttributes = ShippingAttributes::make(
    weight: Weight::fromKilos(),
    dimensions: Dimensions::make(
        height: Measurement::fromMillimeters(100),
        width: Measurement::fromMillimeters(100),
        length: Measurement::fromMillimeters(100),
    )
);

$shippingAttributes->weight;
$shippingAttributes->dimensions;

$shippingAttributes->toArray();

体积

use Lokeland\SVO\Volume;

$volume = Volume::fromCubicMillimeters(10);
$volume = Volume::fromCubicCentimeters(10);
$volume = Volume::fromCubicDecimeters(10);
$volume = Volume::fromCubicMeters(10);

$volume->toCubicMillimeters();
$volume->toCubicCentimeters();
$volume->toCubicDecimeter();
$volume->toCubicMeters();

$volume->isZero();
$volume->add(Volume::fromCubicCentimeters(50));
$volume->subtract(Volume::fromCubicCentimeters(50));
$volume->equal(Volume::fromCubicCentimeters(50));
$volume->equalOrGreaterThan(Volume::fromCubicCentimeters(50));
$volume->equalOrLessThan(Volume::fromCubicCentimeters(50));
$volume->greaterThan(Volume::fromCubicCentimeters(50));
$volume->lessThan(Volume::fromCubicDecimeters(50));
$volume->multiply(2);
$volume->isBetween(
    Volume::fromCubicCentimeters(10),
    Volume::fromCubicCentimeters(70)
);

注意值内部以立方毫米存储

集合

测量集合

use Lokeland\SVO\Measurement;
use Lokeland\SVO\MeasurementCollection;

$collection = MeasurementCollection::make([
    Measurement::fromDecimeter(10),
    Measurement::fromDecimeter(20),
    Measurement::fromDecimeter(30),
]);

$collection->findLongest();
$collection->findShortest();
$collection->orderByLongToShort();
$collection->orderByShortToLong();
$collection->sumMeasurements();

重量集合

use Lokeland\SVO\Weight;
use Lokeland\SVO\WeightCollection;

$collection = WeightCollection::make([
    Weight::fromKilos(1),
    Weight::fromKilos(2),
    Weight::fromKilos(3),
]);

$collection->findHeaviest();
$collection->findLightest();
$collection->orderByHeavyToLight();
$collection->orderByLightToHeavy();
$collection->sumWeight();

最小值和最大值

在添加或减去值时,您可以提供min/max作为第二个参数,如下所示

use Lokeland\SVO\Measurement;

$measurement = Measurement::fromMeters(1);
$measurement->add(
    measurement: Measurement::fromMeters(5),
    max: Measurement::fromMeters(2),
);
$measurement->toMeters(); // 2

$measurement = Measurement::fromMeters(5);
$measurement->subtract(
    measurement: Measurement::fromMeters(4),
    min: Measurement::fromMeters(3)
);
$measurement->toMeters(); // 3

测试

composer test

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅贡献指南以获取详细信息。

安全漏洞

请审查我们的安全策略了解如何报告安全漏洞。

致谢

使用Spatie的包骨架创建。

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。