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

feat(to-data): add toData-method on FillWithData-Trait #103

Merged
merged 2 commits into from
Sep 2, 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 @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [Added method to delete person records](https://github.com/5pm-HDH/churchtools-api/pull/91)
- [Added Calendar-Request](https://github.com/5pm-HDH/churchtools-api/pull/92)
- [Added Permission-Request](https://github.com/5pm-HDH/churchtools-api/pull/102)
- [Serialize Models to Data-Array for JSON-Export](https://github.com/5pm-HDH/churchtools-api/pull/103)

### Changed
- [Refactor CTClient:](https://github.com/5pm-HDH/churchtools-api/pull/83) transform inheritance from GuzzleClient to composition-relation
Expand Down
19 changes: 19 additions & 0 deletions docs/out/Models.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ Create a collection of models filled with data:

```

**Convert Model to data**

Convert a model with the `toData`-method (FillWithData-Trait):

```php
use CTApi\Models\Person;

$data = $this->person->toData();

var_dump( $data["firstName"]);
// Output: "John"

var_dump( $data["meta"]["createdPerson"]["firstName"]);
// Output: "Simon"


```


**`get` and `set`-methods**

The attributes of a model can be used accessed with getters and setter.
Expand Down
7 changes: 7 additions & 0 deletions docs/src/ressources/Models.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ Create a collection of models filled with data:

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

**Convert Model to data**

Convert a model with the `toData`-method (FillWithData-Trait):

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


**`get` and `set`-methods**

The attributes of a model can be used accessed with getters and setter.
Expand Down
43 changes: 43 additions & 0 deletions src/Models/Traits/FillWithData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,49 @@

trait FillWithData
{
/**
* Serialize the Model-Data to associative array. Also converts all properties that are objects to associative array.
* @return array data
*/
public function toData(): array
{
$data = get_object_vars($this);
$castedData = [];
foreach ($data as $propertyKey => $propertyValue) {
$castedData[$propertyKey] = $this->castPropertyToData($propertyValue);
}
return $castedData;
}

private function castPropertyToData($propertyValue)
{
if (is_object($propertyValue)) { // cast objects
return $this->castObjectToData($propertyValue);
} else if (is_array($propertyValue)) {
return $this->castArrayToData($propertyValue);
} else {
return $propertyValue;
}
}

private function castArrayToData(array $array): array
{
return array_map(function ($entry) {
return $this->castPropertyToData($entry);
}, $array);
}

private function castObjectToData(object $object): ?array
{
$reflectionClass = new \ReflectionClass($object);
$usedTraits = $reflectionClass->getTraitNames();

if (in_array(FillWithData::class, $usedTraits)) {
return $object->toData();
}
return null;
}

private function fillWithData(array $array): void
{
foreach ($array as $key => $value) {
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/Docs/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@

class ModelTest extends TestCase
{
private $person;

protected function setUp(): void
{
parent::setUp();
$this->person = Person::createModelFromData([
"id" => 21,
"firstName" => "John",
"lastName" => "Doe",
"meta" => [
"createdPerson" => [
"firstName" => "Simon"
]
]
]);
}

/**
* @doesNotPerformAssertions
*/
Expand Down Expand Up @@ -40,6 +57,14 @@ public function testCreateModelsFromArray()
$this->assertEquals("Kling/ Maier/ ", $lastNames);
}

public function testConvertModelToData()
{
$data = $this->person->toData();

$this->assertEquals("John", $data["firstName"]);
$this->assertEquals("Simon", $data["meta"]["createdPerson"]["firstName"]);
}

/**
* @doesNotPerformAssertions
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/Models/FillWithDataTraitToData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php


namespace Tests\Unit\Models;


use CTApi\Models\Traits\FillWithData;
use CTApi\Requests\SongRequest;
use PHPUnit\Framework\TestCase;
use Tests\Unit\TestCaseHttpMocked;

class FillWithDataTraitToData extends TestCaseHttpMocked
{
public function testCreateSong()
{
$song = SongRequest::find(21);

$this->assertNotNull($song);

$data = $song->toData();

$this->assertNotEmpty($data);
$this->assertEquals("21", $data["id"]);
$this->assertEquals("We welcome you", $data["name"]);

// Check that Model-Properties are also casted to array
$this->assertNotEmpty($data["arrangements"]);
$lastArrangement = end($data["arrangements"]);
$this->assertIsArray($lastArrangement);
$this->assertEquals("221", $lastArrangement["id"]);
}
}