Skip to content

Commit

Permalink
Merge pull request #38 from compwright/to-array
Browse files Browse the repository at this point in the history
Add GenericCollection::toArray()
  • Loading branch information
harikt authored Aug 3, 2022
2 parents e77f73a + 8be3579 commit 4b0564f
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 2 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@
"psr-4": {
"Aura\\Marshal\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
8 changes: 8 additions & 0 deletions docs/advanced-usage.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Advanced Usage

## Extracting Marshaled Collections To Array

Say you want to get your data back out, relations and all, but in array format. You can call toArray() on any collection:

```php
$posts = $manager->posts->toArray();
```

## Entity and Collection Builders

We have a good amount of control over how the type objects create entities and
Expand Down
4 changes: 3 additions & 1 deletion src/Collection/GenericCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Aura\Marshal\Collection;

use Aura\Marshal\Data;
use Aura\Marshal\Type\GenericType;
use Aura\Marshal\ToArrayTrait;

/**
*
Expand All @@ -22,6 +22,8 @@
*/
class GenericCollection extends Data
{
use ToArrayTrait;

/**
*
* Returns an array of all values for a single field in the collection.
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/GenericEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

use Aura\Marshal\Data;
use Aura\Marshal\Lazy\LazyInterface;
use Aura\Marshal\ToArrayTrait;

/**
*
Expand All @@ -23,6 +24,7 @@
class GenericEntity extends Data
{
use MagicArrayAccessTrait;
use ToArrayTrait;

/**
*
Expand Down
8 changes: 8 additions & 0 deletions src/ToArrayInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Aura\Marshal;

interface ToArrayInterface
{
public function toArray(): array;
}
27 changes: 27 additions & 0 deletions src/ToArrayTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Aura\Marshal;

use Aura\Marshal\Lazy\GenericLazy;

trait ToArrayTrait
{
public function toArray(): array
{
return array_map(
function ($entity)
{
if ($entity instanceof GenericLazy) {
$entity = $entity->get($this);
}

if ($entity instanceof ToArrayInterface) {
return $entity->toArray();
}

return $entity;
},
$this->data
);
}
}
9 changes: 8 additions & 1 deletion tests/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use Aura\Marshal\Type\GenericType;
use Aura\Marshal\Entity\Builder as EntityBuilder;
use Aura\Marshal\Collection\Builder as CollectionBuilder;
use Aura\Marshal\Lazy\Builder as LazyBuilder;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

/**
Expand All @@ -13,6 +12,8 @@
*/
class CollectionTest extends TestCase
{
protected $data;

protected $type;

protected $collection;
Expand All @@ -35,6 +36,7 @@ protected function set_up()
];
}

$this->data = $data;
$this->collection = new GenericCollection($data);
$this->empty_collection = new GenericCollection([]);
}
Expand Down Expand Up @@ -95,4 +97,9 @@ public function testObjectsInCollectionAreInIdentityMap()
$actual = $collection[0];
$this->assertSame($expect->name, $actual->name);
}

public function testToArray()
{
$this->assertEquals($this->data, $this->collection->toArray());
}
}
14 changes: 14 additions & 0 deletions tests/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,18 @@ public function testMagicPropertyAccess()
$expect = (object) ['foreign_field' => 'foreign_value'];
$this->assertEquals($expect, $actual);
}

public function testToArray()
{
$entity = $this->newGenericEntity();
$expect = [
'foo' => 'bar',
'baz' => 'dim',
'zim' => 'gir',
'related' => (object) [
'foreign_field' => 'foreign_value',
],
];
$this->assertEquals($expect, $entity->toArray());
}
}

0 comments on commit 4b0564f

Please sign in to comment.