Skip to content

Commit 9b7428f

Browse files
feat(client): use real enums
1 parent 36e3c1d commit 9b7428f

File tree

11 files changed

+80
-76
lines changed

11 files changed

+80
-76
lines changed

src/CasGenerator/CasGeneratorGenerateCasParams.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* fromDate: string,
3333
* password: string,
3434
* toDate: string,
35-
* casAuthority?: CasAuthority::*,
35+
* casAuthority?: CasAuthority|value-of<CasAuthority>,
3636
* panNo?: string,
3737
* }
3838
*/
@@ -69,7 +69,7 @@ final class CasGeneratorGenerateCasParams implements BaseModel
6969
/**
7070
* CAS authority to generate the document from (currently only kfintech is supported).
7171
*
72-
* @var CasAuthority::*|null $casAuthority
72+
* @var value-of<CasAuthority>|null $casAuthority
7373
*/
7474
#[Api('cas_authority', enum: CasAuthority::class, optional: true)]
7575
public ?string $casAuthority;
@@ -110,14 +110,14 @@ public function __construct()
110110
*
111111
* You must use named parameters to construct any parameters with a default value.
112112
*
113-
* @param CasAuthority::* $casAuthority
113+
* @param CasAuthority|value-of<CasAuthority> $casAuthority
114114
*/
115115
public static function with(
116116
string $email,
117117
string $fromDate,
118118
string $password,
119119
string $toDate,
120-
?string $casAuthority = null,
120+
CasAuthority|string|null $casAuthority = null,
121121
?string $panNo = null,
122122
): self {
123123
$obj = new self;
@@ -127,7 +127,7 @@ public static function with(
127127
$obj->password = $password;
128128
$obj->toDate = $toDate;
129129

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

133133
return $obj;
@@ -180,12 +180,12 @@ public function withToDate(string $toDate): self
180180
/**
181181
* CAS authority to generate the document from (currently only kfintech is supported).
182182
*
183-
* @param CasAuthority::* $casAuthority
183+
* @param CasAuthority|value-of<CasAuthority> $casAuthority
184184
*/
185-
public function withCasAuthority(string $casAuthority): self
185+
public function withCasAuthority(CasAuthority|string $casAuthority): self
186186
{
187187
$obj = clone $this;
188-
$obj->casAuthority = $casAuthority;
188+
$obj->casAuthority = $casAuthority instanceof CasAuthority ? $casAuthority->value : $casAuthority;
189189

190190
return $obj;
191191
}

src/CasGenerator/CasGeneratorGenerateCasParams/CasAuthority.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44

55
namespace CasParser\CasGenerator\CasGeneratorGenerateCasParams;
66

7-
use CasParser\Core\Concerns\SdkEnum;
8-
use CasParser\Core\Conversion\Contracts\ConverterSource;
9-
107
/**
118
* CAS authority to generate the document from (currently only kfintech is supported).
129
*/
13-
final class CasAuthority implements ConverterSource
10+
enum CasAuthority: string
1411
{
15-
use SdkEnum;
16-
17-
public const KFINTECH = 'kfintech';
12+
case KFINTECH = 'kfintech';
1813

19-
public const CAMS = 'cams';
14+
case CAMS = 'cams';
2015

21-
public const CDSL = 'cdsl';
16+
case CDSL = 'cdsl';
2217

23-
public const NSDL = 'nsdl';
18+
case NSDL = 'nsdl';
2419
}

src/CasParser/UnifiedResponse/DematAccount.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* additionalInfo?: AdditionalInfo|null,
1717
* boID?: string|null,
1818
* clientID?: string|null,
19-
* dematType?: DematType::*|null,
19+
* dematType?: value-of<DematType>|null,
2020
* dpID?: string|null,
2121
* dpName?: string|null,
2222
* holdings?: Holdings|null,
@@ -49,7 +49,7 @@ final class DematAccount implements BaseModel
4949
/**
5050
* Type of demat account.
5151
*
52-
* @var DematType::*|null $dematType
52+
* @var value-of<DematType>|null $dematType
5353
*/
5454
#[Api('demat_type', enum: DematType::class, optional: true)]
5555
public ?string $dematType;
@@ -85,13 +85,13 @@ public function __construct()
8585
*
8686
* You must use named parameters to construct any parameters with a default value.
8787
*
88-
* @param DematType::* $dematType
88+
* @param DematType|value-of<DematType> $dematType
8989
*/
9090
public static function with(
9191
?AdditionalInfo $additionalInfo = null,
9292
?string $boID = null,
9393
?string $clientID = null,
94-
?string $dematType = null,
94+
DematType|string|null $dematType = null,
9595
?string $dpID = null,
9696
?string $dpName = null,
9797
?Holdings $holdings = null,
@@ -102,7 +102,7 @@ public static function with(
102102
null !== $additionalInfo && $obj->additionalInfo = $additionalInfo;
103103
null !== $boID && $obj->boID = $boID;
104104
null !== $clientID && $obj->clientID = $clientID;
105-
null !== $dematType && $obj->dematType = $dematType;
105+
null !== $dematType && $obj->dematType = $dematType instanceof DematType ? $dematType->value : $dematType;
106106
null !== $dpID && $obj->dpID = $dpID;
107107
null !== $dpName && $obj->dpName = $dpName;
108108
null !== $holdings && $obj->holdings = $holdings;
@@ -147,12 +147,12 @@ public function withClientID(string $clientID): self
147147
/**
148148
* Type of demat account.
149149
*
150-
* @param DematType::* $dematType
150+
* @param DematType|value-of<DematType> $dematType
151151
*/
152-
public function withDematType(string $dematType): self
152+
public function withDematType(DematType|string $dematType): self
153153
{
154154
$obj = clone $this;
155-
$obj->dematType = $dematType;
155+
$obj->dematType = $dematType instanceof DematType ? $dematType->value : $dematType;
156156

157157
return $obj;
158158
}

src/CasParser/UnifiedResponse/DematAccount/DematType.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44

55
namespace CasParser\CasParser\UnifiedResponse\DematAccount;
66

7-
use CasParser\Core\Concerns\SdkEnum;
8-
use CasParser\Core\Conversion\Contracts\ConverterSource;
9-
107
/**
118
* Type of demat account.
129
*/
13-
final class DematType implements ConverterSource
10+
enum DematType: string
1411
{
15-
use SdkEnum;
16-
17-
public const NSDL = 'NSDL';
12+
case NSDL = 'NSDL';
1813

19-
public const CDSL = 'CDSL';
14+
case CDSL = 'CDSL';
2015
}

src/CasParser/UnifiedResponse/Meta.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
/**
1414
* @phpstan-type meta_alias = array{
15-
* casType?: CasType::*|null,
15+
* casType?: value-of<CasType>|null,
1616
* generatedAt?: \DateTimeInterface|null,
1717
* statementPeriod?: StatementPeriod|null,
1818
* }
@@ -25,7 +25,7 @@ final class Meta implements BaseModel
2525
/**
2626
* Type of CAS detected and processed.
2727
*
28-
* @var CasType::*|null $casType
28+
* @var value-of<CasType>|null $casType
2929
*/
3030
#[Api('cas_type', enum: CasType::class, optional: true)]
3131
public ?string $casType;
@@ -49,16 +49,16 @@ public function __construct()
4949
*
5050
* You must use named parameters to construct any parameters with a default value.
5151
*
52-
* @param CasType::* $casType
52+
* @param CasType|value-of<CasType> $casType
5353
*/
5454
public static function with(
55-
?string $casType = null,
55+
CasType|string|null $casType = null,
5656
?\DateTimeInterface $generatedAt = null,
5757
?StatementPeriod $statementPeriod = null,
5858
): self {
5959
$obj = new self;
6060

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

@@ -68,12 +68,12 @@ public static function with(
6868
/**
6969
* Type of CAS detected and processed.
7070
*
71-
* @param CasType::* $casType
71+
* @param CasType|value-of<CasType> $casType
7272
*/
73-
public function withCasType(string $casType): self
73+
public function withCasType(CasType|string $casType): self
7474
{
7575
$obj = clone $this;
76-
$obj->casType = $casType;
76+
$obj->casType = $casType instanceof CasType ? $casType->value : $casType;
7777

7878
return $obj;
7979
}

src/CasParser/UnifiedResponse/Meta/CasType.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,14 @@
44

55
namespace CasParser\CasParser\UnifiedResponse\Meta;
66

7-
use CasParser\Core\Concerns\SdkEnum;
8-
use CasParser\Core\Conversion\Contracts\ConverterSource;
9-
107
/**
118
* Type of CAS detected and processed.
129
*/
13-
final class CasType implements ConverterSource
10+
enum CasType: string
1411
{
15-
use SdkEnum;
16-
17-
public const NSDL = 'NSDL';
12+
case NSDL = 'NSDL';
1813

19-
public const CDSL = 'CDSL';
14+
case CDSL = 'CDSL';
2015

21-
public const CAMS_KFINTECH = 'CAMS_KFINTECH';
16+
case CAMS_KFINTECH = 'CAMS_KFINTECH';
2217
}

src/CasParser/UnifiedResponse/MutualFund/Scheme.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* nav?: float|null,
2323
* nominees?: list<string>|null,
2424
* transactions?: list<Transaction>|null,
25-
* type?: Type::*|null,
25+
* type?: value-of<Type>|null,
2626
* units?: float|null,
2727
* value?: float|null,
2828
* }
@@ -80,7 +80,7 @@ final class Scheme implements BaseModel
8080
/**
8181
* Type of mutual fund scheme.
8282
*
83-
* @var Type::*|null $type
83+
* @var value-of<Type>|null $type
8484
*/
8585
#[Api(enum: Type::class, optional: true)]
8686
public ?string $type;
@@ -109,7 +109,7 @@ public function __construct()
109109
*
110110
* @param list<string> $nominees
111111
* @param list<Transaction> $transactions
112-
* @param Type::* $type
112+
* @param Type|value-of<Type> $type
113113
*/
114114
public static function with(
115115
?AdditionalInfo $additionalInfo = null,
@@ -120,7 +120,7 @@ public static function with(
120120
?float $nav = null,
121121
?array $nominees = null,
122122
?array $transactions = null,
123-
?string $type = null,
123+
Type|string|null $type = null,
124124
?float $units = null,
125125
?float $value = null,
126126
): self {
@@ -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;
137+
null !== $type && $obj->type = $type instanceof Type ? $type->value : $type;
138138
null !== $units && $obj->units = $units;
139139
null !== $value && $obj->value = $value;
140140

@@ -231,12 +231,12 @@ public function withTransactions(array $transactions): self
231231
/**
232232
* Type of mutual fund scheme.
233233
*
234-
* @param Type::* $type
234+
* @param Type|value-of<Type> $type
235235
*/
236-
public function withType(string $type): self
236+
public function withType(Type|string $type): self
237237
{
238238
$obj = clone $this;
239-
$obj->type = $type;
239+
$obj->type = $type instanceof Type ? $type->value : $type;
240240

241241
return $obj;
242242
}

src/CasParser/UnifiedResponse/MutualFund/Scheme/Type.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,16 @@
44

55
namespace CasParser\CasParser\UnifiedResponse\MutualFund\Scheme;
66

7-
use CasParser\Core\Concerns\SdkEnum;
8-
use CasParser\Core\Conversion\Contracts\ConverterSource;
9-
107
/**
118
* Type of mutual fund scheme.
129
*/
13-
final class Type implements ConverterSource
10+
enum Type: string
1411
{
15-
use SdkEnum;
16-
17-
public const EQUITY = 'Equity';
12+
case EQUITY = 'Equity';
1813

19-
public const DEBT = 'Debt';
14+
case DEBT = 'Debt';
2015

21-
public const HYBRID = 'Hybrid';
16+
case HYBRID = 'Hybrid';
2217

23-
public const OTHER = 'Other';
18+
case OTHER = 'Other';
2419
}

src/Core/Attributes/Api.php

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

77
use CasParser\Core\Conversion\Contracts\Converter;
88
use CasParser\Core\Conversion\Contracts\ConverterSource;
9+
use CasParser\Core\Conversion\EnumOf;
910
use CasParser\Core\Conversion\ListOf;
1011
use CasParser\Core\Conversion\MapOf;
1112

@@ -15,14 +16,15 @@
1516
#[\Attribute(\Attribute::TARGET_PROPERTY)]
1617
final class Api
1718
{
18-
/**
19-
* @var class-string<ConverterSource>|Converter|string|null
20-
*/
19+
/** @var class-string<ConverterSource>|Converter|string|null */
2120
public readonly Converter|string|null $type;
2221

22+
/** @var array<string,Converter> */
23+
private static array $enumConverters = [];
24+
2325
/**
2426
* @param class-string<ConverterSource>|Converter|string|null $type
25-
* @param class-string<ConverterSource>|Converter|null $enum
27+
* @param class-string<\BackedEnum>|Converter|null $enum
2628
* @param class-string<ConverterSource>|Converter|null $union
2729
* @param class-string<ConverterSource>|Converter|string|null $list
2830
* @param class-string<ConverterSource>|Converter|string|null $map
@@ -37,6 +39,28 @@ public function __construct(
3739
public readonly bool $nullable = false,
3840
public readonly bool $optional = false,
3941
) {
40-
$this->type = $type ?? $enum ?? $union ?? ($list ? new ListOf($list) : ($map ? new MapOf($map) : null));
42+
$type ??= $union;
43+
if (null !== $list) {
44+
$type ??= new ListOf($list);
45+
}
46+
if (null !== $map) {
47+
$type ??= new MapOf($map);
48+
}
49+
if (null !== $enum) {
50+
$type ??= $enum instanceof Converter ? $enum : $this->getEnumConverter($enum);
51+
}
52+
53+
$this->type = $type;
54+
}
55+
56+
/** @property class-string<\BackedEnum> $enum */
57+
private function getEnumConverter(string $enum): Converter
58+
{
59+
if (!isset(self::$enumConverters[$enum])) {
60+
$converter = new EnumOf(array_column($enum::cases(), 'value')); // @phpstan-ignore-line
61+
self::$enumConverters[$enum] = $converter;
62+
}
63+
64+
return self::$enumConverters[$enum];
4165
}
4266
}

src/ServiceContracts/CasGeneratorContract.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface CasGeneratorContract
1919
* @param string $fromDate Start date for the CAS period (format YYYY-MM-DD)
2020
* @param string $password Password to protect the generated CAS PDF
2121
* @param string $toDate End date for the CAS period (format YYYY-MM-DD)
22-
* @param CasAuthority::* $casAuthority CAS authority to generate the document from (currently only kfintech is supported)
22+
* @param CasAuthority|value-of<CasAuthority> $casAuthority CAS authority to generate the document from (currently only kfintech is supported)
2323
* @param string $panNo PAN number (optional for some CAS authorities)
2424
*/
2525
public function generateCas(

0 commit comments

Comments
 (0)