Skip to content

Commit d5736cf

Browse files
feat(php): differentiate null and omit
1 parent b45934c commit d5736cf

File tree

8 files changed

+81
-62
lines changed

8 files changed

+81
-62
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"$schema": "https://getcomposer.org/schema.json",
33
"autoload": {
44
"files": [
5+
"src/Core/Omit.php",
56
"src/Client.php"
67
],
78
"psr-4": {

src/Contracts/CasGeneratorContract.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use CasParser\RequestOptions;
99
use CasParser\Responses\CasGenerator\CasGeneratorGenerateCasResponse;
1010

11+
use const CasParser\Core\OMIT as omit;
12+
1113
interface CasGeneratorContract
1214
{
1315
/**
@@ -23,8 +25,8 @@ public function generateCas(
2325
$fromDate,
2426
$password,
2527
$toDate,
26-
$casAuthority = null,
27-
$panNo = null,
28+
$casAuthority = omit,
29+
$panNo = omit,
2830
?RequestOptions $requestOptions = null,
2931
): CasGeneratorGenerateCasResponse;
3032
}

src/Contracts/CasParserContract.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use CasParser\CasParser\UnifiedResponse;
88
use CasParser\RequestOptions;
99

10+
use const CasParser\Core\OMIT as omit;
11+
1012
interface CasParserContract
1113
{
1214
/**
@@ -15,9 +17,9 @@ interface CasParserContract
1517
* @param string $pdfURL URL to the CAS PDF file
1618
*/
1719
public function camsKfintech(
18-
$password = null,
19-
$pdfFile = null,
20-
$pdfURL = null,
20+
$password = omit,
21+
$pdfFile = omit,
22+
$pdfURL = omit,
2123
?RequestOptions $requestOptions = null,
2224
): UnifiedResponse;
2325

@@ -27,9 +29,9 @@ public function camsKfintech(
2729
* @param string $pdfURL URL to the CAS PDF file
2830
*/
2931
public function cdsl(
30-
$password = null,
31-
$pdfFile = null,
32-
$pdfURL = null,
32+
$password = omit,
33+
$pdfFile = omit,
34+
$pdfURL = omit,
3335
?RequestOptions $requestOptions = null,
3436
): UnifiedResponse;
3537

@@ -39,9 +41,9 @@ public function cdsl(
3941
* @param string $pdfURL URL to the CAS PDF file
4042
*/
4143
public function nsdl(
42-
$password = null,
43-
$pdfFile = null,
44-
$pdfURL = null,
44+
$password = omit,
45+
$pdfFile = omit,
46+
$pdfURL = omit,
4547
?RequestOptions $requestOptions = null,
4648
): UnifiedResponse;
4749

@@ -51,9 +53,9 @@ public function nsdl(
5153
* @param string $pdfURL URL to the CAS PDF file
5254
*/
5355
public function smartParse(
54-
$password = null,
55-
$pdfFile = null,
56-
$pdfURL = null,
56+
$password = omit,
57+
$pdfFile = omit,
58+
$pdfURL = omit,
5759
?RequestOptions $requestOptions = null,
5860
): UnifiedResponse;
5961
}

src/Core/Omit.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CasParser\Core;
6+
7+
const OMIT = Omittable::OMIT;

src/Core/Omittable.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CasParser\Core;
6+
7+
/**
8+
* @internal
9+
*/
10+
enum Omittable
11+
{
12+
case OMIT;
13+
}

src/Core/Util.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,19 +349,12 @@ public static function decodeContent(MessageInterface $rsp): mixed
349349

350350
/**
351351
* @param array<string, mixed> $arr
352-
* @param list<string> $keys
353352
*
354353
* @return array<string, mixed>
355354
*/
356-
public static function array_filter_null(array $arr, array $keys): array
355+
public static function array_filter_omit(array $arr): array
357356
{
358-
foreach ($keys as $key) {
359-
if (array_key_exists($key, $arr) && is_null($arr[$key])) {
360-
unset($arr[$key]);
361-
}
362-
}
363-
364-
return $arr;
357+
return array_filter($arr, fn ($v, $_) => OMIT !== $v, ARRAY_FILTER_USE_BOTH);
365358
}
366359

367360
/**

src/Services/CasGeneratorService.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use CasParser\RequestOptions;
1414
use CasParser\Responses\CasGenerator\CasGeneratorGenerateCasResponse;
1515

16+
use const CasParser\Core\OMIT as omit;
17+
1618
final class CasGeneratorService implements CasGeneratorContract
1719
{
1820
public function __construct(private Client $client) {}
@@ -33,19 +35,20 @@ public function generateCas(
3335
$fromDate,
3436
$password,
3537
$toDate,
36-
$casAuthority = null,
37-
$panNo = null,
38+
$casAuthority = omit,
39+
$panNo = omit,
3840
?RequestOptions $requestOptions = null,
3941
): CasGeneratorGenerateCasResponse {
40-
$args = [
41-
'email' => $email,
42-
'fromDate' => $fromDate,
43-
'password' => $password,
44-
'toDate' => $toDate,
45-
'casAuthority' => $casAuthority,
46-
'panNo' => $panNo,
47-
];
48-
$args = Util::array_filter_null($args, ['casAuthority', 'panNo']);
42+
$args = Util::array_filter_omit(
43+
[
44+
'email' => $email,
45+
'fromDate' => $fromDate,
46+
'password' => $password,
47+
'toDate' => $toDate,
48+
'casAuthority' => $casAuthority,
49+
'panNo' => $panNo,
50+
],
51+
);
4952
[$parsed, $options] = CasGeneratorGenerateCasParams::parseRequest(
5053
$args,
5154
$requestOptions

src/Services/CasParserService.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use CasParser\Core\Util;
1616
use CasParser\RequestOptions;
1717

18+
use const CasParser\Core\OMIT as omit;
19+
1820
final class CasParserService implements CasParserContract
1921
{
2022
public function __construct(private Client $client) {}
@@ -28,15 +30,14 @@ public function __construct(private Client $client) {}
2830
* @param string $pdfURL URL to the CAS PDF file
2931
*/
3032
public function camsKfintech(
31-
$password = null,
32-
$pdfFile = null,
33-
$pdfURL = null,
33+
$password = omit,
34+
$pdfFile = omit,
35+
$pdfURL = omit,
3436
?RequestOptions $requestOptions = null,
3537
): UnifiedResponse {
36-
$args = [
37-
'password' => $password, 'pdfFile' => $pdfFile, 'pdfURL' => $pdfURL,
38-
];
39-
$args = Util::array_filter_null($args, ['password', 'pdfFile', 'pdfURL']);
38+
$args = Util::array_filter_omit(
39+
['password' => $password, 'pdfFile' => $pdfFile, 'pdfURL' => $pdfURL]
40+
);
4041
[$parsed, $options] = CasParserCamsKfintechParams::parseRequest(
4142
$args,
4243
$requestOptions
@@ -61,15 +62,14 @@ public function camsKfintech(
6162
* @param string $pdfURL URL to the CAS PDF file
6263
*/
6364
public function cdsl(
64-
$password = null,
65-
$pdfFile = null,
66-
$pdfURL = null,
65+
$password = omit,
66+
$pdfFile = omit,
67+
$pdfURL = omit,
6768
?RequestOptions $requestOptions = null,
6869
): UnifiedResponse {
69-
$args = [
70-
'password' => $password, 'pdfFile' => $pdfFile, 'pdfURL' => $pdfURL,
71-
];
72-
$args = Util::array_filter_null($args, ['password', 'pdfFile', 'pdfURL']);
70+
$args = Util::array_filter_omit(
71+
['password' => $password, 'pdfFile' => $pdfFile, 'pdfURL' => $pdfURL]
72+
);
7373
[$parsed, $options] = CasParserCdslParams::parseRequest(
7474
$args,
7575
$requestOptions
@@ -94,15 +94,14 @@ public function cdsl(
9494
* @param string $pdfURL URL to the CAS PDF file
9595
*/
9696
public function nsdl(
97-
$password = null,
98-
$pdfFile = null,
99-
$pdfURL = null,
97+
$password = omit,
98+
$pdfFile = omit,
99+
$pdfURL = omit,
100100
?RequestOptions $requestOptions = null,
101101
): UnifiedResponse {
102-
$args = [
103-
'password' => $password, 'pdfFile' => $pdfFile, 'pdfURL' => $pdfURL,
104-
];
105-
$args = Util::array_filter_null($args, ['password', 'pdfFile', 'pdfURL']);
102+
$args = Util::array_filter_omit(
103+
['password' => $password, 'pdfFile' => $pdfFile, 'pdfURL' => $pdfURL]
104+
);
106105
[$parsed, $options] = CasParserNsdlParams::parseRequest(
107106
$args,
108107
$requestOptions
@@ -127,15 +126,14 @@ public function nsdl(
127126
* @param string $pdfURL URL to the CAS PDF file
128127
*/
129128
public function smartParse(
130-
$password = null,
131-
$pdfFile = null,
132-
$pdfURL = null,
129+
$password = omit,
130+
$pdfFile = omit,
131+
$pdfURL = omit,
133132
?RequestOptions $requestOptions = null,
134133
): UnifiedResponse {
135-
$args = [
136-
'password' => $password, 'pdfFile' => $pdfFile, 'pdfURL' => $pdfURL,
137-
];
138-
$args = Util::array_filter_null($args, ['password', 'pdfFile', 'pdfURL']);
134+
$args = Util::array_filter_omit(
135+
['password' => $password, 'pdfFile' => $pdfFile, 'pdfURL' => $pdfURL]
136+
);
139137
[$parsed, $options] = CasParserSmartParseParams::parseRequest(
140138
$args,
141139
$requestOptions

0 commit comments

Comments
 (0)