Skip to content

Commit 9154fac

Browse files
committed
Refactored and cleaned up
1 parent 96d7e53 commit 9154fac

9 files changed

Lines changed: 96 additions & 117 deletions

File tree

src/ApiClient.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function caseById(int $id): CaseFile
145145
return $this->createItemResult($response, CaseFile::class);
146146
}
147147

148-
public function caseCreate(array $caseData): CaseFile
148+
public function caseCreate(CaseFile $caseFile): CaseFile
149149
{
150150
$linkName = self::REL_CREATE_CASE;
151151
$url = $this->getRequestUrl($linkName);
@@ -165,11 +165,10 @@ public function caseCreate(array $caseData): CaseFile
165165
}
166166

167167
$response = $this->request(Request::METHOD_POST, $location, [
168-
'json' => $caseData,
168+
'json' => $caseFile->apiSerialize(),
169169
]);
170170

171171
if (Response::HTTP_CREATED !== $response->getStatusCode()) {
172-
// @TODO Log stuff.
173172
$message = 'Cannot create case';
174173
throw $this->createRuntimeException($message, response: $response);
175174
}
@@ -181,8 +180,8 @@ public function caseFileUpdate(CaseFile $caseFile, CaseFile $updatedCaseFile): b
181180
{
182181
$url = $caseFile->links->getUrl('self');
183182
$diff = new JsonDiff(
184-
$caseFile->jsonSerialize(),
185-
$updatedCaseFile->jsonSerialize(),
183+
$caseFile->apiSerialize(),
184+
$updatedCaseFile->apiSerialize(),
186185
options: JsonDiff::SKIP_TEST_OPS,
187186
);
188187
$response = $this->request(Request::METHOD_PATCH, $url, [
@@ -241,7 +240,7 @@ public function matterByMatterNumber(string $matterNumber): Matter
241240
return $this->createItemResult($response, Matter::class);
242241
}
243242

244-
public function matterCreate(array $matterData, ?CaseFile $case = null): Matter
243+
public function matterCreate(Matter $matter, ?CaseFile $case = null): Matter
245244
{
246245
$linkName = self::REL_CREATE_MATTER;
247246
$url = null !== $case ? $case->links->getUrl($linkName) : $this->getRequestUrl($linkName);
@@ -265,7 +264,7 @@ public function matterCreate(array $matterData, ?CaseFile $case = null): Matter
265264
}
266265

267266
$response = $this->request(Request::METHOD_POST, $location, [
268-
'json' => $matterData,
267+
'json' => $matter->apiSerialize(),
269268
]);
270269

271270
if (Response::HTTP_CREATED !== $response->getStatusCode()) {
@@ -310,7 +309,7 @@ public function documentById(int $id): Document
310309
return $this->createItemResult($response, Document::class);
311310
}
312311

313-
public function documentCreate(array $documentData, string $filename, Matter $matter): Document
312+
public function documentCreate(Document $document, string $filename, Matter $matter): Document
314313
{
315314
$linkName = self::REL_CREATE_DOCUMENT;
316315
$url = $matter->links->getUrl($linkName);
@@ -337,7 +336,7 @@ public function documentCreate(array $documentData, string $filename, Matter $ma
337336

338337
try {
339338
$response = $this->request(Request::METHOD_POST, $location, [
340-
'body' => $documentData
339+
'body' => $document->apiSerialize()
341340
+ [
342341
'File' => $fileHandle,
343342
],
@@ -442,6 +441,9 @@ protected function getAccessToken(): array
442441
});
443442
}
444443

444+
/**
445+
* @param array<array-key, mixed> $options
446+
*/
445447
protected function request(string $method, string $path, array $options = []): ResponseInterface
446448
{
447449
if (Request::METHOD_PATCH === $method && array_key_exists('json', $options)) {
@@ -512,6 +514,9 @@ protected function configureOptions(OptionsResolver $resolver): void
512514
->setAllowedTypes('cache_item_lifetime', 'int');
513515
}
514516

517+
/**
518+
* @param array<string, string|int> $values
519+
*/
515520
protected function getRequestUrl(string $rel, array $values = []): string
516521
{
517522
$index = $this->getServiceIndex();
@@ -524,6 +529,9 @@ protected function getRequestUrl(string $rel, array $values = []): string
524529
return $this->replacePlaceholders($url, $values);
525530
}
526531

532+
/**
533+
* @param array<string, string|int> $values
534+
*/
527535
protected function getSearchRequestUrl(string $rel, array $values): string
528536
{
529537
$url = $this->getRequestUrl($rel, []);
@@ -553,6 +561,9 @@ protected function getSearchRequestUrl(string $rel, array $values): string
553561
return $this->replacePlaceholders($url, $values);
554562
}
555563

564+
/**
565+
* @param array<string, string|int> $values
566+
*/
556567
protected function replacePlaceholders(string $url, array $values): string
557568
{
558569
// Replace URL placeholders ('{…}')
@@ -611,6 +622,7 @@ protected function createItemResult(ResponseInterface $response, string $class):
611622
return $class::fromSimpleXMLElement($item);
612623
}
613624

625+
// @phpstan-ignore missingType.iterableValue
614626
public function log($level, \Stringable|string $message, array $context = []): void
615627
{
616628
if (null !== $this->logger) {
@@ -682,8 +694,8 @@ public function getLinkCollection(AbstractF2Item $item, string $rel): Collection
682694

683695
private function computeDiff(AbstractF2Item $old, AbstractF2Item $new): JsonDiff
684696
{
685-
$oldValues = $old::filterForJsonPatch($old->jsonSerialize());
686-
$newValues = $new::filterForJsonPatch($new->jsonSerialize());
697+
$oldValues = $old->apiSerialize();
698+
$newValues = $new->apiSerialize();
687699

688700
return new JsonDiff($oldValues, $newValues, options: JsonDiff::SKIP_TEST_OPS);
689701
}

src/Model/AbstractF2Item.php

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

77
use ItkDev\F2ApiClient\Model\F2Item\Links;
88

9-
abstract class AbstractF2Item extends AbstractItem
9+
abstract class AbstractF2Item extends AbstractItem implements \Stringable
1010
{
1111
public ?int $id = null;
1212
public Links $links;
@@ -32,6 +32,16 @@ public function __toString(): string
3232
return sprintf('F2 Item (%s)', $this->id);
3333
}
3434

35+
/**
36+
* Serialize with the names (paths) needed for JSON patch.
37+
*
38+
* @return array<string, mixed>
39+
*/
40+
public function apiSerialize(): array
41+
{
42+
return [];
43+
}
44+
3545
/**
3646
* Serialize with the names (paths) needed for JSON patch.
3747
*
@@ -42,24 +52,10 @@ public function __toString(): string
4252
#[\Override]
4353
public function jsonSerialize(): array
4454
{
45-
return [
55+
return $this->apiSerialize()
56+
+ [
4657
'id' => $this->id,
4758
'links' => $this->links->jsonSerialize(),
4859
];
4960
}
50-
51-
/**
52-
* @param array<string, mixed> $values
53-
*
54-
* @return array<string, mixed>
55-
*/
56-
public static function filterForJsonPatch(array $values): array
57-
{
58-
// Keep only keys starting with a capital letter.
59-
return array_filter(
60-
$values,
61-
static fn (string $key) => ucfirst($key) === $key,
62-
ARRAY_FILTER_USE_KEY,
63-
);
64-
}
6561
}

src/Model/AbstractItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ItkDev\F2ApiClient\Model;
66

7-
abstract class AbstractItem implements \JsonSerializable, \Stringable
7+
abstract class AbstractItem implements \JsonSerializable
88
{
99
public static function fromSimpleXMLElement(\SimpleXMLElement $sxe): static
1010
{

src/Model/CaseFile.php

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,16 @@
77
final class CaseFile extends AbstractF2Item
88
{
99
// @see resources/f2-rest-docs/f2-rest-docs-v13s.html#56
10-
11-
public string $caseNumber;
12-
13-
public string $title;
14-
public bool $closed;
15-
public JournalPlan $journalPlan;
16-
public ProcessInstruction $processInstruction;
17-
public ?\DateTimeImmutable $deadline;
18-
public \DateTimeImmutable $createdDate;
19-
public \DateTimeImmutable $modifiedDate;
20-
public PartyItemAbstract $modifiedBy;
21-
public ?PartyItemAbstract $responsible;
22-
23-
// Link
24-
// List of Link (read-only)
25-
// One or more links to other related resources.
26-
// Matters
27-
// List of Matter
28-
// List of all accessible matters on the case.
29-
/** @var Matter[] */
30-
public array $matters;
10+
public ?string $caseNumber = null;
11+
public ?string $title = null;
12+
public bool $closed = false;
13+
public ?JournalPlan $journalPlan = null;
14+
public ?ProcessInstruction $processInstruction = null;
15+
public ?\DateTimeImmutable $deadline = null;
16+
public ?\DateTimeImmutable $createdDate = null;
17+
public ?\DateTimeImmutable $modifiedDate = null;
18+
public ?Party $modifiedBy = null;
19+
public ?Party $responsible = null;
3120

3221
// @mago-ignore analysis:non-documented-property,mixed-argument
3322
#[\Override]
@@ -44,10 +33,8 @@ public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
4433
$this->deadline = $this->createDateTime($sxe->Deadline);
4534
$this->createdDate = $this->createDateTime($sxe->CreatedDate);
4635
$this->modifiedDate = $this->createDateTime($sxe->ModifiedDate);
47-
$this->modifiedBy = PartyItemAbstract::fromSimpleXMLElement($sxe->ModifiedBy);
48-
$this->responsible = $sxe->Responsible ? PartyItemAbstract::fromSimpleXMLElement($sxe->Responsible) : null;
49-
50-
$this->matters = static::listOf(Matter::class, $sxe->Matters);
36+
$this->modifiedBy = Party::fromSimpleXMLElement($sxe->ModifiedBy);
37+
$this->responsible = $sxe->Responsible ? Party::fromSimpleXMLElement($sxe->Responsible) : null;
5138

5239
return $this;
5340
}
@@ -59,21 +46,26 @@ public function __toString(): string
5946
}
6047

6148
#[\Override]
62-
public function jsonSerialize(): array
49+
public function apiSerialize(): array
6350
{
6451
return [
6552
'CaseNumber' => $this->caseNumber,
6653
'Title' => $this->title,
6754
'Closed' => $this->closed,
6855
'Deadline' => $this->deadline,
56+
];
57+
}
6958

70-
'journalPlan' => $this->journalPlan->jsonSerialize(),
71-
'processInstruction' => $this->processInstruction->jsonSerialize(),
59+
#[\Override]
60+
public function jsonSerialize(): array
61+
{
62+
return [
63+
'journalPlan' => $this->journalPlan?->jsonSerialize(),
64+
'processInstruction' => $this->processInstruction?->jsonSerialize(),
7265
'createdDate' => $this->jsonSerializeDateTime($this->createdDate),
7366
'modifiedDate' => $this->jsonSerializeDateTime($this->modifiedDate),
74-
'modifiedBy' => $this->modifiedBy->jsonSerialize(),
67+
'modifiedBy' => $this->modifiedBy?->jsonSerialize(),
7568
'responsible' => $this->responsible?->jsonSerialize(),
76-
'matters' => array_map(static fn (Matter $matter) => $matter->jsonSerialize(), $this->matters),
7769
] + parent::jsonSerialize();
7870
}
7971
}

src/Model/Collection.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/**
1010
* A (simple) collection.
1111
*/
12-
final class Collection extends AbstractF2Item
12+
final class Collection extends AbstractItem
1313
{
1414
public string $title;
1515
/** @var Item[] */
@@ -19,8 +19,6 @@ final class Collection extends AbstractF2Item
1919
#[\Override]
2020
public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
2121
{
22-
parent::setFromSimpleXMLElement($sxe);
23-
2422
$this->title = (string) $sxe->Title;
2523
$this->items = [];
2624
/** @var \SimpleXMLElement[] $items */
@@ -32,17 +30,10 @@ public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
3230
return $this;
3331
}
3432

35-
#[\Override]
36-
public function __toString(): string
37-
{
38-
return sprintf('%s (%s)', $this->title, $this->id);
39-
}
40-
4133
#[\Override]
4234
public function jsonSerialize(): array
4335
{
4436
return [
45-
'id' => $this->id,
4637
'title' => $this->title,
4738
'items' => array_map(static fn (Item $item) => $item->jsonSerialize(), $this->items),
4839
];

src/Model/Collection/Item.php

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

55
namespace ItkDev\F2ApiClient\Model\Collection;
66

7-
use ItkDev\F2ApiClient\Model\AbstractF2Item;
7+
use ItkDev\F2ApiClient\Model\AbstractItem;
88

9-
final class Item extends AbstractF2Item
9+
final class Item extends AbstractItem
1010
{
11+
public int $id;
1112
public string $title;
1213

1314
// @mago-ignore analysis:non-documented-property
1415
public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
1516
{
16-
parent::setFromSimpleXMLElement($sxe);
17-
17+
$this->id = (int) $sxe->Id;
1818
$this->title = (string) $sxe->Title;
1919

2020
return $this;
@@ -30,7 +30,7 @@ public function __toString(): string
3030
public function jsonSerialize(): array
3131
{
3232
return [
33-
'Title' => $this->title,
34-
] + parent::jsonSerialize();
33+
'title' => $this->title,
34+
];
3535
}
3636
}

src/Model/Document.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
final class Document extends AbstractF2Item
88
{
99
public string $title;
10-
public ?string $description = null;
10+
public string $description = '';
1111
public ?\DateTimeImmutable $createdDate = null;
1212
public ?\DateTimeImmutable $modifiedDate = null;
1313

@@ -33,12 +33,18 @@ public function __toString(): string
3333
}
3434

3535
#[\Override]
36-
public function jsonSerialize(): array
36+
public function apiSerialize(): array
3737
{
3838
return [
3939
'Title' => $this->title,
4040
'Description' => $this->description,
41+
];
42+
}
4143

44+
#[\Override]
45+
public function jsonSerialize(): array
46+
{
47+
return [
4248
'createdDate' => $this->jsonSerializeDateTime($this->createdDate),
4349
'modifiedDate' => $this->jsonSerializeDateTime($this->modifiedDate),
4450
] + parent::jsonSerialize();

0 commit comments

Comments
 (0)