gryfoss/symfony-unique-in-collection-constraint
Composer 安装命令:
composer require gryfoss/symfony-unique-in-collection-constraint
包简介
Allows validation of objects inside a collection by a provided field.
README 文档
README
A Symfony validation constraint that ensures uniqueness of specific fields within a collection. This constraint validates that the specified field(s) have unique values across all items in a collection, making it perfect for preventing duplicate entries based on certain properties.
🚀 Features
- Field Uniqueness: Validate uniqueness of single or multiple fields within collections
- Composite Uniqueness: Support for checking uniqueness across multiple field combinations
- Symfony Integration: Native Symfony Validator component integration
- PHP 8+ Attributes: Modern attribute-based constraint definition
- 100% Test Coverage: Comprehensive unit and functional test suite
📋 Requirements
- PHP 8.2 or higher
- Symfony Validator Component 7.3+
- Symfony PropertyAccess Component 7.3+
📦 Installation
Install the constraint via Composer:
composer require gryfoss/symfony-unique-in-collection-constraint
🛠️ Usage
Basic Usage with PHP Attributes
You can use the constraint directly on collection properties in your entities:
<?php use GryfOSS\SymfonyUniqueInCollectionConstraint\UniqueInCollection; class Team { #[UniqueInCollection('email')] private array $members = []; // Constructor, getters, setters... }
Advanced Usage Examples
Single Field Uniqueness
<?php use GryfOSS\SymfonyUniqueInCollectionConstraint\UniqueInCollection; class Company { #[UniqueInCollection('email')] private array $employees = []; } // Usage $employees = [ ['name' => 'John Doe', 'email' => 'john@company.com'], ['name' => 'Jane Smith', 'email' => 'jane@company.com'], ['name' => 'Bob Wilson', 'email' => 'john@company.com'], // ❌ Duplicate email! ];
Multiple Field Uniqueness (Composite)
<?php use GryfOSS\SymfonyUniqueInCollectionConstraint\UniqueInCollection; class ProductCatalog { #[UniqueInCollection(['category', 'sku'])] private array $products = []; } // Usage $products = [ ['name' => 'Laptop', 'category' => 'electronics', 'sku' => 'LAP001'], ['name' => 'Phone', 'category' => 'electronics', 'sku' => 'PHN001'], ['name' => 'Tablet', 'category' => 'electronics', 'sku' => 'LAP001'], // ❌ Same category+sku! ];
Manual Validation
<?php use GryfOSS\SymfonyUniqueInCollectionConstraint\UniqueInCollection; use Symfony\Component\Validator\Validation; $validator = Validation::createValidator(); $data = [ ['id' => 1, 'code' => 'ABC123'], ['id' => 2, 'code' => 'DEF456'], ['id' => 3, 'code' => 'ABC123'], // Duplicate code ]; $constraint = new UniqueInCollection('code'); $violations = $validator->validate($data, $constraint); if (count($violations) > 0) { foreach ($violations as $violation) { echo $violation->getMessage(); // "Must be unique within collection." } }
🧪 Running Tests
This project includes both unit tests and functional tests to ensure reliability:
Run All Tests
# Run the complete test suite (unit + functional)
./scripts/run-all-tests.sh
Run Unit Tests Only
# Basic unit tests ./vendor/bin/phpunit # Unit tests with coverage report ./vendor/bin/phpunit --coverage-html coverage/
Run Unit Tests with Coverage Check
# Ensures 100% test coverage
./scripts/check-coverage.sh
Run Functional Tests (Behat)
cd tests/functional
composer install
./vendor/bin/behat
Test Structure
- Unit Tests (
tests/unit/): Test individual components and constraint logic - Functional Tests (
tests/functional/): End-to-end Behat scenarios testing real-world usage - Coverage: Maintained at 100% to ensure reliability
🤝 Contributing
We welcome contributions to improve this constraint! Here's how you can help:
Reporting Issues
- 🐛 Bug Reports: Create an issue with detailed reproduction steps
- 💡 Feature Requests: Submit an enhancement request with your use case
- 📚 Documentation: Help improve documentation and examples
Pull Requests
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Write tests for your changes
- Ensure all tests pass:
./scripts/run-all-tests.sh - Commit your changes:
git commit -m 'Add amazing feature' - Push to your branch:
git push origin feature/amazing-feature - Submit a pull request
Development Guidelines
- Maintain 100% test coverage
- Follow PSR-12 coding standards
- Add comprehensive documentation for new features
- Ensure backward compatibility when possible
- Write clear commit messages
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
👨💻 Author
IDCT Bartosz Pachołek
- Email: bartosz+github@idct.tech
- GitHub: @GryfOSS
Made with ❤️ for the Symfony community
gryfoss/symfony-unique-in-collection-constraint 适用场景与选型建议
gryfoss/symfony-unique-in-collection-constraint 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 gryfoss/symfony-unique-in-collection-constraint 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 gryfoss/symfony-unique-in-collection-constraint 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 26
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-13