Skip to content

Commit

Permalink
Deprecate non-flatten mode of Set::extract.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuswilms committed Feb 3, 2018
1 parent 63adbac commit 063612f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
deprecated and will cause the strategy to switch into _legacy_ mode. In legacy
mode the deprecated `mcrypt` extension will still be used.

- Deprecated the non-flatten mode in `Set::extract()` as it is rarely used.

### Fixed

- The `'key'` and `'class'` options were supposed to be provided only for
Expand Down
58 changes: 52 additions & 6 deletions tests/cases/util/SetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,12 +473,6 @@ public function testExtract() {
$result = Set::extract($a, '/User/id');
$this->assertEqual($expected, $result);

$expected = [
['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4], ['id' => 5]
];
$result = Set::extract($a, '/User/id', ['flatten' => false]);
$this->assertEqual($expected, $result);

$expected = [['test' => $a[0]['Deep']['Nesting']['test']]];
$this->assertEqual(Set::extract($a, '/Deep/Nesting/test'), $expected);
$this->assertEqual(Set::extract($b, '/Deep/Nesting/test'), $expected);
Expand Down Expand Up @@ -1505,6 +1499,58 @@ public function testSetSlice() {
$this->assertEqual(['key1' => 'val1', 'key3' => ['foo' => 'bar']], $removed);
$this->assertEqual(['key2' => 'val2'], $kept);
}

/* Deprecated / BC */

public function testExtractInNonFlattenMode() {
error_reporting(($original = error_reporting()) & ~E_USER_DEPRECATED);

$a = [
[
'User' => [
'id' => '1', 'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
],
],
[
'User' => [
'id' => '2', 'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
],
],
[
'User' => [
'id' => '3', 'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
],
],
[
'User' => [
'id' => '4', 'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
],
],
[
'User' => [
'id' => '5', 'user' => 'mariano',
'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'
],
]
];
$expected = [
['id' => 1], ['id' => 2], ['id' => 3], ['id' => 4], ['id' => 5]
];
$result = Set::extract($a, '/User/id', ['flatten' => false]);
$this->assertEqual($expected, $result);

error_reporting($original);
}
}

?>
5 changes: 5 additions & 0 deletions util/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ public static function extract(array $data, $path = null, array $options = []) {
$defaults = ['flatten' => true];
$options += $defaults;

if (!$options['flatten']) {
$message = 'Non-flatten mode in `Set::extract()` has been deprecated.';
trigger_error($message, E_USER_DEPRECATED);
}

if (!$data) {
return [];
}
Expand Down

0 comments on commit 063612f

Please sign in to comment.