benjaminmal / parity-checker
Parity checker
v2.0.0
2022-09-05 20:49 UTC
Requires
- php: ^8.0
- phpdocumentor/reflection-docblock: ^5.3
- symfony/options-resolver: ^5.4 || ^6.0
- symfony/property-access: ^5.4 || ^6.0
- symfony/property-info: ^5.4 || ^6.0
- webmozart/assert: ^1.10
Requires (Dev)
- benjaminmal/coding-standard: ^1.0
- php-parallel-lint/php-var-dump-check: ^0.5.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.1
- phpstan/phpstan-phpunit: ^1.0
- phpstan/phpstan-symfony: ^1.0
- phpstan/phpstan-webmozart-assert: ^1.0
- phpunit/phpunit: ^9.5
- symfony/var-dumper: ^5.4 || ^6.0
This package is auto-updated.
Last update: 2024-11-16 07:16:18 UTC
README
A PHP parity checker. Useful when you want to check if many objects are having the same datas. It has many options so you can configure the behavior you need, especially speaking of object recursion.
Installation
$ composer require benjaminmal/parity-checker
Getting started
Create the Parity checker using the factory:
$parityChecker = ParityChecker::create();
Or using your implementations using Symfony PropertyAccess & PropertyInfo components
/** @var PropertyAccessorInterface $propertyAccessor */ /** @var PropertyInfoExtractorInterface $propertyInfoExtractor */ $parityChecker = new ParityChecker($propertyAccessor, $propertyInfoExtractor);
Check your objects
$errors = $parityChecker->checkParity([$object1, $object2]); if (! $errors->hasErrors()) { // You're all set ! }
Usages
Options
$errors = $parityChecker->checkParity([$object1, $object2], [ // Do not perform check on these types 'ignore_types' => ['object', 'resource', \DateTimeInterface::class, '$objectProperty1'], // Perform check only on these types 'only_types' => ['string', 'float'], // Perform a loose check ('==' instead of '===') on theses types 'loose_types' => 'array', // Set the recursion limit for objects 'deep_object_limit' => 0, // Set DateTime format to check 'datetime_check_format' => false, // Set DateInterval format to check 'date_interval_format' => '%R %Y %M %D %H %I %S %F', // Set DateTime zone mapping to name 'datetime_zone' => true, // Set a data mapper closure 'data_mapper' => [ 'my-mapper' => new ParityCheckerCallback( 'array', fn ($value, string $property, array $options): mixed => $value['something'], ), ], // Custom checkers. You can set you own checker which replace other. 'custom_checkers' => [ 'my-checker' => new ParityCheckerCallback( ['$property'], fn ($value1, $value2, string $property, array $options): bool => true, ), ], ]);
Errors
$errors = $parityChecker->checkParity([$object1, $object2], $options); if ($errors->hasError()) { foreach ($errors as $error) { $property = $error->getProperty(); $object1 = $error->getObject1(); $object2 = $error->getObject2(); $errorValue1 = $error->getObject1Value(); $errorValue2 = $error->getObject2Value(); } }