Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Units Of Measure Bundle Laravel Package

midnightluke/php-units-of-measure-bundle

Symfony bundle integrating the php-units-of-measure library, providing unit definitions and services to work with quantities, conversions, and formatting in your app. Adds simple configuration and DI wiring for unit-of-measure support.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Domain Alignment: The package provides a structured way to handle units of measure (e.g., length, weight, temperature) in PHP/Laravel applications, which is critical for domains like e-commerce, logistics, healthcare, or scientific applications. It aligns well with Laravel’s ecosystem by offering Doctrine types and form integration.
  • Abstraction Level: The bundle wraps the underlying midnightluke/php-units-of-measure library, abstracting low-level unit conversions and validation. This reduces boilerplate but may limit customization for edge cases.
  • Symfony/Laravel Compatibility: Designed as a Symfony Bundle, it integrates seamlessly with Laravel via Symfony’s bridge (e.g., symfony/flex). However, Laravel’s DI container may require minor adjustments (e.g., service provider configuration).
  • Data Layer Fit: Doctrine types enable direct database storage of units (e.g., meter, kilogram), but requires schema updates if not already normalized (e.g., storing units as strings or enums).

Integration Feasibility

  • Core Features:
    • Doctrine Integration: Adds UnitType for entities (e.g., Product::class with weight as UnitType). Feasible if using Eloquent or Doctrine ORM.
    • Form Types: Provides Symfony form widgets for user input (e.g., dropdowns for units). Laravel’s form handling (e.g., collective/html) may need adapters.
    • Validation: Built-in validation for unit conversions (e.g., rejecting 100cm if only meter is allowed).
  • Dependencies:
    • Requires midnightluke/php-units-of-measure (v1.x) and Symfony components (e.g., form, validator). Laravel’s auto-wiring can mitigate some manual setup.
    • Potential conflict with existing validation libraries (e.g., Laravel’s built-in rules) unless namespaced or overridden.

Technical Risk

  • Low-Medium Risk:
    • Schema Changes: If the database already stores units as raw strings/enums, migration to the bundle’s UnitType may require downtime or complex backfills.
    • Laravel-Specific Gaps: The bundle is Symfony-first; Laravel’s service container or Blade templating may need customizations (e.g., form rendering).
    • Testing Overhead: Unit-heavy applications (e.g., scientific tools) may need extensive validation tests to ensure conversions are accurate.
  • Mitigation:
    • Start with a proof-of-concept in a non-production environment (e.g., test a single entity like Product).
    • Use feature flags to toggle the bundle’s functionality during migration.
    • Monitor performance impact of Doctrine types on query generation.

Key Questions

  1. Database Schema:
    • Are units currently stored as strings/enums, or is the schema already normalized for this bundle?
    • How will existing data migrate to the new UnitType without loss of precision?
  2. Form Handling:
    • Does the team use Symfony forms or Laravel’s native form helpers (e.g., Form::select)? If the latter, how will the bundle’s form types integrate?
  3. Validation Conflicts:
    • Are there existing validation rules (e.g., Laravel’s Rule::in()) that might conflict with the bundle’s validators?
  4. Performance:
    • Will the bundle’s Doctrine types add significant overhead to queries or migrations?
  5. Custom Units:
    • Does the application require custom units beyond the bundle’s defaults (e.g., light-year for a niche app)?

Integration Approach

Stack Fit

  • Laravel Compatibility:
    • The bundle is Symfony-compatible, and Laravel’s integration with Symfony components (via symfony/flex) makes adoption straightforward for most features.
    • Doctrine ORM: Works natively with Laravel if using Doctrine (e.g., via laravel-doctrine/orm). Eloquent users may need a wrapper or hybrid approach.
    • Form Integration: Laravel’s form helpers (e.g., collective/html) may not directly support the bundle’s form types. Options:
      • Use Symfony’s form system alongside Laravel (e.g., via spatie/laravel-symfony-support).
      • Create custom Blade components to render the bundle’s form widgets.
  • Alternative Libraries:
    • Compare with native Laravel solutions (e.g., custom enums + validation) if the bundle’s overhead isn’t justified by its features.

Migration Path

  1. Phase 1: Setup and Validation

    • Install the bundle and midnightluke/php-units-of-measure:
      composer require midnightluke/php-units-of-measure-bundle
      
    • Publish and configure the bundle’s resources (e.g., translations, config) via php artisan vendor:publish.
    • Test basic unit conversions in a seeded environment (e.g., 160cm1.6m).
  2. Phase 2: Doctrine Integration

    • Update entity properties to use UnitType (e.g., #[ORM\Column(type: 'unit')]).
    • Run migrations to update the schema (if needed). Example:
      // Before: $weight = '160cm';
      // After: $weight = new Unit(1.6, 'meter');
      
    • Backfill existing data using a data migration or script.
  3. Phase 3: Form and Validation

    • Replace or extend existing form fields with the bundle’s UnitType form widgets.
    • Update validation logic to leverage the bundle’s built-in rules (e.g., UnitValidator).
  4. Phase 4: Testing and Rollout

    • Write unit/integration tests for critical paths (e.g., unit conversions, form submissions).
    • Gradually roll out to user-facing features, monitoring for errors (e.g., invalid unit submissions).

Compatibility

  • Laravel Versions: Tested with Laravel 8+ (Symfony 5+). Older versions may require polyfills.
  • PHP Version: Requires PHP 8.0+ (due to midnightluke/php-units-of-measure v1.x).
  • Doctrine vs. Eloquent:
    • Doctrine: Full support out-of-the-box.
    • Eloquent: Limited support; may require custom accessors/mutators or a hybrid approach (e.g., store raw values, validate on retrieval).

Sequencing

  1. Non-Critical Features First:
    • Start with internal services (e.g., API endpoints) before public-facing forms.
  2. Isolated Components:
    • Integrate with one entity (e.g., Product) before expanding to others.
  3. Fallback Mechanisms:
    • Implement graceful degradation (e.g., log errors if unit validation fails) during migration.

Operational Impact

Maintenance

  • Pros:
    • Reduced Boilerplate: Centralized unit handling reduces duplicate validation logic.
    • Community Support: MIT-licensed with active upstream (midnightluke/php-units-of-measure).
  • Cons:
    • Dependency Risk: Bundle updates may require testing for breaking changes (e.g., new PHP/Doctrine versions).
    • Debugging Complexity: Unit conversion errors may be harder to trace than simple string/enum checks.
  • Mitigation:
    • Pin bundle versions in composer.json until stability is confirmed.
    • Document custom unit logic for future maintainers.

Support

  • Developer Onboarding:
    • Pros: Clear Doctrine/form integration reduces learning curve for unit-heavy domains.
    • Cons: Developers unfamiliar with Symfony bundles may need training on:
      • Bundle configuration (e.g., config/packages/midnightluke_units.yaml).
      • Form type registration.
  • User Support:
    • Form UX: Users may need guidance on selecting units (e.g., dropdowns vs. free text).
    • Error Handling: Customize validation error messages (e.g., "Invalid unit: 'ft' must be 'meter'").

Scaling

  • Performance:
    • Doctrine Overhead: UnitType may add slight overhead to queries (e.g., type casting). Benchmark with production-like data volumes.
    • Caching: Consider caching frequent unit conversions (e.g., 160cm → 1.6m) if performance is critical.
  • Horizontal Scaling:
    • Stateless by design; no distributed coordination needed for unit conversions.
  • Database Scaling:
    • Schema changes (e.g., adding UnitType) may require downtime or blue-green deployments.

Failure Modes

Failure Scenario Impact Mitigation
Bundle version incompatibility Broken unit conversions/validation Pin versions; test upgrades in staging.
Database migration errors Data corruption Backup before migration; rollback plan.
Form type rendering issues Broken UX Fallback to plain text input with validation.
Custom unit unsupported Invalid data accepted Extend bundle or pre-validate inputs.
High query load from Doctrine types
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity