Skip to content

Commit

Permalink
Merge pull request #18 from sander3/portable-recursion-feature
Browse files Browse the repository at this point in the history
Resolves #16
  • Loading branch information
sander3 authored Jul 29, 2018
2 parents 9e71bf4 + f101fe8 commit c98e961
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ After installing the package, you should publish the configuration file:
$ php artisan vendor:publish --tag=gdpr-config
```

Finally, add the `Soved\Laravel\Gdpr\Portable` trait to the `App\User` model:
Finally, add the `Soved\Laravel\Gdpr\Portable` trait to the `App\User` model and implement the `Soved\Laravel\Gdpr\Contracts\Portable` contract:

```php
<?php
Expand All @@ -36,8 +36,9 @@ namespace App;
use Soved\Laravel\Gdpr\Portable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Soved\Laravel\Gdpr\Contracts\Portable as PortableContract;

class User extends Authenticatable
class User extends Authenticatable implements PortableContract
{
use Portable, Notifiable;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Contracts/Portable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Soved\Laravel\Gdpr\Contracts;

interface Portable
{
/**
* Convert the model instance to a GDPR compliant data portability array.
*
* @return array
*/
public function portable();

/**
* Get the GDPR compliant data portability array for the model.
*
* @return array
*/
public function toPortableArray();
}
54 changes: 53 additions & 1 deletion src/Portable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Soved\Laravel\Gdpr;

use Soved\Laravel\Gdpr\Contracts\Portable as PortableContract;

trait Portable
{
/**
Expand All @@ -13,7 +15,7 @@ public function portable()
{
// Eager load the given relations
if (isset($this->gdprWith)) {
$this->loadMissing($this->gdprWith);
$this->loadRelations($this->gdprWith);
}

// Make the given attributes visible
Expand All @@ -29,6 +31,56 @@ public function portable()
return $this->toPortableArray();
}

/**
* Eager load the given relations.
*
* @param array $relations
* @return void
*/
public function loadRelations(array $relations)
{
$portableRelations = $this->getPortableRelations($relations);

array_walk($portableRelations, [$this, 'loadPortableRelation']);

$this->load(array_diff($relations, $portableRelations));
}

/**
* Get all portable relations.
*
* @param array $relations
* @return array
*/
private function getPortableRelations(array $relations)
{
$portableRelations = [];

foreach ($relations as $relation) {
if ($this->$relation()->getRelated() instanceof PortableContract) {
$portableRelations[] = $relation;
}
}

return $portableRelations;
}

/**
* Load and transform a portable relation.
*
* @param string $relation
* @return void
*/
private function loadPortableRelation(string $relation)
{
$this->attributes[$relation] = $this
->$relation()
->get()
->transform(function ($item) {
return $item->portable();
});
}

/**
* Get the GDPR compliant data portability array for the model.
*
Expand Down

0 comments on commit c98e961

Please sign in to comment.