Skip to content

Commit 318effb

Browse files
authored
Version 2 (#10)
1 parent 2858dc1 commit 318effb

File tree

10 files changed

+475
-139
lines changed

10 files changed

+475
-139
lines changed

.circleci/config.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# PHP CircleCI 2.0 configuration file
2+
#
3+
# Check https://circleci.com/docs/2.0/language-php/ for more details
4+
#
5+
version: 2
6+
jobs:
7+
build:
8+
docker:
9+
# Specify the version you desire here
10+
- image: circleci/php:7.4-apache
11+
12+
# Specify service dependencies here if necessary
13+
# CircleCI maintains a library of pre-built images
14+
# documented at https://circleci.com/docs/2.0/circleci-images/
15+
# Using the RAM variation mitigates I/O contention
16+
# for database intensive operations.
17+
# - image: circleci/mysql:5.7-ram
18+
#
19+
# - image: redis:2.8.19
20+
21+
steps:
22+
- checkout
23+
24+
- run: sudo apt-get update -y # PHP CircleCI 2.0 Configuration File# PHP CircleCI 2.0 Configuration File sudo apt install zlib1g-dev libsqlite3-dev
25+
- run: sudo docker-php-ext-install zip
26+
27+
# Download and cache dependencies
28+
- restore_cache:
29+
keys:
30+
# "composer.lock" can be used if it is committed to the repo
31+
- v1-dependencies-{{ checksum "composer.json" }}
32+
# fallback to using the latest cache if no exact match is found
33+
- v1-dependencies-
34+
35+
- run: composer selfupdate && composer install -n --prefer-dist
36+
37+
- save_cache:
38+
key: v1-dependencies-{{ checksum "composer.json" }}
39+
paths:
40+
- ./vendor
41+
42+
# prepare the database
43+
# - run: touch storage/testing.sqlite
44+
# - run: php artisan migrate --env=testing --database=sqlite_testing --force
45+
46+
# run tests with phpunit or codecept
47+
- run: ./vendor/bin/phpunit
48+
# - run: ./vendor/bin/codecept build
49+
# - run: ./vendor/bin/codecept run

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/vendor
22
composer.lock
3+
*.cache
4+
clover.xml
5+
junit-logfile.xml

.php_cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
$finder = Symfony\Component\Finder\Finder::create()
4+
->notPath('vendor')
5+
->in(__DIR__)
6+
->name('*.php')
7+
->ignoreDotFiles(true)
8+
->ignoreVCS(true);
9+
10+
return PhpCsFixer\Config::create()
11+
->setRules([
12+
'@PSR2' => true,
13+
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
14+
'no_unused_imports' => true,
15+
'psr4' => true,
16+
])
17+
->setFinder($finder);

.travis.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
env:
2+
global:
3+
- COMPOSER_MEMORY_LIMIT=-1
4+
- XDEBUG_MODE=coverage
5+
16
language: php
27

38
php:
4-
- 5.5
59
- 5.6
610
- 7.0
711
- 7.1
12+
- 7.2
13+
- 7.3
14+
- 7.4
15+
- nightly
816

917
cache:
1018
directories:
1119
- $HOME/.composer/cache
1220

13-
before_install:
14-
- sudo apt-get update -qq
15-
1621
before_script:
17-
- curl -s http://getcomposer.org/installer | php
18-
- php -n composer.phar install --dev --verbose
22+
- composer selfupdate
23+
- composer install
1924

20-
script: phpunit
25+
script: vendor/bin/phpunit

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
[![Build status](https://ci.appveyor.com/api/projects/status/wues2pcnb6s07bbl?svg=true)](https://ci.appveyor.com/project/roger-vila/array-diff-multidimensional)
55
[![Code Climate](https://codeclimate.com/github/rogervila/array-diff-multidimensional/badges/gpa.svg)](https://codeclimate.com/github/rogervila/array-diff-multidimensional)
66
[![StyleCI](https://styleci.io/repos/82589676/shield?branch=master)](https://styleci.io/repos/82589676)
7+
[![Total Downloads](https://img.shields.io/packagist/dt/rogervila/array-diff-multidimensional)](https://packagist.org/packages/rogervila/array-diff-multidimensional)
8+
[![Latest Stable Version](https://img.shields.io/packagist/v/rogervila/array-diff-multidimensional)](https://packagist.org/packages/rogervila/array-diff-multidimensional)
9+
[![License](https://img.shields.io/packagist/l/rogervila/array-diff-multidimensional)](https://packagist.org/packages/rogervila/array-diff-multidimensional)
10+
711
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/0d8faa82-5cd3-44dd-9759-a8a1b7b55fce/big.png)](https://insight.sensiolabs.com/projects/0d8faa82-5cd3-44dd-9759-a8a1b7b55fce)
812

913
Works like the [PHP array_diff()](http://php.net/manual/es/function.array-diff.php) function, but with multidimensional arrays.
@@ -37,7 +41,7 @@ $old = [
3741
],
3842
];
3943

40-
var_dump(ArrayDiffMultidimensional::compare($new,$old));
44+
var_dump(ArrayDiffMultidimensional::compare($new, $old));
4145

4246
```
4347

@@ -51,6 +55,27 @@ The result of comparing `$new` with `$old` will return a new array with the chan
5155
]
5256
```
5357

58+
## Strict vs Loose comparisons
59+
60+
**Comparisons are strict by default**, but you can specify that you want to do a loose comparison passing a boolean as a third parameter for `compare` method, or calling the `looseComparison`
61+
62+
```php
63+
// This will deactivate the strict comparison mode
64+
ArrayDiffMultidimensional::compare($new, $old, false);
65+
66+
// This method call is equivalent
67+
ArrayDiffMultidimensional::looseComparison($new, $old);
68+
```
69+
70+
Also, a `strictComparison` method is available for more clarity
71+
```php
72+
// Comparisons are strict by default
73+
ArrayDiffMultidimensional::compare($new, $old);
74+
75+
// This method call is equivalent
76+
ArrayDiffMultidimensional::strictComparison($new, $old);
77+
```
78+
5479
## License
5580

5681
Array Diff Multidimensional is an open-sourced package licensed under the [MIT license](http://opensource.org/licenses/MIT).

appveyor.yml

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
11
build: false
2-
3-
platform: x86
4-
5-
clone_folder: c:\projects\arraydiffmultidimensional
2+
version: appveyor-{branch}-{build}
3+
shallow_clone: false
4+
clone_folder: C:\projects\app
65

76
environment:
87
matrix:
9-
- PHP_DOWNLOAD_FILE: php-5.5.9-nts-Win32-VC11-x86.zip
10-
- PHP_DOWNLOAD_FILE: php-5.6.4-nts-Win32-VC11-x86.zip
11-
- PHP_DOWNLOAD_FILE: php-7.0.0-nts-Win32-VC14-x86.zip
12-
- PHP_DOWNLOAD_FILE: php-7.1.0-nts-Win32-VC14-x86.zip
8+
- php_ver: 8.0.0
9+
- php_ver: 7.4.13
10+
- php_ver: 7.3.25
11+
- php_ver: 7.2.34
12+
- php_ver: 7.1.33
13+
- php_ver: 7.0.33
14+
- php_ver: 5.6.40
15+
- php_ver: 5.5.38
16+
# - php_ver: 5.4.45
17+
# - php_ver: 5.3.29
18+
19+
cache:
20+
- '%APPDATA%\Composer'
21+
- '%LOCALAPPDATA%\Composer'
22+
- C:\tools\php -> .appveyor.yml
23+
- C:\tools\composer.phar -> .appveyor.yml
1324

1425
init:
15-
- SET PATH=c:\php;%PATH%
16-
- SET COMPOSER_NO_INTERACTION=1
17-
- SET PHP=1
26+
- SET PATH=C:\tools\php;%PATH%
1827

1928
install:
20-
- IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php)
21-
- cd c:\php
22-
- IF %PHP%==1 appveyor DownloadFile https://raw.githubusercontent.com/symfony/binary-utils/master/cacert.pem
23-
- IF %PHP%==1 appveyor DownloadFile http://windows.php.net/downloads/releases/archives/%PHP_DOWNLOAD_FILE%
24-
- IF %PHP%==1 7z x %PHP_DOWNLOAD_FILE% -y >nul
25-
- IF %PHP%==1 del /Q *.zip
26-
- IF %PHP%==1 echo @php %%~dp0composer.phar %%* > composer.bat
27-
- IF %PHP%==1 copy /Y php.ini-development php.ini
28-
- IF %PHP%==1 echo max_execution_time=1200 >> php.ini
29-
- IF %PHP%==1 echo date.timezone="UTC" >> php.ini
30-
- IF %PHP%==1 echo extension_dir=ext >> php.ini
31-
- IF %PHP%==1 echo extension=php_curl.dll >> php.ini
32-
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
33-
- IF %PHP%==1 echo extension=php_intl.dll >> php.ini
34-
- IF %PHP%==1 echo extension=php_mbstring.dll >> php.ini
35-
- IF %PHP%==1 echo extension=php_fileinfo.dll >> php.ini
36-
- IF %PHP%==1 echo extension=php_pdo_sqlite.dll >> php.ini
37-
- IF %PHP%==1 echo curl.cainfo=c:\php\cacert.pem >> php.ini
38-
- appveyor DownloadFile https://getcomposer.org/composer.phar
39-
- cd c:\projects\arraydiffmultidimensional
40-
- mkdir %APPDATA%\Composer
41-
- composer update --prefer-dist --no-progress --ansi
29+
- ps: Set-Service wuauserv -StartupType Manual
30+
- IF NOT EXIST C:\tools\php (choco install --yes --allow-empty-checksums php --version %php_ver% --params '/InstallDir:C:\tools\php')
31+
- cd C:\tools\php
32+
- copy php.ini-production php.ini
33+
- echo date.timezone="UTC" >> php.ini
34+
- echo memory_limit=512M >> php.ini
35+
- echo extension_dir=ext >> php.ini
36+
- echo extension=php_curl.dll >> php.ini
37+
- echo extension=php_openssl.dll >> php.ini
38+
- echo extension=php_mbstring.dll >> php.ini
39+
- IF NOT EXIST C:\tools\composer.phar (cd C:\tools && appveyor DownloadFile https://getcomposer.org/composer.phar)
40+
- php C:\tools\composer.phar --version
41+
- cd C:\projects\app
42+
43+
before_test:
44+
- cd C:\projects\app
45+
- php C:\tools\composer.phar selfupdate
46+
- php C:\tools\composer.phar update --optimize-autoloader --no-interaction --no-progress --prefer-stable --no-ansi
47+
- php C:\tools\composer.phar info -D | sort
4248

4349
test_script:
44-
- cd c:\projects\arraydiffmultidimensional
45-
- vendor\bin\phpunit.bat --verbose
50+
- cd C:\projects\app
51+
- vendor\bin\phpunit

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@
2020
}
2121
},
2222
"require-dev": {
23-
"phpunit/phpunit": "^4.8 || ^5.0"
23+
"phpunit/phpunit": "^4.8 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
2424
}
2525
}

phpunit.xml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
<phpunit
2-
bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
9-
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="true"
12-
verbose="true"
13-
stderr="true"
14-
>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
153
<testsuites>
164
<testsuite name="Test Suite">
175
<directory>./tests/</directory>
186
</testsuite>
197
</testsuites>
8+
<filter>
9+
<whitelist processUncoveredFilesFromWhitelist="true">
10+
<directory suffix=".php">./src</directory>
11+
</whitelist>
12+
</filter>
13+
<logging>
14+
<log type="coverage-clover" target="clover.xml"/>
15+
<log type="junit" target="junit-logfile.xml"/>
16+
</logging>
2017
</phpunit>

src/ArrayDiffMultidimensional.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@
44

55
class ArrayDiffMultidimensional
66
{
7-
87
/**
98
* Returns an array with the differences between $array1 and $array2
9+
* $strict variable defines if comparison must be strict or not
1010
*
1111
* @param array $array1
1212
* @param array $array2
13+
* @param bool $strict
14+
*
1315
* @return array
1416
*/
15-
public static function compare($array1, $array2)
17+
public static function compare($array1, $array2, $strict = true)
1618
{
19+
if (!is_array($array1)) {
20+
throw new \InvalidArgumentException('array1 must be an array!');
21+
}
22+
23+
if (!is_array($array2)) {
24+
return $array1;
25+
}
26+
1727
$result = array();
1828

1929
if (!is_array($array2)) {
@@ -27,9 +37,9 @@ public static function compare($array1, $array2)
2737
}
2838

2939
if (is_array($value)) {
30-
$recursiveArrayDiff = static::compare($value, $array2[$key]);
40+
$recursiveArrayDiff = static::compare($value, $array2[$key], $strict);
3141

32-
if (count($recursiveArrayDiff)) {
42+
if (count($recursiveArrayDiff) > 0) {
3343
$result[$key] = $recursiveArrayDiff;
3444
}
3545

@@ -38,16 +48,43 @@ public static function compare($array1, $array2)
3848

3949
$value1 = $value;
4050
$value2 = $array2[$key];
51+
4152
if (is_float($value1) || is_float($value2)) {
42-
$value1 = (string)$value1;
43-
$value2 = (string)$value2;
53+
$value1 = (string) $value1;
54+
$value2 = (string) $value2;
4455
}
4556

46-
if ($value1 != $value2) {
57+
if ($strict ? $value1 !== $value2 : $value1 != $value2) {
4758
$result[$key] = $value;
4859
}
4960
}
5061

5162
return $result;
5263
}
64+
65+
/**
66+
* Returns an array with a strict comparison between $array1 and $array2
67+
*
68+
* @param array $array1
69+
* @param array $array2
70+
*
71+
* @return array
72+
*/
73+
public static function strictComparison($array1, $array2)
74+
{
75+
return static::compare($array1, $array2, true);
76+
}
77+
78+
/**
79+
* Returns an array with a loose comparison between $array1 and $array2
80+
*
81+
* @param array $array1
82+
* @param array $array2
83+
*
84+
* @return array
85+
*/
86+
public static function looseComparison($array1, $array2)
87+
{
88+
return static::compare($array1, $array2, false);
89+
}
5390
}

0 commit comments

Comments
 (0)