Skip to content

Commit 96d7e53

Browse files
committed
Cleaned up
1 parent 80239c2 commit 96d7e53

11 files changed

Lines changed: 61 additions & 33 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ See the [F2 REST API documentation](resources/f2-rest-docs/f2-rest-docs-v13s.htm
1010
composer require itk-dev/f2-api-client
1111
```
1212

13+
## Usage
14+
15+
See [F2ApiClientCommand](src/Command/F2ApiClientCommand.php) for an example usage.
16+
1317
## Testing
1418

1519
Edit `.env.local` and set these variables:

phpstan.dist.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,3 @@ parameters:
44
- src
55

66
treatPhpDocTypesAsCertain: false
7-
8-
ignoreErrors:
9-
- message: '#Method ItkDev\\F2ApiClient\\ApiClient::[^ ]+\(\) has parameter \$[^ ]+ with no value type specified in iterable type array.#'
10-
path: src/*
11-
12-
- message: '#Method ItkDev\\F2ApiClient\\Model\\[^ ]+::jsonSerialize\(\) return type has no value type specified in iterable type array.#'
13-
path: src/*
14-
15-
- message: '#Method ItkDev\\F2ApiClient\\Command\\F2ApiClientCommand::responseToArray\(\) has parameter \$response with no value type specified in iterable type array.#'
16-
path: src/Command/F2ApiClientCommand.php

src/ApiClient.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,10 +682,8 @@ public function getLinkCollection(AbstractF2Item $item, string $rel): Collection
682682

683683
private function computeDiff(AbstractF2Item $old, AbstractF2Item $new): JsonDiff
684684
{
685-
$oldValues = $old->jsonSerialize();
686-
$newValues = $new->jsonSerialize();
687-
688-
// @todo Filter out some values, e.g. timestamps.
685+
$oldValues = $old::filterForJsonPatch($old->jsonSerialize());
686+
$newValues = $new::filterForJsonPatch($new->jsonSerialize());
689687

690688
return new JsonDiff($oldValues, $newValues, options: JsonDiff::SKIP_TEST_OPS);
691689
}

src/Command/F2ApiClientCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ private function createClient(?string $cacheDirectory): ApiClient
8282
}
8383

8484
/**
85+
* @param AbstractItem|array<array-key, mixed> $response
86+
*
8587
* @return array<array-key, mixed>
8688
*/
8789
private function responseToArray(array|AbstractItem $response): array

src/Model/AbstractF2Item.php

Lines changed: 20 additions & 1 deletion
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 implements \Stringable
9+
abstract class AbstractF2Item extends AbstractItem
1010
{
1111
public ?int $id = null;
1212
public Links $links;
@@ -34,6 +34,10 @@ public function __toString(): string
3434

3535
/**
3636
* Serialize with the names (paths) needed for JSON patch.
37+
*
38+
* Only names starting with a capital letter will be included in JSON patch'ing (cf. self::filterForJsonPatch).
39+
*
40+
* @see self::filterForJsonPatch()
3741
*/
3842
#[\Override]
3943
public function jsonSerialize(): array
@@ -43,4 +47,19 @@ public function jsonSerialize(): array
4347
'links' => $this->links->jsonSerialize(),
4448
];
4549
}
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+
}
4665
}

src/Model/AbstractItem.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ protected function createDateTime(\SimpleXMLElement|string $value): \DateTimeImm
4747
return (new \DateTimeImmutable((string) $value, $apiTimeZone))->setTimeZone($appTimeZone);
4848
}
4949

50+
/**
51+
* @return array<string, mixed>
52+
*/
53+
abstract public function jsonSerialize(): array;
54+
5055
protected function jsonSerializeDateTime(?\DateTimeImmutable $dateTime): ?string
5156
{
5257
return $dateTime?->format(\DateTimeInterface::ATOM);

src/Model/CaseFile.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@ public function jsonSerialize(): array
6565
'CaseNumber' => $this->caseNumber,
6666
'Title' => $this->title,
6767
'Closed' => $this->closed,
68-
'JournalPlan' => $this->journalPlan->jsonSerialize(),
69-
'ProcessInstruction' => $this->processInstruction->jsonSerialize(),
7068
'Deadline' => $this->deadline,
71-
'CreatedDate' => $this->jsonSerializeDateTime($this->createdDate),
72-
'ModifiedDate' => $this->jsonSerializeDateTime($this->modifiedDate),
73-
'ModifiedBy' => $this->modifiedBy->jsonSerialize(),
74-
'Responsible' => $this->responsible?->jsonSerialize(),
75-
'Matters' => array_map(static fn (Matter $matter) => $matter->jsonSerialize(), $this->matters),
69+
70+
'journalPlan' => $this->journalPlan->jsonSerialize(),
71+
'processInstruction' => $this->processInstruction->jsonSerialize(),
72+
'createdDate' => $this->jsonSerializeDateTime($this->createdDate),
73+
'modifiedDate' => $this->jsonSerializeDateTime($this->modifiedDate),
74+
'modifiedBy' => $this->modifiedBy->jsonSerialize(),
75+
'responsible' => $this->responsible?->jsonSerialize(),
76+
'matters' => array_map(static fn (Matter $matter) => $matter->jsonSerialize(), $this->matters),
7677
] + parent::jsonSerialize();
7778
}
7879
}

src/Model/Collection/Item.php

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

77
use ItkDev\F2ApiClient\Model\AbstractF2Item;
88

9-
final class Item extends AbstractF2Item implements \JsonSerializable
9+
final class Item extends AbstractF2Item
1010
{
1111
public string $title;
1212

src/Model/Document.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public function jsonSerialize(): array
3838
return [
3939
'Title' => $this->title,
4040
'Description' => $this->description,
41-
'CreatedDate' => $this->jsonSerializeDateTime($this->createdDate),
42-
'ModifiedDate' => $this->jsonSerializeDateTime($this->modifiedDate),
41+
42+
'createdDate' => $this->jsonSerializeDateTime($this->createdDate),
43+
'modifiedDate' => $this->jsonSerializeDateTime($this->modifiedDate),
4344
] + parent::jsonSerialize();
4445
}
4546
}

src/Model/F2Item/Links.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,25 @@
55
namespace ItkDev\F2ApiClient\Model\F2Item;
66

77
use ItkDev\F2ApiClient\Exception\RuntimeException;
8+
use ItkDev\F2ApiClient\Model\AbstractItem;
89

910
/**
1011
* @phpstan-type Link array{rel: string, href: string, title:string}
1112
*/
12-
final class Links implements \JsonSerializable
13+
final class Links extends AbstractItem
1314
{
1415
public function __construct(
1516
/** @var array<string, Link> */
1617
private readonly array $links,
1718
) {
1819
}
1920

21+
#[\Override]
22+
public function __toString(): string
23+
{
24+
return self::class;
25+
}
26+
2027
// @mago-ignore analysis:non-documented-property
2128
public static function fromSimpleXMLElement(\SimpleXMLElement $sxe): static
2229
{

0 commit comments

Comments
 (0)