-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractResource.php
More file actions
69 lines (59 loc) · 1.79 KB
/
AbstractResource.php
File metadata and controls
69 lines (59 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
declare(strict_types=1);
namespace Rapttor\WhopSdk\Resources;
use Rapttor\WhopSdk\Http\HttpClient;
use Rapttor\WhopSdk\Http\Page;
/**
* Base class for all Whop API resource wrappers.
*/
abstract class AbstractResource
{
public function __construct(
protected readonly HttpClient $client,
) {}
/**
* Build a Page from a raw API response.
*
* @param array<string, mixed> $response
* @param array<string, mixed> $params
* @return Page<array<string, mixed>>
*/
protected function buildPage(array $response, array $params): Page
{
return Page::fromResponse($response, $this, $params);
}
/**
* Resources that support listing must implement this to enable auto-pagination.
*
* @param array<string, mixed> $params
* @return Page<array<string, mixed>>
*/
public function list(array $params = []): Page
{
throw new \BadMethodCallException(static::class . ' does not support list().');
}
/**
* Helper: extract and validate a required string parameter.
*
* @param array<string, mixed> $params
*/
protected function requireParam(array $params, string $key): string
{
$value = $params[$key] ?? null;
if (!is_string($value) || $value === '') {
throw new \InvalidArgumentException("\"{$key}\" is required and must be a non-empty string.");
}
return $value;
}
/**
* Helper: strip known resource-level params before forwarding to the HTTP query.
*
* @param array<string, mixed> $params
* @param list<string> $keys
* @return array<string, mixed>
*/
protected function stripParams(array $params, array $keys): array
{
return array_diff_key($params, array_flip($keys));
}
}