Skip to content

Commit c084a53

Browse files
committed
Refactored
1 parent 666f06c commit c084a53

10 files changed

Lines changed: 224 additions & 171 deletions

File tree

src/Client/ApiClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ protected function getSearchRequestUrl(string $rel, array $values): string
247247

248248
try {
249249
$cache = $this->getCache();
250-
$cacheKey = sha1(__METHOD__.'|||'.$rel);
250+
$cacheKey = sha1(__METHOD__ . '|||' . $rel);
251251

252252
$url = $cache->get($cacheKey, function (CacheItemInterface $item) use ($url) {
253253
$item->expiresAfter((int) $this->options['cache_item_lifetime']);

src/Command/F2ApiClientCommand.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public function __invoke(
2323
string $action,
2424
#[Argument]
2525
?string $arg = null,
26+
#[Option(description: 'Show links.')]
27+
bool $showLinks = false,
2628
#[Option(description: 'Path to to cache directory. If not specified, no requests will be cached.')]
2729
?string $cacheDirectory = null,
2830
): int
@@ -31,15 +33,21 @@ public function __invoke(
3133

3234
$response = match ($action) {
3335
'getServiceIndex' => $client->getServiceIndex(),
34-
'caseSearch' => $client->caseSearch((string)$arg),
35-
'caseById' => $client->caseById((string)$arg),
36-
'matterSearch' => $client->matterSearch((string)$arg),
37-
'matterById' => $client->matterById((string)$arg),
38-
'matterByMatterNumber' => $client->matterByMatterNumber((string)$arg),
39-
'documentById'=> $client->documentById((string)$arg),
36+
'caseSearch' => $client->caseSearch((string) $arg),
37+
'caseById' => $client->caseById((string) $arg),
38+
'matterSearch' => $client->matterSearch((string) $arg),
39+
'matterById' => $client->matterById((string) $arg),
40+
'matterByMatterNumber' => $client->matterByMatterNumber((string) $arg),
41+
'documentById' => $client->documentById((string) $arg),
4042
default => throw new InvalidArgumentException(sprintf('Invalid action: %s', $action)),
4143
};
4244

45+
$response = json_decode(json_encode($response), true);
46+
if (!$showLinks) {
47+
// @todo Recurse!
48+
unset($response['links']);
49+
}
50+
4351
$io->writeln((string) json_encode(
4452
$response,
4553
JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,

src/Model/AbstractItem.php

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

77
abstract class AbstractItem implements \JsonSerializable, \Stringable
88
{
9-
abstract static function fromSimpleXMLElement(\SimpleXMLElement $sxe);
9+
public static function fromSimpleXMLElement(\SimpleXMLElement $sxe): static
10+
{
11+
return (new static())->setFromSimpleXMLElement($sxe);
12+
}
13+
14+
public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): self
15+
{
16+
return $this;
17+
}
1018

19+
/**
20+
* @param class-string<AbstractItem> $class
21+
*/
1122
protected static function listOf(string $class, \SimpleXMLElement $sxe): array
1223
{
1324
$items = [];
1425
foreach ($sxe as $child) {
15-
$items[] = $child::class::fromSimpleXMLElement($child);
26+
$items[] = $class::fromSimpleXMLElement($child);
1627
}
1728

1829
return $items;

src/Model/Atom.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,29 @@
66

77
final class Atom extends AbstractItem
88
{
9-
public function __construct(
10-
protected string $id,
11-
protected string $title,
12-
protected \DateTimeImmutable $published,
13-
protected \DateTimeImmutable $updated,
14-
)
15-
{
16-
}
9+
public string $id;
10+
public string $title;
11+
public \DateTimeImmutable $published;
12+
public \DateTimeImmutable $updated;
1713

18-
public static function fromSimpleXMLElement(\SimpleXMLElement $sxe)
14+
#[\Override]
15+
public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
1916
{
20-
return new self(
21-
id: (string) $sxe->id,
22-
title: (string) $sxe->title,
23-
published: new \DateTimeImmutable((string) $sxe->published),
24-
updated: new \DateTimeImmutable((string) $sxe->updated),
25-
);
17+
$this->id = (string) $sxe->id;
18+
$this->title = (string) $sxe->title;
19+
$this->published = new \DateTimeImmutable((string) $sxe->published);
20+
$this->updated = new \DateTimeImmutable((string) $sxe->updated);
21+
22+
return $this;
2623
}
2724

25+
#[\Override]
2826
public function __toString(): string
2927
{
3028
return sprintf('%s (%s)', $this->title, $this->id);
3129
}
3230

31+
#[\Override]
3332
public function jsonSerialize(): array
3433
{
3534
return [

src/Model/CaseFile.php

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,88 +4,61 @@
44

55
namespace ItkDev\F2ApiClient\Model;
66

7-
final class CaseFile extends AbstractItem
7+
final class CaseFile extends F2Item
88
{
9-
/**
10-
* @see resources/f2-rest-docs/f2-rest-docs-v13s.html#56
11-
*/
12-
public function __construct(
13-
// Id
14-
// int
15-
// Internal identifier.
16-
public readonly int $id,
9+
// @see resources/f2-rest-docs/f2-rest-docs-v13s.html#56
1710

18-
// CaseNumber
19-
// string
20-
// Official case number (for instance "2012 - 342").
21-
public readonly string $caseNumber,
11+
public string $caseNumber;
2212

23-
// Title
24-
// string
25-
// Case title.
26-
public readonly string $title,
13+
public string $title;
14+
public \DateTimeImmutable $createdDate;
15+
public \DateTimeImmutable $modifiedDate;
16+
public PartyItem $modifiedBy;
17+
public PartyItem $responsible;
2718

28-
// CreatedDate
29-
// DateTime
30-
// Date and time the case was created.
31-
public readonly \DateTimeImmutable $createdDate,
19+
// Link
20+
// List of Link (read-only)
21+
// One or more links to other related resources.
22+
// Matters
23+
// List of Matter
24+
// List of all accessible matters on the case.
25+
/** @var Matter[] */
26+
public array $matters;
3227

33-
// ModifiedDate
34-
// DateTime
35-
// Date and time the case was last modified.
36-
public readonly \DateTimeImmutable $modifiedDate,
28+
public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
29+
{
30+
parent::setFromSimpleXMLElement($sxe);
3731

38-
// ModifiedBy
39-
// PartyItem
40-
// Last user who modified the case.
41-
public readonly PartyItem $modifiedBy,
32+
$this->id = (int) $sxe->Id;
33+
$this->caseNumber = (string) $sxe->CaseNumber;
34+
$this->title = (string) $sxe->Title;
35+
$this->createdDate = new \DateTimeImmutable((string) $sxe->CreatedDate);
36+
$this->modifiedDate = new \DateTimeImmutable((string) $sxe->ModifiedDate);
37+
$this->modifiedBy = PartyItem::fromSimpleXMLElement($sxe->ModifiedBy);
38+
$this->responsible = PartyItem::fromSimpleXMLElement($sxe->Responsible);
4239

43-
// Responsible
44-
// PartyItem
45-
// The party who is responsible for the case.
46-
public readonly PartyItem $responsible,
47-
// Link
48-
// List of Link (read-only)
49-
// One or more links to other related resources.
50-
// Matters
51-
// List of Matter
52-
// List of all accessible matters on the case.
53-
// /** @var Matter[] */
54-
// public readonly array $matters,
55-
)
56-
{
57-
}
40+
$this->matters = static::listOf(Matter::class, $sxe->Matters);
5841

59-
public static function fromSimpleXMLElement(\SimpleXMLElement $sxe)
60-
{
61-
return new self(
62-
id: (int) $sxe->Id,
63-
caseNumber: (string) $sxe->CaseNumber,
64-
title: (string) $sxe->Title,
65-
createdDate: new \DateTimeImmutable((string) $sxe->CreatedDate),
66-
modifiedDate: new \DateTimeImmutable((string) $sxe->ModifiedDate),
67-
modifiedBy: PartyItem::fromSimpleXMLElement($sxe->ModifiedBy),
68-
responsible: PartyItem::fromSimpleXMLElement($sxe->Responsible),
69-
// matters: static::listOf(Matter::class, $sxe->Matters),
70-
);
42+
return $this;
7143
}
7244

73-
public function __toString()
45+
#[\Override]
46+
public function __toString(): string
7447
{
7548
return sprintf('Case %s: %s', $this->caseNumber, $this->title);
7649
}
7750

51+
#[\Override]
7852
public function jsonSerialize(): array
7953
{
8054
return [
81-
'id' => $this->id,
8255
'caseNumber' => $this->caseNumber,
8356
'title' => $this->title,
8457
'createdDate' => $this->createdDate,
8558
'modifiedDate' => $this->modifiedDate,
8659
'modifiedBy' => $this->modifiedBy->jsonSerialize(),
8760
'responsible' => $this->responsible->jsonSerialize(),
88-
// 'matters' => array_map(static fn (Matter $matter) => $matter->jsonSerialize(), $this->matters),
89-
];
61+
'matters' => array_map(static fn(Matter $matter) => $matter->jsonSerialize(), $this->matters),
62+
] + parent::jsonSerialize();
9063
}
9164
}

src/Model/Document.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@
44

55
namespace ItkDev\F2ApiClient\Model;
66

7-
final class Document extends AbstractItem
7+
final class Document extends F2Item
88
{
9-
private Matter $matter;
10-
11-
public function getMatter(): Matter
12-
{
13-
return $this->matter;
14-
}
15-
16-
public function setMatter(Matter $matter): Document
17-
{
18-
$this->matter = $matter;
19-
20-
return $this;
21-
}
229
}

src/Model/F2Item.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ItkDev\F2ApiClient\Model;
6+
7+
use ItkDev\F2ApiClient\Model\F2Item\Links;
8+
9+
abstract class F2Item extends AbstractItem
10+
{
11+
public ?int $id = null;
12+
public ?Links $links = null;
13+
14+
#[\Override]
15+
public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
16+
{
17+
parent::setFromSimpleXMLElement($sxe);
18+
19+
$this->id = (int) $sxe->Id;
20+
$this->links = Links::fromSimpleXMLElement($sxe);
21+
22+
return $this;
23+
}
24+
25+
#[\Override]
26+
public function __toString(): string
27+
{
28+
return sprintf('F2 Item (%s)', $this->id);
29+
}
30+
31+
#[\Override]
32+
public function jsonSerialize(): array
33+
{
34+
return [
35+
'id' => $this->id,
36+
'links' => $this->links?->jsonSerialize(),
37+
];
38+
}
39+
}

src/Model/F2Item/Links.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace ItkDev\F2ApiClient\Model\F2Item;
6+
7+
/**
8+
* @phpstan-type Link array{rel: string, href: string, title:string}
9+
*/
10+
class Links implements \JsonSerializable
11+
{
12+
public function __construct(
13+
/** @var array<string, Link> */
14+
private readonly array $links,
15+
)
16+
{
17+
}
18+
19+
static function fromSimpleXMLElement(\SimpleXMLElement $sxe): self
20+
{
21+
$links = [];
22+
foreach ($sxe->Link as $link) {
23+
$attributes = ((array) $link)['@attributes'] ?? null;
24+
if (is_array($attributes) && array_key_exists('rel', $attributes)) {
25+
$links[$attributes['rel']] = $attributes;
26+
}
27+
}
28+
29+
return new self(links: $links);
30+
}
31+
32+
/**
33+
* @param string $rel
34+
* @return Link
35+
*/
36+
public function getLink(string $rel): ?array
37+
{
38+
return $this->links[$rel] ?? null;
39+
}
40+
41+
#[\Override]
42+
public function jsonSerialize(): array
43+
{
44+
return $this->links;
45+
}
46+
}

0 commit comments

Comments
 (0)