https://github.com/codeliner/php-equalsbuilder
EqualsBuilder for PHP
https://github.com/codeliner/php-equalsbuilder
Last synced: 12 months ago
JSON representation
EqualsBuilder for PHP
- Host: GitHub
- URL: https://github.com/codeliner/php-equalsbuilder
- Owner: codeliner
- License: bsd-3-clause
- Created: 2014-01-10T19:37:07.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2014-06-13T16:15:59.000Z (about 12 years ago)
- Last Synced: 2025-05-25T02:18:40.621Z (about 1 year ago)
- Language: PHP
- Size: 168 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
EqualsBuilder
=============
EqualsBuilder for PHP
[](https://travis-ci.org/codeliner/php-equalsbuilder)
## Installation
Installation of codeliner/php-equalsbuilder uses composer. For composer documentation, please refer to
[getcomposer.org](http://getcomposer.org/). Add following requirement to your composer.json
```sh
"codeliner/php-equalsbuilder" : "1.2.*"
```
## Usage
Use the EqualsBuilder to compare any number of value pairs at once.
```php
echo EqualsBuilder::create()
->append('equals', 'equals')
->append(1, 1)
->append(1, '1')
->equals() ?
"All value pairs are equal" : "At least one value pair is not equal";
//Output: All value pairs are equal
```
You can enable strict mode to compare against the value types, too.
```php
echo EqualsBuilder::create()
->append('equals', 'equals')
->append(1, 1)
->append(1, '1')
->strict() //enable strict mode
->equals() ?
"All value pairs are equal" : "At least one value pair is not equal";
//Output: At least one value pair is not equal
```
If you only provide the first parameter the second one is set to true. This enables you to use comparison methods of
objects together with the EqualsBuilder.
```php
echo EqualsBuilder::create()
->append($vo->sameValueAs($otherVo))
->append(1, 1)
->append(1, '1')
->equals() ?
"All value pairs are equal" : "At least one value pair is not equal";
//Output: All value pairs are equal
```
You can also provide a callback as third parameter which is called with the value pair. The callback should return a boolean value.
```php
echo EqualsBuilder::create()
->append($vo, $otherVo, function($a, $b) { return $a->sameValueAs($b);})
->append(1, 1)
->append(1, '1')
->equals() ?
"All value pairs are equal" : "At least one value pair is not equal";
//Output: All value pairs are equal
```
The callback option gets really interesting when $a and $b are array lists (arrays with continuous integer keys starting at 0 and ending at count - 1 ).
In this case the callback is called for every item in list $a together with the corresponding item of list $b assumed that count($a) == count($b) is true.
```php
$aList = array($vo1, $vo2, $vo3);
$bList = array($vo1, $vo2, $vo3);
echo EqualsBuilder::create()
->append($aList, $bList, function($aVO, $bVO) { return $aVO->sameValueAs($bVO);})
->append(1, 1)
->append(1, '1')
->equals() ?
"All value pairs are equal" : "At least one value pair is not equal";
//Output: All value pairs are equal
```