Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract Model #118

Merged
merged 5 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [Refactor: Use Query-Parameters for Where-Clause](https://github.com/5pm-HDH/churchtools-api/pull/106)
- [Fix Unit-Test for Person Update-Request](https://github.com/5pm-HDH/churchtools-api/pull/109)
- [Fix Integration-Test: Cache Api-key](https://github.com/5pm-HDH/churchtools-api/pull/113)
- [Abstract Model](https://github.com/5pm-HDH/churchtools-api/pull/118)

### Fixed

Expand Down
21 changes: 21 additions & 0 deletions docs/out/Models.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ The attributes of a model can be used accessed with getters and setter.

```

The model id can be retrieved with the `getId` getter. There is also a null-safe getter (`getIdOrFail`) and a integer casted getter (`getIdAsInteger`):

```php
use CTApi\Models\Person;

// can be null:
var_dump( $this->person->getId());
// Output: "21"


// throws CTModelException if id is null:
var_dump( $this->person->getIdOrFail());
// Output: "21"


// return int or throws CTModelException:
var_dump( $this->person->getIdAsInteger());
// Output: 21


```

**`request`-method (one-to-one - singular)**

Expand Down
42 changes: 0 additions & 42 deletions docs/out/Requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,8 @@ $twoPersons = PersonRequest::where('ids', [219, 318])->get();

## Details

* [AuthRequest](#authrequest)
* [PersonRequest](#personrequest)
* [EventRequest](#eventrequest)
* [EventRequestAgenda](#eventrequestagenda)

### AuthRequest

* ✅ authWithEmailAndPassword
* ❌ all; find / findOrFail; where; orderBy; limit

### PersonRequest

* ✅ whoami
* ✅ all
* ✅ find / findOrFail
* ✅ where
* ✅ orderBy

**Where filter criteria:**

| Criteria | Value | Description |
Expand All @@ -166,41 +150,15 @@ $twoPersons = PersonRequest::where('ids', [219, 318])->get();

### EventRequest

* ✅ all
* ✅ find / findOrFail
* ✅ where
* ✅ orderBy

**Where filter criteria:**

| Criteria | Value | Description |
| --- | --- | --- |
| from | date-string (YYYY-MM-DD) | select events beginning with date |
| to | date-string (YYYY-MM-DD) | select events ending with date |

### EventRequestAgenda

* ✅ get

```php
use \CTApi\Requests\EventAgendaRequest;
use \CTApi\Models\Event;

$eventId = 21;
$event = Event::createModelFromData(['id' => $eventId]);

$agenda = EventAgendaRequest::fromEvent($eventId)->get();
$agenda = $event->requestAgenda();

```

### SongRequest

* ✅ all
* ✅ find / findOrFail
* ✅ where
* ✅ orderBy

**Where filter criteria:**

| Criteria | Value | Description |
Expand Down
3 changes: 3 additions & 0 deletions docs/src/ressources/Models.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ The attributes of a model can be used accessed with getters and setter.

{{ \Tests\Unit\Docs\ModelTest.testGetterAndSetter }}

The model id can be retrieved with the `getId` getter. There is also a null-safe getter (`getIdOrFail`) and a integer casted getter (`getIdAsInteger`):

{{ \Tests\Unit\Docs\ModelTest.testGetId }}

**`request`-method (one-to-one - singular)**

Expand Down
41 changes: 0 additions & 41 deletions docs/src/ressources/Requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,8 @@ $twoPersons = PersonRequest::where('ids', [219, 318])->get();

## Details

* [AuthRequest](#authrequest)
* [PersonRequest](#personrequest)
* [EventRequest](#eventrequest)
* [EventRequestAgenda](#eventrequestagenda)

### AuthRequest

* ✅ authWithEmailAndPassword
* ❌ all; find / findOrFail; where; orderBy; limit

### PersonRequest

* ✅ whoami
* ✅ all
* ✅ find / findOrFail
* ✅ where
* ✅ orderBy

**Where filter criteria:**

| Criteria | Value | Description |
Expand All @@ -124,40 +108,15 @@ $twoPersons = PersonRequest::where('ids', [219, 318])->get();

### EventRequest

* ✅ all
* ✅ find / findOrFail
* ✅ where
* ✅ orderBy

**Where filter criteria:**

| Criteria | Value | Description |
| --- | --- | --- |
| from | date-string (YYYY-MM-DD) | select events beginning with date |
| to | date-string (YYYY-MM-DD) | select events ending with date |

### EventRequestAgenda

* ✅ get

```php
use \CTApi\Requests\EventAgendaRequest;
use \CTApi\Models\Event;

$eventId = 21;
$event = Event::createModelFromData(['id' => $eventId]);

$agenda = EventAgendaRequest::fromEvent($eventId)->get();
$agenda = $event->requestAgenda();
```

### SongRequest

* ✅ all
* ✅ find / findOrFail
* ✅ where
* ✅ orderBy

**Where filter criteria:**

| Criteria | Value | Description |
Expand Down
11 changes: 1 addition & 10 deletions src/Models/Absence.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
use CTApi\Models\Traits\ExtractData;
use CTApi\Models\Traits\FillWithData;

class Absence implements UpdatableModel
class Absence extends AbstractModel implements UpdatableModel
{
use FillWithData, ExtractData;

protected ?string $id = null;
protected ?string $comment = null;
protected ?string $absenceReasonId = null;
protected ?AbsenceReason $absenceReason = null;
Expand Down Expand Up @@ -48,14 +47,6 @@ static function getModifiableAttributes(): array
];
}

/**
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}

/**
* @param string|null $id
* @return Absence
Expand Down
11 changes: 1 addition & 10 deletions src/Models/AbsenceReason.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,14 @@

use CTApi\Models\Traits\FillWithData;

class AbsenceReason
class AbsenceReason extends AbstractModel
{
use FillWithData;

protected ?string $id = null;
protected ?string $name = null;
protected ?string $nameTranslated = null;
protected ?string $sortKey = null;

/**
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}

/**
* @param string|null $id
* @return AbsenceReason
Expand Down
60 changes: 60 additions & 0 deletions src/Models/AbstractModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php


namespace CTApi\Models;


use CTApi\Exceptions\CTModelException;

/**
* Class AbstractModel
* @package CTApi\Models
*/
abstract class AbstractModel
{
protected ?string $id = null;

/**
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}

/**
* Fluent setter have to be implemented by child-class. Returns instance of model.
* @param string|null $id
* @return mixed
*/
abstract public function setId(?string $id);

/**
* Throws exception if id is null.
* @return string
*/
public function getIdOrFail(): string
{
if (is_null($this->id)) {
throw new CTModelException("Id of " . get_class($this) . " is null.");
}
return $this->id;
}

/**
* Throws exception if id cannot be casted to int.
* @return int
*/
public function getIdAsInteger(): int
{
$id = $this->getIdOrFail();

$castedInteger = intval($id);
$backCastedInteger = strval($castedInteger);
if ($backCastedInteger == $id) {
return $castedInteger;
} else {
throw new CTModelException("Could not cast id (" . $id . ") to integer in " . get_class($this) . ".");
}
}
}
11 changes: 1 addition & 10 deletions src/Models/Appointment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

use CTApi\Models\Traits\FillWithData;

class Appointment
class Appointment extends AbstractModel
{
use FillWithData;

protected ?string $id = null;
protected ?string $caption = null;
protected ?string $note = null;
protected ?string $version = null;
Expand Down Expand Up @@ -47,14 +46,6 @@ protected function fillArrayType(string $key, array $data): void
}
}

/**
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}

/**
* @param string|null $id
* @return Appointment
Expand Down
11 changes: 1 addition & 10 deletions src/Models/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
use CTApi\Models\Traits\MetaAttribute;
use CTApi\Requests\AppointmentRequestBuilder;

class Calendar
class Calendar extends AbstractModel
{
use FillWithData, MetaAttribute;

protected ?string $id = null;
protected ?string $name = null;
protected ?string $nameTranslated = null;
protected ?string $sortKey = null;
Expand Down Expand Up @@ -43,14 +42,6 @@ public function requestAppointments(): ?AppointmentRequestBuilder
}
}

/**
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}

/**
* @param string|null $id
* @return Calendar
Expand Down
15 changes: 3 additions & 12 deletions src/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
use CTApi\Requests\FileRequest;
use CTApi\Requests\FileRequestBuilder;

class Event
class Event extends AbstractModel
{
use FillWithData;

protected ?string $id = null;
protected ?string $guid = null;
protected ?string $name = null;
protected ?string $description = null;
Expand Down Expand Up @@ -45,13 +44,13 @@ protected function fillArrayType(string $key, array $data): void

public function requestAgenda(): EventAgenda
{
return (new EventAgendaRequestBuilder((int)$this->getId()))->get();
return (new EventAgendaRequestBuilder($this->getIdAsInteger()))->get();
}

public function requestFiles(): ?FileRequestBuilder
{
if (!is_null($this->getId())) {
return FileRequest::forEvent((int)$this->getId());
return FileRequest::forEvent($this->getIdAsInteger());
}
return null;
}
Expand All @@ -67,14 +66,6 @@ public function requestEventServiceWithServiceId(int $serviceId): ?EventService
return null;
}

/**
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}

/**
* @param string|null $id
* @return Event
Expand Down
Loading