From 7edfce7d8b63aaf1f29e4c84f8ab239d71dd87e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Bal=C4=8Dytis?= Date: Mon, 7 Oct 2019 11:30:18 +0300 Subject: [PATCH] Support objects with integer keys. Fixes #1 --- .scrutinizer.yml | 25 +++++++++++++++++++++++ composer.json | 3 ++- src/DataFilter.php | 1 + tests/DataFilterTest.php | 44 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 .scrutinizer.yml create mode 100644 tests/DataFilterTest.php diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 0000000..4cfd5bf --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,25 @@ +checks: + php: + code_rating: true + +filter: + excluded_paths: + - tests/* + - vendor/* + +build: + environment: + php: '7.3' + + dependencies: + before: + - composer install + - mkdir -p build/logs + + tests: + override: + - + command: 'bin/phpunit --coverage-clover build/logs/clover.xml' + coverage: + file: 'build/logs/clover.xml' + format: 'clover' diff --git a/composer.json b/composer.json index 605f260..a30ea7e 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "require-dev": { "phpunit/phpunit": "^6.5", "paysera/lib-php-cs-fixer-config": "^2.2.2", - "mockery/mockery": "^1.2" + "mockery/mockery": "^1.2", + "ext-json": "*" }, "config": { "bin-dir": "bin" diff --git a/src/DataFilter.php b/src/DataFilter.php index 33e96b2..94e1abd 100644 --- a/src/DataFilter.php +++ b/src/DataFilter.php @@ -37,6 +37,7 @@ private function filterObject($data, NormalizationContext $context) { $result = new stdClass(); foreach ($data as $key => $value) { + $key = (string)$key; if ($value !== null && $context->isFieldIncluded($key)) { $result->$key = $this->filterData($value, $context->createScopedContext($key)); } diff --git a/tests/DataFilterTest.php b/tests/DataFilterTest.php new file mode 100644 index 0000000..e2fc29a --- /dev/null +++ b/tests/DataFilterTest.php @@ -0,0 +1,44 @@ +filterData($input, $context); + $this->assertEquals($expected, json_encode($output)); + } + + public function providerForFilterDataWithoutFiltering() + { + return [ + ['{}', new stdClass()], + ['[]', []], + ['{"a":"b"}', ['a' => 'b']], + ['{"a":"b","1":2}', ['a' => 'b', 1 => 2]], + ['[1,2]', [0 => 1, 1 => 2]], + ['{"0":1,"2":2}', [0 => 1, 2 => 2]], + ]; + } +}