Skip to content

Commit c32e61d

Browse files
fix:improve the type check for the json objects (#10)
1 parent 7ba9910 commit c32e61d

File tree

3 files changed

+71
-12
lines changed

3 files changed

+71
-12
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* This example lists out all tags for one customer.
4+
*
5+
* For any example to work, you must supply your own secrets in config.ini:
6+
* - username
7+
* - client
8+
* - secret_key
9+
*/
10+
11+
use BrightFlair\SpektrixAPI\CustomerNotFoundException;
12+
13+
chdir(dirname(__DIR__));
14+
require "vendor/autoload.php";
15+
16+
$config = parse_ini_file("config.ini");
17+
$client = new BrightFlair\SpektrixAPI\Client(
18+
$config["username"],
19+
$config["client"],
20+
$config["secret_key"],
21+
);
22+
23+
$email = $argv[1] ?? null;
24+
if(!$email) {
25+
fwrite(STDERR, "No email address supplied\n");
26+
exit(1);
27+
}
28+
$tagList =[];
29+
30+
try {
31+
$customer = $client->getCustomer(email: $email);
32+
echo "Customer found!\n";
33+
echo "ID: $customer->id\n";
34+
echo "Email: $customer->email\n";
35+
echo "First name: $customer->firstName\n";
36+
echo "Last name: $customer->lastName\n";
37+
$tagList = $client->getTagsForCustomer($customer);
38+
39+
}
40+
catch(CustomerNotFoundException) {
41+
echo "No customer was found with the email address $email\n";
42+
}
43+
echo "There are " . count($tagList) . " tags on the account:\n";
44+
45+
foreach($tagList as $i => $tag) {
46+
echo $i + 1;
47+
echo ": ";
48+
echo $tag->name;
49+
echo " ($tag->id)";
50+
echo "\n";
51+
}

src/Client.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,24 @@ public function getTagsForCustomer(Customer|string $customer):array {
143143
);
144144

145145
$tagList = [];
146-
/** @var JsonArrayPrimitive $jsonArray */
147-
$jsonArray = $this->json($authenticatedRequest);
148-
foreach($jsonArray->getPrimitiveValue() as $item) {
149-
array_push(
150-
$tagList,
151-
new Tag(
152-
$item->getString("id"),
153-
$item->getString("name"),
154-
)
155-
);
146+
$json = $this->json($authenticatedRequest);
147+
148+
if ($json instanceof JsonArrayPrimitive) {
149+
150+
151+
foreach ($json->getPrimitiveValue() as $item) {
152+
array_push(
153+
$tagList,
154+
new Tag(
155+
$item->getString("id"),
156+
$item->getString("name")
157+
)
158+
);
159+
160+
}
161+
156162
}
163+
157164
return $tagList;
158165
}
159166

test/phpunit/ClientTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ public function testGetTagsForCustomer():void {
234234
$expectedUri = str_replace("{id}", $customerId, $expectedUri);
235235

236236
$fetchClient = self::getFetchClient($expectedUri, 200, [
237-
["id" => "id-1", "name" => "name-1"],
238-
["id" => "id-2", "name" => "name-2"],
237+
238+
["id" => "id-1", "name" => "name-1"],
239+
["id" => "id-2", "name" => "name-2"],
239240
]);
240241

241242
$sut = new Client(self::TEST_USERNAME, self::TEST_CLIENT, self::TEST_SECRET_KEY, $fetchClient);

0 commit comments

Comments
 (0)