Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
build:
docker:
# Specify the version you desire here
- image: circleci/php:7.4-apache

# Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images
# documented at https://circleci.com/docs/2.0/circleci-images/
# Using the RAM variation mitigates I/O contention
# for database intensive operations.
# - image: circleci/mysql:5.7-ram
#
# - image: redis:2.8.19

steps:
- checkout

- 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
- run: sudo docker-php-ext-install zip

# Download and cache dependencies
- restore_cache:
keys:
# "composer.lock" can be used if it is committed to the repo
- v1-dependencies-{{ checksum "composer.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-

- run: composer selfupdate && composer install -n --prefer-dist

- save_cache:
key: v1-dependencies-{{ checksum "composer.json" }}
paths:
- ./vendor

# prepare the database
# - run: touch storage/testing.sqlite
# - run: php artisan migrate --env=testing --database=sqlite_testing --force

# run tests with phpunit or codecept
- run: ./vendor/bin/phpunit
# - run: ./vendor/bin/codecept build
# - run: ./vendor/bin/codecept run
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/vendor
composer.lock
*.cache
clover.xml
junit-logfile.xml
17 changes: 17 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

$finder = Symfony\Component\Finder\Finder::create()
->notPath('vendor')
->in(__DIR__)
->name('*.php')
->ignoreDotFiles(true)
->ignoreVCS(true);

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'ordered_imports' => ['sortAlgorithm' => 'alpha'],
'no_unused_imports' => true,
'psr4' => true,
])
->setFinder($finder);
19 changes: 12 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
env:
global:
- COMPOSER_MEMORY_LIMIT=-1
- XDEBUG_MODE=coverage

language: php

php:
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4
- nightly

cache:
directories:
- $HOME/.composer/cache

before_install:
- sudo apt-get update -qq

before_script:
- curl -s http://getcomposer.org/installer | php
- php -n composer.phar install --dev --verbose
- composer selfupdate
- composer install

script: phpunit
script: vendor/bin/phpunit
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
[![Build status](https://ci.appveyor.com/api/projects/status/wues2pcnb6s07bbl?svg=true)](https://ci.appveyor.com/project/roger-vila/array-diff-multidimensional)
[![Code Climate](https://codeclimate.com/github/rogervila/array-diff-multidimensional/badges/gpa.svg)](https://codeclimate.com/github/rogervila/array-diff-multidimensional)
[![StyleCI](https://styleci.io/repos/82589676/shield?branch=master)](https://styleci.io/repos/82589676)
[![Total Downloads](https://img.shields.io/packagist/dt/rogervila/array-diff-multidimensional)](https://packagist.org/packages/rogervila/array-diff-multidimensional)
[![Latest Stable Version](https://img.shields.io/packagist/v/rogervila/array-diff-multidimensional)](https://packagist.org/packages/rogervila/array-diff-multidimensional)
[![License](https://img.shields.io/packagist/l/rogervila/array-diff-multidimensional)](https://packagist.org/packages/rogervila/array-diff-multidimensional)

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/0d8faa82-5cd3-44dd-9759-a8a1b7b55fce/big.png)](https://insight.sensiolabs.com/projects/0d8faa82-5cd3-44dd-9759-a8a1b7b55fce)

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

var_dump(ArrayDiffMultidimensional::compare($new,$old));
var_dump(ArrayDiffMultidimensional::compare($new, $old));

```

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

## Strict vs Loose comparisons

**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`

```php
// This will deactivate the strict comparison mode
ArrayDiffMultidimensional::compare($new, $old, false);

// This method call is equivalent
ArrayDiffMultidimensional::looseComparison($new, $old);
```

Also, a `strictComparison` method is available for more clarity
```php
// Comparisons are strict by default
ArrayDiffMultidimensional::compare($new, $old);

// This method call is equivalent
ArrayDiffMultidimensional::strictComparison($new, $old);
```

## License

Array Diff Multidimensional is an open-sourced package licensed under the [MIT license](http://opensource.org/licenses/MIT).
76 changes: 41 additions & 35 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
build: false

platform: x86

clone_folder: c:\projects\arraydiffmultidimensional
version: appveyor-{branch}-{build}
shallow_clone: false
clone_folder: C:\projects\app

environment:
matrix:
- PHP_DOWNLOAD_FILE: php-5.5.9-nts-Win32-VC11-x86.zip
- PHP_DOWNLOAD_FILE: php-5.6.4-nts-Win32-VC11-x86.zip
- PHP_DOWNLOAD_FILE: php-7.0.0-nts-Win32-VC14-x86.zip
- PHP_DOWNLOAD_FILE: php-7.1.0-nts-Win32-VC14-x86.zip
- php_ver: 8.0.0
- php_ver: 7.4.13
- php_ver: 7.3.25
- php_ver: 7.2.34
- php_ver: 7.1.33
- php_ver: 7.0.33
- php_ver: 5.6.40
- php_ver: 5.5.38
# - php_ver: 5.4.45
# - php_ver: 5.3.29

cache:
- '%APPDATA%\Composer'
- '%LOCALAPPDATA%\Composer'
- C:\tools\php -> .appveyor.yml
- C:\tools\composer.phar -> .appveyor.yml

init:
- SET PATH=c:\php;%PATH%
- SET COMPOSER_NO_INTERACTION=1
- SET PHP=1
- SET PATH=C:\tools\php;%PATH%

install:
- IF EXIST c:\php (SET PHP=0) ELSE (mkdir c:\php)
- cd c:\php
- IF %PHP%==1 appveyor DownloadFile https://raw.githubusercontent.com/symfony/binary-utils/master/cacert.pem
- IF %PHP%==1 appveyor DownloadFile http://windows.php.net/downloads/releases/archives/%PHP_DOWNLOAD_FILE%
- IF %PHP%==1 7z x %PHP_DOWNLOAD_FILE% -y >nul
- IF %PHP%==1 del /Q *.zip
- IF %PHP%==1 echo @php %%~dp0composer.phar %%* > composer.bat
- IF %PHP%==1 copy /Y php.ini-development php.ini
- IF %PHP%==1 echo max_execution_time=1200 >> php.ini
- IF %PHP%==1 echo date.timezone="UTC" >> php.ini
- IF %PHP%==1 echo extension_dir=ext >> php.ini
- IF %PHP%==1 echo extension=php_curl.dll >> php.ini
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
- IF %PHP%==1 echo extension=php_intl.dll >> php.ini
- IF %PHP%==1 echo extension=php_mbstring.dll >> php.ini
- IF %PHP%==1 echo extension=php_fileinfo.dll >> php.ini
- IF %PHP%==1 echo extension=php_pdo_sqlite.dll >> php.ini
- IF %PHP%==1 echo curl.cainfo=c:\php\cacert.pem >> php.ini
- appveyor DownloadFile https://getcomposer.org/composer.phar
- cd c:\projects\arraydiffmultidimensional
- mkdir %APPDATA%\Composer
- composer update --prefer-dist --no-progress --ansi
- ps: Set-Service wuauserv -StartupType Manual
- IF NOT EXIST C:\tools\php (choco install --yes --allow-empty-checksums php --version %php_ver% --params '/InstallDir:C:\tools\php')
- cd C:\tools\php
- copy php.ini-production php.ini
- echo date.timezone="UTC" >> php.ini
- echo memory_limit=512M >> php.ini
- echo extension_dir=ext >> php.ini
- echo extension=php_curl.dll >> php.ini
- echo extension=php_openssl.dll >> php.ini
- echo extension=php_mbstring.dll >> php.ini
- IF NOT EXIST C:\tools\composer.phar (cd C:\tools && appveyor DownloadFile https://getcomposer.org/composer.phar)
- php C:\tools\composer.phar --version
- cd C:\projects\app

before_test:
- cd C:\projects\app
- php C:\tools\composer.phar selfupdate
- php C:\tools\composer.phar update --optimize-autoloader --no-interaction --no-progress --prefer-stable --no-ansi
- php C:\tools\composer.phar info -D | sort

test_script:
- cd c:\projects\arraydiffmultidimensional
- vendor\bin\phpunit.bat --verbose
- cd C:\projects\app
- vendor\bin\phpunit
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
}
},
"require-dev": {
"phpunit/phpunit": "^4.8 || ^5.0"
"phpunit/phpunit": "^4.8 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
}
}
25 changes: 11 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
<phpunit
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
verbose="true"
stderr="true"
>
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="clover.xml"/>
<log type="junit" target="junit-logfile.xml"/>
</logging>
</phpunit>
53 changes: 45 additions & 8 deletions src/ArrayDiffMultidimensional.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,38 @@

class ArrayDiffMultidimensional
{

/**
* Returns an array with the differences between $array1 and $array2
* $strict variable defines if comparison must be strict or not
*
* @param array $array1
* @param array $array2
* @param bool $strict
*
* @return array
*/
public static function compare($array1, $array2)
public static function compare($array1, $array2, $strict = true)
{
if (!is_array($array1)) {
throw new \InvalidArgumentException('array1 must be an array!');
}

if (!is_array($array2)) {
return $array1;
}

$result = array();

foreach ($array1 as $key => $value) {
if (!is_array($array2) || !array_key_exists($key, $array2)) {
if (!array_key_exists($key, $array2)) {
$result[$key] = $value;
continue;
}

if (is_array($value)) {
$recursiveArrayDiff = static::compare($value, $array2[$key]);
$recursiveArrayDiff = static::compare($value, $array2[$key], $strict);

if (count($recursiveArrayDiff)) {
if (count($recursiveArrayDiff) > 0) {
$result[$key] = $recursiveArrayDiff;
}

Expand All @@ -34,16 +44,43 @@ public static function compare($array1, $array2)

$value1 = $value;
$value2 = $array2[$key];

if (is_float($value1) || is_float($value2)) {
$value1 = (string)$value1;
$value2 = (string)$value2;
$value1 = (string) $value1;
$value2 = (string) $value2;
}

if ($value1 != $value2) {
if ($strict ? $value1 !== $value2 : $value1 != $value2) {
$result[$key] = $value;
}
}

return $result;
}

/**
* Returns an array with a strict comparison between $array1 and $array2
*
* @param array $array1
* @param array $array2
*
* @return array
*/
public static function strictComparison($array1, $array2)
{
return static::compare($array1, $array2, true);
}

/**
* Returns an array with a loose comparison between $array1 and $array2
*
* @param array $array1
* @param array $array2
*
* @return array
*/
public static function looseComparison($array1, $array2)
{
return static::compare($array1, $array2, false);
}
}
Loading