postgresql-for-doctrine icon indicating copy to clipboard operation
postgresql-for-doctrine copied to clipboard

Add Range types

Open janklan opened this issue 6 months ago • 5 comments

This PR complements https://github.com/martin-georgiev/postgresql-for-doctrine/pull/263 that added support for range functions. It adds the support for simple mapping of range types in Doctrine entities.

Summary by CodeRabbit

  • New Features

    • Added support for PostgreSQL numeric range (NUMRANGE) types, enabling conversion between database range strings and PHP objects.
    • Introduced a numeric range object with methods for parsing, string formatting, and containment checks.
    • Provided utility functions for numeric range calculations and string-to-number conversions.
  • Tests

    • Added comprehensive unit tests for numeric range utility functions, covering range containment and string-to-number conversion scenarios.

janklan avatar May 07 '25 02:05 janklan

@martin-georgiev I've been mapping various range types to my entities for ages and so I thought you might be interested in having the funcitonality in this package. I would be interested in that too, beacuse this PR, together with the others I have recently filed, would allow me to reduce the number of dependencies by consolidating various 3rd party and custom Doctrine extensions into your package.

Obviously I also think others might benefit, and it's time for me to give back to the community.

I've only added the NumRange type as a PoC for you to have a look and tell me WDYT. I didn't want to waste time should you ultimately reject the idea of hosting these things here.

I'm looking forward to your response. Also I tried to reach out via DM on Symfony's slack. It might be a faster thing to talk if you're interested.

janklan avatar May 07 '25 02:05 janklan

Walkthrough

This update introduces a set of new classes and interfaces to support numeric range handling in PHP, especially for PostgreSQL's NUMRANGE type. It includes a Doctrine DBAL type for numrange, generic and arithmetic range models, utility methods for numeric operations, and comprehensive unit tests for the utility functions.

Changes

File(s) Change Summary
src/MartinGeorgiev/Doctrine/DBAL/Types/NumRange.php Added a Doctrine DBAL type class NumRange for PostgreSQL NUMRANGE, implementing conversion between database strings and PHP ArithmeticRange objects with validation and error handling.
src/MartinGeorgiev/Model/ArithmeticRange.php Introduced the ArithmeticRange class, a generic range model for numeric types, supporting parsing from string, containment checks, and string formatting.
src/MartinGeorgiev/Model/BaseRange.php Added the abstract BaseRange class implementing RangeInterface, providing methods to inspect range boundaries (infinite, empty, single/both boundaries).
src/MartinGeorgiev/Model/RangeInterface.php Introduced the RangeInterface interface, defining required methods for range objects including containment and boundary checks.
src/MartinGeorgiev/Utils/MathUtils.php Added the MathUtils utility class with static methods for checking if a value is within a range (inRange) and converting strings to numbers (stringToNumber).
tests/Unit/MartinGeorgiev/Utils/MathUtilsTest.php Added PHPUnit tests for MathUtils, covering range checking and string-to-number conversion with various edge cases and input scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant DB as PostgreSQL Database
    participant NumRange as NumRange Type
    participant PHP as PHP Application
    participant Range as ArithmeticRange

    DB->>NumRange: Provide NUMRANGE string
    NumRange->>NumRange: convertToPHPValue(value)
    NumRange->>Range: ArithmeticRange::createFromString(value)
    Range-->>NumRange: ArithmeticRange object
    NumRange-->>PHP: Return ArithmeticRange

    PHP->>NumRange: Provide ArithmeticRange object
    NumRange->>NumRange: convertToDatabaseValue(value)
    NumRange-->>DB: Return NUMRANGE string

Poem

In the garden of code, new ranges appear,
Numbers stretch far, both far and near.
With MathUtils to guide, and tests to defend,
Now numrange and friends on Doctrine depend.
From stringy bounds to numbers true—
🐇 This rabbit says, "Bravo! Well done, crew!"

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar May 07 '25 02:05 coderabbitai[bot]

@martin-georgiev let me re-visit this one quickly.

janklan avatar May 12 '25 23:05 janklan

@janklan It would be interesting to add int4range and int8range as well.

seb-jean avatar May 19 '25 10:05 seb-jean

@janklan Do you need help? What do you have left to do?

seb-jean avatar May 24 '25 05:05 seb-jean

Closing in favour of #396 which got released as part of https://github.com/martin-georgiev/postgresql-for-doctrine/pull/393

martin-georgiev avatar Jul 30 '25 00:07 martin-georgiev