Skip to content

Commit

Permalink
added toArrayWithout instead of adding exlcude keys to the toArray
Browse files Browse the repository at this point in the history
function
  • Loading branch information
arnedesmedt committed Apr 15, 2020
1 parent 34d7865 commit 3e1e828
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/ImmutableRecord.php
Expand Up @@ -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;
}
13 changes: 8 additions & 5 deletions src/ImmutableRecordLogic.php
Expand Up @@ -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:
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/ImmutableRecordLogicTest.php
Expand Up @@ -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);
Expand Down

0 comments on commit 3e1e828

Please sign in to comment.