From 34d7865a4907b22d77fde13d484ee6fa31db3dfd Mon Sep 17 00:00:00 2001 From: Arne De Smedt Date: Wed, 15 Apr 2020 08:49:02 +0200 Subject: [PATCH 1/3] exclude keys in toArray function --- src/ImmutableRecord.php | 2 +- src/ImmutableRecordLogic.php | 6 +++++- tests/ImmutableRecordLogicTest.php | 23 +++++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/ImmutableRecord.php b/src/ImmutableRecord.php index 0abe53e..f2eff4f 100644 --- a/src/ImmutableRecord.php +++ b/src/ImmutableRecord.php @@ -43,7 +43,7 @@ public static function fromArray(array $nativeData); */ public function with(array $recordData); - public function toArray(): array; + public function toArray(string ...$excludeKeys): array; public function equals(ImmutableRecord $other): bool; } diff --git a/src/ImmutableRecordLogic.php b/src/ImmutableRecordLogic.php index 717647b..89f2f37 100644 --- a/src/ImmutableRecordLogic.php +++ b/src/ImmutableRecordLogic.php @@ -87,12 +87,16 @@ public function with(array $recordData): self return $copy; } - public function toArray(): array + public function toArray(string ...$excludeKeys): array { $nativeData = []; $arrayPropItemTypeMap = self::getArrayPropItemTypeMapFromMethodOrCache(); foreach (self::$__propTypeMap as $key => [$type, $isNative, $isNullable]) { + if (in_array($key, $excludeKeys)) { + continue; + } + switch ($type) { case ImmutableRecord::PHP_TYPE_STRING: case ImmutableRecord::PHP_TYPE_INT: diff --git a/tests/ImmutableRecordLogicTest.php b/tests/ImmutableRecordLogicTest.php index aa62013..21e50ae 100644 --- a/tests/ImmutableRecordLogicTest.php +++ b/tests/ImmutableRecordLogicTest.php @@ -152,6 +152,29 @@ public function it_equals_other_record_with_same_values() $this->assertTrue($valueObjects->equals($other)); } + /** + * @test + */ + public function it_excludes_keys_while_converting_back_to_array() + { + $valueObjects = TypeHintedImmutableRecord::fromArray($this->data); + + $this->data['type'] = null; + $this->data['percentage'] = 0.5; + + unset($this->data['version'], $this->data['name']); + + $dataWithoutKeys = $valueObjects->toArray('version', 'name'); + + $this->assertArrayNotHasKey('version', $dataWithoutKeys); + $this->assertArrayNotHasKey('name', $dataWithoutKeys); + + $this->assertEquals( + $this->data, + $dataWithoutKeys + ); + } + /** * @test */ From 3e1e82859c9fbcd92376bd44f145adbdad231434 Mon Sep 17 00:00:00 2001 From: Arne De Smedt Date: Wed, 15 Apr 2020 09:20:31 +0200 Subject: [PATCH 2/3] added toArrayWithout instead of adding exlcude keys to the toArray function --- src/ImmutableRecord.php | 4 +++- src/ImmutableRecordLogic.php | 13 ++++++++----- tests/ImmutableRecordLogicTest.php | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/ImmutableRecord.php b/src/ImmutableRecord.php index f2eff4f..fe773d8 100644 --- a/src/ImmutableRecord.php +++ b/src/ImmutableRecord.php @@ -43,7 +43,9 @@ public static function fromArray(array $nativeData); */ public function with(array $recordData); - public function toArray(string ...$excludeKeys): array; + public function toArray(): array; + + public function toArrayWithout(string ...$excludeKeys): array; public function equals(ImmutableRecord $other): bool; } diff --git a/src/ImmutableRecordLogic.php b/src/ImmutableRecordLogic.php index 89f2f37..226ddb7 100644 --- a/src/ImmutableRecordLogic.php +++ b/src/ImmutableRecordLogic.php @@ -87,16 +87,12 @@ public function with(array $recordData): self return $copy; } - public function toArray(string ...$excludeKeys): array + public function toArray(): array { $nativeData = []; $arrayPropItemTypeMap = self::getArrayPropItemTypeMapFromMethodOrCache(); foreach (self::$__propTypeMap as $key => [$type, $isNative, $isNullable]) { - if (in_array($key, $excludeKeys)) { - continue; - } - switch ($type) { case ImmutableRecord::PHP_TYPE_STRING: case ImmutableRecord::PHP_TYPE_INT: @@ -128,6 +124,13 @@ public function toArray(string ...$excludeKeys): array return $nativeData; } + public function toArrayWithout(string ...$excludeKeys) : array + { + $nativeData = $this->toArray(); + + return array_diff_key($nativeData, array_flip($excludeKeys)); + } + public function equals(ImmutableRecord $other): bool { if (get_class($this) !== get_class($other)) { diff --git a/tests/ImmutableRecordLogicTest.php b/tests/ImmutableRecordLogicTest.php index 21e50ae..1c43931 100644 --- a/tests/ImmutableRecordLogicTest.php +++ b/tests/ImmutableRecordLogicTest.php @@ -164,7 +164,7 @@ public function it_excludes_keys_while_converting_back_to_array() unset($this->data['version'], $this->data['name']); - $dataWithoutKeys = $valueObjects->toArray('version', 'name'); + $dataWithoutKeys = $valueObjects->toArrayWithout('version', 'name'); $this->assertArrayNotHasKey('version', $dataWithoutKeys); $this->assertArrayNotHasKey('name', $dataWithoutKeys); From 316e7364df00e1951eb40e7678f5e0befd81fac1 Mon Sep 17 00:00:00 2001 From: Arne De Smedt Date: Wed, 15 Apr 2020 10:15:05 +0200 Subject: [PATCH 3/3] added toArrayExcept and toArrayOnly and removed previous features --- src/ImmutableRecord.php | 4 +++- src/ImmutableRecordLogic.php | 9 ++++++++- tests/ImmutableRecordLogicTest.php | 25 ++++++++++++++++++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/ImmutableRecord.php b/src/ImmutableRecord.php index fe773d8..c69267b 100644 --- a/src/ImmutableRecord.php +++ b/src/ImmutableRecord.php @@ -45,7 +45,9 @@ public function with(array $recordData); public function toArray(): array; - public function toArrayWithout(string ...$excludeKeys): array; + public function toArrayExcept(string ...$excludeKeys): array; + + public function toArrayOnly(string ...$includeKeys): array; public function equals(ImmutableRecord $other): bool; } diff --git a/src/ImmutableRecordLogic.php b/src/ImmutableRecordLogic.php index 226ddb7..b581934 100644 --- a/src/ImmutableRecordLogic.php +++ b/src/ImmutableRecordLogic.php @@ -124,13 +124,20 @@ public function toArray(): array return $nativeData; } - public function toArrayWithout(string ...$excludeKeys) : array + public function toArrayExcept(string ...$excludeKeys) : array { $nativeData = $this->toArray(); return array_diff_key($nativeData, array_flip($excludeKeys)); } + public function toArrayOnly(string ...$includeKeys) : array + { + $nativeData = $this->toArray(); + + return array_intersect_key($nativeData, array_flip($includeKeys)); + } + public function equals(ImmutableRecord $other): bool { if (get_class($this) !== get_class($other)) { diff --git a/tests/ImmutableRecordLogicTest.php b/tests/ImmutableRecordLogicTest.php index 1c43931..5825d55 100644 --- a/tests/ImmutableRecordLogicTest.php +++ b/tests/ImmutableRecordLogicTest.php @@ -164,7 +164,7 @@ public function it_excludes_keys_while_converting_back_to_array() unset($this->data['version'], $this->data['name']); - $dataWithoutKeys = $valueObjects->toArrayWithout('version', 'name'); + $dataWithoutKeys = $valueObjects->toArrayExcept('version', 'name'); $this->assertArrayNotHasKey('version', $dataWithoutKeys); $this->assertArrayNotHasKey('name', $dataWithoutKeys); @@ -175,6 +175,29 @@ public function it_excludes_keys_while_converting_back_to_array() ); } + /** + * @test + */ + public function it_includes_keys_while_converting_back_to_array() + { + $valueObjects = TypeHintedImmutableRecord::fromArray($this->data); + + $this->data['type'] = null; + $this->data['percentage'] = 0.5; + + unset($this->data['version'], $this->data['name'], $this->data['type'], $this->data['percentage']); + + $dataWithKeys = $valueObjects->toArrayOnly('access', 'itemList'); + + $this->assertArrayHasKey('access', $dataWithKeys); + $this->assertArrayHasKey('itemList', $dataWithKeys); + + $this->assertEquals( + $this->data, + $dataWithKeys + ); + } + /** * @test */