Skip to content

Commit e25fa02

Browse files
chore: refactor methods
1 parent f992e92 commit e25fa02

File tree

19 files changed

+90
-92
lines changed

19 files changed

+90
-92
lines changed

scripts/lint

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ set -e
55
cd -- "$(dirname -- "$0")/.."
66

77
echo "==> Running PHPStan"
8-
exec -- ./vendor/bin/phpstan analyse --memory-limit=1G
8+
exec -- ./vendor/bin/phpstan analyse --memory-limit=2G

src/CasGenerator/CasGeneratorGenerateCasParams.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public static function with(
127127
$obj->password = $password;
128128
$obj->toDate = $toDate;
129129

130-
null !== $casAuthority && $obj->casAuthority = $casAuthority instanceof CasAuthority ? $casAuthority->value : $casAuthority;
130+
null !== $casAuthority && $obj['casAuthority'] = $casAuthority;
131131
null !== $panNo && $obj->panNo = $panNo;
132132

133133
return $obj;
@@ -185,7 +185,7 @@ public function withToDate(string $toDate): self
185185
public function withCasAuthority(CasAuthority|string $casAuthority): self
186186
{
187187
$obj = clone $this;
188-
$obj->casAuthority = $casAuthority instanceof CasAuthority ? $casAuthority->value : $casAuthority;
188+
$obj['casAuthority'] = $casAuthority;
189189

190190
return $obj;
191191
}

src/CasGenerator/CasGeneratorGenerateCasResponse.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66

77
use CasParser\Core\Attributes\Api;
88
use CasParser\Core\Concerns\SdkModel;
9+
use CasParser\Core\Concerns\SdkResponse;
910
use CasParser\Core\Contracts\BaseModel;
11+
use CasParser\Core\Conversion\Contracts\ResponseConverter;
1012

1113
/**
1214
* @phpstan-type cas_generator_generate_cas_response = array{
1315
* msg?: string, status?: string
1416
* }
15-
* When used in a response, this type parameter can define a $rawResponse property.
16-
* @template TRawResponse of object = object{}
17-
*
18-
* @mixin TRawResponse
1917
*/
20-
final class CasGeneratorGenerateCasResponse implements BaseModel
18+
final class CasGeneratorGenerateCasResponse implements BaseModel, ResponseConverter
2119
{
2220
/** @use SdkModel<cas_generator_generate_cas_response> */
2321
use SdkModel;
2422

23+
use SdkResponse;
24+
2525
#[Api(optional: true)]
2626
public ?string $msg;
2727

src/CasParser/UnifiedResponse.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
use CasParser\CasParser\UnifiedResponse\Summary;
1414
use CasParser\Core\Attributes\Api;
1515
use CasParser\Core\Concerns\SdkModel;
16+
use CasParser\Core\Concerns\SdkResponse;
1617
use CasParser\Core\Contracts\BaseModel;
18+
use CasParser\Core\Conversion\Contracts\ResponseConverter;
1719

1820
/**
1921
* @phpstan-type unified_response = array{
@@ -25,16 +27,14 @@
2527
* nps?: list<Np>,
2628
* summary?: Summary,
2729
* }
28-
* When used in a response, this type parameter can define a $rawResponse property.
29-
* @template TRawResponse of object = object{}
30-
*
31-
* @mixin TRawResponse
3230
*/
33-
final class UnifiedResponse implements BaseModel
31+
final class UnifiedResponse implements BaseModel, ResponseConverter
3432
{
3533
/** @use SdkModel<unified_response> */
3634
use SdkModel;
3735

36+
use SdkResponse;
37+
3838
/** @var list<DematAccount>|null $dematAccounts */
3939
#[Api('demat_accounts', list: DematAccount::class, optional: true)]
4040
public ?array $dematAccounts;

src/CasParser/UnifiedResponse/DematAccount.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static function with(
114114
null !== $additionalInfo && $obj->additionalInfo = $additionalInfo;
115115
null !== $boID && $obj->boID = $boID;
116116
null !== $clientID && $obj->clientID = $clientID;
117-
null !== $dematType && $obj->dematType = $dematType instanceof DematType ? $dematType->value : $dematType;
117+
null !== $dematType && $obj['dematType'] = $dematType;
118118
null !== $dpID && $obj->dpID = $dpID;
119119
null !== $dpName && $obj->dpName = $dpName;
120120
null !== $holdings && $obj->holdings = $holdings;
@@ -165,7 +165,7 @@ public function withClientID(string $clientID): self
165165
public function withDematType(DematType|string $dematType): self
166166
{
167167
$obj = clone $this;
168-
$obj->dematType = $dematType instanceof DematType ? $dematType->value : $dematType;
168+
$obj['dematType'] = $dematType;
169169

170170
return $obj;
171171
}

src/CasParser/UnifiedResponse/Meta.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function with(
5858
): self {
5959
$obj = new self;
6060

61-
null !== $casType && $obj->casType = $casType instanceof CasType ? $casType->value : $casType;
61+
null !== $casType && $obj['casType'] = $casType;
6262
null !== $generatedAt && $obj->generatedAt = $generatedAt;
6363
null !== $statementPeriod && $obj->statementPeriod = $statementPeriod;
6464

@@ -73,7 +73,7 @@ public static function with(
7373
public function withCasType(CasType|string $casType): self
7474
{
7575
$obj = clone $this;
76-
$obj->casType = $casType instanceof CasType ? $casType->value : $casType;
76+
$obj['casType'] = $casType;
7777

7878
return $obj;
7979
}

src/CasParser/UnifiedResponse/MutualFund/Scheme.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static function with(
134134
null !== $nav && $obj->nav = $nav;
135135
null !== $nominees && $obj->nominees = $nominees;
136136
null !== $transactions && $obj->transactions = $transactions;
137-
null !== $type && $obj->type = $type instanceof Type ? $type->value : $type;
137+
null !== $type && $obj['type'] = $type;
138138
null !== $units && $obj->units = $units;
139139
null !== $value && $obj->value = $value;
140140

@@ -236,7 +236,7 @@ public function withTransactions(array $transactions): self
236236
public function withType(Type|string $type): self
237237
{
238238
$obj = clone $this;
239-
$obj->type = $type instanceof Type ? $type->value : $type;
239+
$obj['type'] = $type;
240240

241241
return $obj;
242242
}

src/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null)
2828
{
2929
$this->apiKey = (string) ($apiKey ?? getenv('CAS_PARSER_API_KEY'));
3030

31-
$base = $baseUrl ?? getenv(
31+
$baseUrl ??= getenv(
3232
'CAS_PARSER_BASE_URL'
3333
) ?: 'https://portfolio-parser.api.casparser.in';
3434

@@ -43,7 +43,7 @@ public function __construct(?string $apiKey = null, ?string $baseUrl = null)
4343
headers: [
4444
'Content-Type' => 'application/json', 'Accept' => 'application/json',
4545
],
46-
baseUrl: $base,
46+
baseUrl: $baseUrl,
4747
options: $options,
4848
);
4949

src/Core/BaseClient.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* body: mixed,
3030
* }
3131
*/
32-
class BaseClient
32+
abstract class BaseClient
3333
{
3434
protected UriInterface $baseUrl;
3535

@@ -77,14 +77,11 @@ public function request(
7777
// @phpstan-ignore-next-line
7878
$rsp = $this->sendRequest($opts, req: $request, data: $body, redirectCount: 0, retryCount: 0);
7979

80-
$decoded = Util::decodeContent($rsp);
81-
8280
if (!is_null($stream)) {
8381
return new $stream(
8482
convert: $convert,
8583
request: $request,
86-
response: $rsp,
87-
stream: $decoded
84+
response: $rsp
8885
);
8986
}
9087

@@ -93,23 +90,20 @@ public function request(
9390
convert: $convert,
9491
client: $this,
9592
request: $req,
93+
response: $rsp,
9694
options: $opts,
97-
data: $decoded,
9895
);
9996
}
10097

10198
if (!is_null($convert)) {
102-
return Conversion::coerce($convert, value: $decoded);
99+
return Conversion::coerceResponse($convert, response: $rsp);
103100
}
104101

105-
return $decoded;
102+
return Util::decodeContent($rsp);
106103
}
107104

108105
/** @return array<string, string> */
109-
protected function authHeaders(): array
110-
{
111-
return [];
112-
}
106+
abstract protected function authHeaders(): array;
113107

114108
/**
115109
* @internal

src/Core/Concerns/SdkResponse.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace CasParser\Core\Concerns;
4+
5+
use CasParser\Core\Util;
6+
use Psr\Http\Message\ResponseInterface;
7+
8+
/**
9+
* @internal
10+
* SdkResponse must only be used in conjunction with classes that use the SdkModel trait
11+
*/
12+
trait SdkResponse
13+
{
14+
private ?ResponseInterface $_rawResponse;
15+
16+
public static function fromResponse(ResponseInterface $response): static
17+
{
18+
$instance = new static;
19+
$instance->_rawResponse = $response;
20+
$instance->__unserialize(Util::decodeContent($response)); // @phpstan-ignore-line
21+
22+
return $instance;
23+
}
24+
25+
public function getRawResponse(): ?ResponseInterface
26+
{
27+
return $this->_rawResponse;
28+
}
29+
}

0 commit comments

Comments
 (0)