{"id":29312780,"url":"https://github.com/khaledalam/unit","last_synced_at":"2025-07-07T09:07:49.935Z","repository":{"id":303142218,"uuid":"1014533313","full_name":"khaledalam/unit","owner":"khaledalam","description":"A lightweight PHP library for working with physical quantities and unit conversions. Supports arithmetic operations, precision formatting, and dimensional analysis.","archived":false,"fork":false,"pushed_at":"2025-07-05T23:36:11.000Z","size":0,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-05T23:45:39.803Z","etag":null,"topics":["composer-package","dimensional-analysis","match","measurement","php","php-library","physics","quantity","unit-conversion","unit-converter"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/khaledalam/unit","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/khaledalam.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-05T23:07:06.000Z","updated_at":"2025-07-05T23:35:59.000Z","dependencies_parsed_at":"2025-07-05T23:45:42.348Z","dependency_job_id":"d993d406-ccae-4a7a-b827-5c6b76a11a06","html_url":"https://github.com/khaledalam/unit","commit_stats":null,"previous_names":["khaledalam/unit"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/khaledalam/unit","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khaledalam%2Funit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khaledalam%2Funit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khaledalam%2Funit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khaledalam%2Funit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khaledalam","download_url":"https://codeload.github.com/khaledalam/unit/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khaledalam%2Funit/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264049015,"owners_count":23549396,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composer-package","dimensional-analysis","match","measurement","php","php-library","physics","quantity","unit-conversion","unit-converter"],"created_at":"2025-07-07T09:07:46.240Z","updated_at":"2025-07-07T09:07:49.928Z","avatar_url":"https://github.com/khaledalam.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unit\n\n[![Latest Stable Version](https://poser.pugx.org/khaledalam/unit/v)](https://packagist.org/packages/KhaledAlam/Unit)\n[![Push](https://github.com/KhaledAlam/Unit/actions/workflows/push.yml/badge.svg)](https://github.com/KhaledAlam/Unit/actions/workflows/push.yml)\n[![codecov](https://codecov.io/gh/KhaledAlam/Unit/graph/badge.svg?token=4MIM2LRPRD)](https://codecov.io/gh/KhaledAlam/Unit)\n[![License](https://poser.pugx.org/khaledalam/unit/license)](https://packagist.org/packages/khaledalam/unit)\n\n### PHP Units \u0026 Dimensions Library\n\nA lightweight, type-safe PHP library for working with **quantities**, **units**, and **dimensional analysis**. Inspired by scientific computing needs, this library lets you define units, register them globally, perform arithmetic with dimension checking, and convert across compatible units.\n\n---\n\n## Features\n\n- [x] Immutable objects\n- [x] Dimensionally-aware arithmetic (`add`, `subtract`, `multiply`, `divide`)\n- [x] Automatic conversion between compatible units (e.g., cm to m)\n- [x] Support for compound units (e.g., m/s, kg⋅m²/s²)\n- [x] Enum-powered unit naming (`Name` enum)\n- [x] Custom unit registry\n\n---\n\n## Installation\n\n```bash\ncomposer require khaledalam/unit\n```\n\n---\n\n## Basic Usage\n\n```php\n\u003c?php\n// test.php\n\n// __construct(string $name, Name|string $symbol, float $factor, Dimension $dimension)\n\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse KhaledAlam\\Unit\\Unit;\nuse KhaledAlam\\Unit\\Name;\nuse KhaledAlam\\Unit\\Dimension;\nuse KhaledAlam\\Unit\\Quantity;\nuse KhaledAlam\\Unit\\UnitRegistry;\n\n// Register base units\nUnitRegistry::register(new Unit(Name::M-\u003evalue, Name::M, 1.0, new Dimension(['L' =\u003e 1])));\n\nUnitRegistry::register(new Unit(Name::CM-\u003evalue, Name::CM, 0.01, new Dimension(['L' =\u003e 1])));\n\n// Create quantities\n$length1 = Quantity::from(2.0, 'm');\n$length2 = Quantity::from(100.0, 'cm');\n\n// Add quantities (auto conversion)\n$sum = $length1-\u003eadd($length2); // Result: 3.0 m\n\necho $sum; // \"3 m\"\n\n?\u003e\n```\noutput:\n```bash\n% php main.php\n3 m\n```\n\n---\n\n## Arithmetic Support\n\n```php\n$velocity = Quantity::from(10, 'm')-\u003edivide(Quantity::from(2, 's'));  // 5 m/s\n$area = Quantity::from(2, 'm')-\u003emultiply(Quantity::from(3, 'm'));     // 6 m²\n```\n\nAll operations return new `Quantity` objects with proper dimensions and units.\n\n---\n\n## Unit Registration\n\nDefine and register your own units:\n\n```php\nUnitRegistry::register(new Unit('inch', Name::INCH, 0.0254, new Dimension(['L' =\u003e 1])));\n```\n\n---\n\n## Exception Handling\n\nOperations on incompatible dimensions will throw an exception:\n\n```php\n$mass = Quantity::from(5, 'kg');\n$time = Quantity::from(3, 's');\n\n$mass-\u003eadd($time); // InvalidArgumentException\n```\n\n---\n\n## Running Tests\n\nThis project uses PHP-internal's built-in `run-tests.php` format.\n\n```bash\nphp run-tests.php tests/ --show-diff\n```\n\nOr \n\n```bash\n./vendor/bin/phpunit \\\n    --configuration phpunit.xml.dist \\\n    --testsuite=unit\n```\n\nEach test follows `.phpt` format and validates expected behavior.\n\n---\n\n## Project Structure\n\n```\nsrc/\n  └── Unit/\n       ├── Quantity.php\n       ├── Unit.php\n       ├── Dimension.php\n       ├── UnitRegistry.php\n       └── Name.php (enum)\n\ntests/\n  ├── add/\n  ├── convert/\n  ├── divide/\n  └── ...\n```\n\n---\n\n## About\n\nBuilt by **[Khaled Alam](https://khaledalam.net/)** to bring better scientific and data modeling features to PHP developers.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhaledalam%2Funit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhaledalam%2Funit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhaledalam%2Funit/lists"}