postgresql-for-doctrine
postgresql-for-doctrine copied to clipboard
Add Range types
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.
@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.
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.
🪧 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
@coderabbitaiin 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
@coderabbitaiin 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 pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere 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.
@martin-georgiev let me re-visit this one quickly.
@janklan It would be interesting to add int4range and int8range as well.
@janklan Do you need help? What do you have left to do?
Closing in favour of #396 which got released as part of https://github.com/martin-georgiev/postgresql-for-doctrine/pull/393