Skip to content

Commit d281063

Browse files
authored
Add ability to filter responses (#2)
* Add ability to filter responses * Add ability to filter responses
1 parent 2dff2f0 commit d281063

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/Responses/AbstractResponse.php

+22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22
namespace Cubex\ApiTransport\Responses;
33

4+
use Cubex\ApiTransport\Exceptions\GenericApiException;
45
use Packaged\Helpers\Objects;
6+
use function array_filter;
7+
use function is_array;
58

69
abstract class AbstractResponse implements ApiResponse
710
{
@@ -16,4 +19,23 @@ public function hydrate(object $response): static
1619

1720
return $this;
1821
}
22+
23+
/**
24+
* @param string $property
25+
* @param callable $func
26+
*
27+
* @return $this
28+
* @throws GenericApiException
29+
*/
30+
public function filterClone(string $property, callable $func): static
31+
{
32+
if (!isset($this->$property) || !is_array($this->$property))
33+
{
34+
throw new GenericApiException();
35+
}
36+
37+
$resp = clone $this;
38+
$resp->$property = array_filter($this->$property, $func);
39+
return $resp;
40+
}
1941
}

0 commit comments

Comments
 (0)