codeliner / php-equalsbuilder
EqualsBuilder for PHP.
Installs: 4 450
Dependents: 4
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 0
Open Issues: 4
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-11-05 02:02:51 UTC
README
EqualsBuilder for PHP
Installation
Installation of codeliner/php-equalsbuilder uses composer. For composer documentation, please refer to getcomposer.org. Add following requirement to your composer.json
"codeliner/php-equalsbuilder" : "1.2.*"
Usage
Use the EqualsBuilder to compare any number of value pairs at once.
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.
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.
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.
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.
$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