承接 withinboredom/distance 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

withinboredom/distance

最新稳定版本:v1.0.0

Composer 安装命令:

composer require withinboredom/distance

包简介

typesafe distance

README 文档

README

Identity

This library uses some tricks to intern values so that two distances are always equal to one another, no matter the distance in time or space.

use Withinboredom\Distance;
use Withinboredom\Distance\Unit;

$meter = Distance::from(Unit::Meters, 1);
$centimeters = Distance::from(Unit::Centimeters, 100);

echo $meter === $centimeters ? 'true' : 'false'
// outputs: true

Type Safety

You can ensure nobody will accidentally confuse inches with centimeters or Nautical Miles with Miles:

function moveForward(Distance $distance) { /* move */ }

moveForward(Distance::from(Unit::Miles, 10));

Conversions and Math

You can convert between units and even perform operations, like sorting and arithmetic:

$meter = Meters(1);

$meter = $meter->multiply(10)->add(Inches(10)); // get 10 meters and 10 inches

echo Centimeters(10) < $meter ? 'true' : 'false';
// output: true

Support for Crell\Serde

You cannot serialize/deserialize/clone Distance objects. However, if you use something like Serde, you can still serialize your value objects:

class Robot {
    public function __construct(
        #[Field('height_in_centimeters')]
        #[DistanceAs(Unit::Centimeters)]
        public Distance $height,
    ) {}
}

$serde = new SerdeCommon(handlers: new \Withinboredom\Distance\SerdeExporter());
$serde->serialize(new Robot(Meters(5)), 'json');

The above will be serialized (and deserialized) from:

{
    "height_in_centimeters": 500
}

Units

There are several distance units supported

SI Units

  • Micrometers
  • Millimeters
  • Centimeters
  • Meters
  • Kilometers

Imperial Units

  • Inches
  • Feet
  • Yards
  • Miles
  • Furlongs

Misc.

  • Nautical Miles
  • Astronomical Units

FAQ

How far can I go?

The base unit is in micrometers, so on a 64-bit system you can go ~60AU with micrometer accuracy. For reference, Pluto is usually about 40 AU away from the sun.

If you are limited to 32-bit systems, this is not for you. The maximum distance on a 32-bit system is just a couple of meters.

Why does this exist?

I don’t like magic numbers.

How performant is this?

The main overhead is in autoloading and function-call overhead. Thus, if realtime performance is a concern, you might want to stick to magic numbers.

Developing

If you wish to create a PR or update the code here:

  1. Clone the repo
  2. composer install to install test dependencies
  3. yarn to install git hooks for formatting
  4. Open in favorite IDE.

Code Standards

Per coding styles are followed.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-27