Skip to content

Commit

Permalink
Merge pull request #284 from analogueorm/5.6
Browse files Browse the repository at this point in the history
Sync master
  • Loading branch information
adrorocker authored Feb 10, 2020
2 parents dcc1a1c + 4c6cf3c commit 8b47ef8
Show file tree
Hide file tree
Showing 29 changed files with 301 additions and 111 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ matrix:
before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction
- travis_retry composer update --no-interaction --prefer-source
- if [ "$LARAVEL_VERSION" != "" ]; then composer require --dev "laravel/laravel:${LARAVEL_VERSION}" --no-update; fi;
- composer update

script:
- vendor/bin/phpunit
Expand Down
6 changes: 5 additions & 1 deletion src/Commands/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ protected function postStoreProcess()

// Now we can sync the related collections
//
// TODO (note) : not sute this check is needed, as we can assume
// TODO (note) : not sure this check is needed, as we can assume
// the aggregate exists in the Post Store Process
if ($this->aggregate->exists()) {
$this->aggregate->syncRelationships($foreignRelationships);
Expand Down Expand Up @@ -186,6 +186,10 @@ protected function insert()
unset($attributes[$keyName]);
}

if (isset($attributes['attributes'])) {
unset($attributes['attributes']);
}

$id = $this->query->insertGetId($attributes, $keyName);

$aggregate->setEntityAttribute($keyName, $id);
Expand Down
11 changes: 5 additions & 6 deletions src/EntityMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public function getProperties(): array
*
* @return array
*/
public function getEvents() : array
public function getEvents(): array
{
return $this->events;
}
Expand All @@ -510,7 +510,7 @@ public function getEvents() : array
*
* @return string
*/
public function getAttributesPropertyName() : string
public function getAttributesPropertyName(): string
{
}

Expand Down Expand Up @@ -1572,10 +1572,7 @@ public function getMorphClass(): string
*/
public function newCollection(array $entities = []): EntityCollection
{
$collection = new EntityCollection($entities);
$keyName = $this->getAttributeNameForColumn($this->getKeyName());

return $collection->keyBy($keyName);
return new EntityCollection($entities);
}

/**
Expand Down Expand Up @@ -1745,6 +1742,7 @@ public function getAttributeNamesFromColumns($array)
return $newArray;
}
if ($this->camelCaseHydratation) {
$newArray = [];
foreach ($array as $key => $value) {
$attributeName = camel_case($key);
$newArray[$attributeName] = $value;
Expand Down Expand Up @@ -1796,6 +1794,7 @@ public function getColumnNamesFromAttributes($array)
return $newArray;
}
if ($this->camelCaseHydratation) {
$newArray = [];
foreach ($array as $key => $value) {
$attributeName = snake_case($key);
$newArray[$attributeName] = $value;
Expand Down
4 changes: 2 additions & 2 deletions src/Plugins/SoftDeletes/SoftDeletesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected function registerSoftDelete(Mapper $mapper)
*
* @return InternallyMappable
*/
protected function getMappable($entity) : InternallyMappable
protected function getMappable($entity): InternallyMappable
{
if ($entity instanceof InternallyMappable) {
return $entity;
Expand All @@ -106,7 +106,7 @@ protected function getMappable($entity) : InternallyMappable
/**
* {@inheritdoc}
*/
public function getCustomEvents(): array
public function getCustomEvents(): array
{
return [
'restoring' => Events\Restoring::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Timestamps/TimestampsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function register()
*
* @return InternallyMappable
*/
protected function getMappable($entity) : InternallyMappable
protected function getMappable($entity): InternallyMappable
{
if ($entity instanceof InternallyMappable) {
return $entity;
Expand Down
30 changes: 17 additions & 13 deletions src/Relationships/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public function firstOrFail($columns = ['*'])
*
* @return \Illuminate\Support\Collection
*/
public function get($columns = ['*']) : Collection
public function get($columns = ['*']): Collection
{
// First we'll add the proper select columns onto the query so it is run with
// the proper columns. Then, we will get the results and hydrate out pivot
Expand All @@ -205,7 +205,7 @@ public function get($columns = ['*']) : Collection

$select = $this->getSelectColumns($columns);

$entities = $this->query->addSelect($select)->get()->all();
$entities = $this->query->addSelect($select)->disableCache()->get()->all();

$entities = $this->hydratePivotRelation($entities);

Expand All @@ -217,28 +217,29 @@ public function get($columns = ['*']) : Collection
*
* @param array $entities
*
* @return void
* @return array
*/
protected function hydratePivotRelation(array $entities)
{
// TODO (note) We should definitely get rid of the pivot in a next
// release, as this is not quite relevant in a datamapper context.
$host = $this;

return array_map(function ($entity) use ($host) {
return array_map(function ($entity) {
$entityWrapper = $this->factory->make($entity);

$pivot = $this->newExistingPivot($this->cleanPivotAttributes($entityWrapper));
$pivotAttributes = $this->cleanPivotAttributes($entityWrapper);
$pivot = $this->newExistingPivot($pivotAttributes);
$entityWrapper->setEntityAttribute('pivot', $pivot);

return $entityWrapper->getObject();
$object = $entityWrapper->unwrap();

return $object;
}, $entities);
}

/**
* Get the pivot attributes from a model.
*
* @param $entity
* @param InternallyMappable $entity
*
* @return array
*/
Expand Down Expand Up @@ -442,7 +443,9 @@ public function match(array $results, $relation)
// children back to their parent using the dictionary and the keys on the
// the parent models. Then we will return the hydrated models back out.
return array_map(function ($result) use ($dictionary, $keyName, $cache, $relation, $host) {
if (isset($dictionary[$key = $result[$keyName]])) {
$key = $result[$keyName];

if (isset($dictionary[$key])) {
$collection = $host->relatedMap->newCollection($dictionary[$key]);

$result[$relation] = $collection;
Expand Down Expand Up @@ -475,7 +478,10 @@ protected function buildDictionary(EntityCollection $results)

foreach ($results as $entity) {
$wrapper = $this->factory->make($entity);
$dictionary[$wrapper->getEntityAttribute('pivot')->$foreign][] = $entity;

$foreignKey = $wrapper->getEntityAttribute('pivot')->$foreign;

$dictionary[$foreignKey][] = $entity;
}

return $dictionary;
Expand Down Expand Up @@ -614,8 +620,6 @@ protected function detachExcept(array $entities = [])
$query->where($this->foreignKey, '=', $this->parent->getEntityAttribute($parentKey));

$query->delete();

$query = $this->newPivotQuery();
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Relationships/EmbeddedRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function setPrefix(string $prefix)
*
* @return string
*/
public function getPrefix() : string
public function getPrefix(): string
{
return $this->prefix;
}
Expand All @@ -149,7 +149,7 @@ public function getPrefix() : string
*
* @return array
*/
protected function getEmbeddedObjectAttributes() : array
protected function getEmbeddedObjectAttributes(): array
{
$entityMap = $this->getRelatedMapper()->getEntityMap();

Expand All @@ -166,7 +166,7 @@ protected function getEmbeddedObjectAttributes() : array
*
* @return string
*/
protected function getParentAttributeKey($key) : string
protected function getParentAttributeKey($key): string
{
return $this->getPrefixedAttributeKey($this->getMappedParentAttribute($key));
}
Expand All @@ -179,7 +179,7 @@ protected function getParentAttributeKey($key) : string
*
* @return string
*/
protected function getMappedParentAttribute(string $key) : string
protected function getMappedParentAttribute(string $key): string
{
if (array_key_exists($key, $this->columnMap)) {
return $this->columnMap[$key];
Expand All @@ -195,7 +195,7 @@ protected function getMappedParentAttribute(string $key) : string
*
* @return string
*/
protected function getPrefixedAttributeKey(string $attributeKey) : string
protected function getPrefixedAttributeKey(string $attributeKey): string
{
return $this->prefix.$attributeKey;
}
Expand All @@ -206,7 +206,7 @@ protected function getPrefixedAttributeKey(string $attributeKey) : string
*
* @return array
*/
abstract public function match(array $results) : array;
abstract public function match(array $results): array;

/**
* Build an embedded object instance.
Expand All @@ -217,7 +217,7 @@ abstract public function match(array $results) : array;
*/
protected function buildEmbeddedObject(array $attributes)
{
$resultBuilder = new ResultBuilder($this->getRelatedMapper());
$resultBuilder = new ResultBuilder($this->getRelatedMapper(), true);

// TODO : find a way to support eager load within an embedded
// object.
Expand All @@ -233,7 +233,7 @@ protected function buildEmbeddedObject(array $attributes)
*
* @return array $columns
*/
abstract public function normalize($object) : array;
abstract public function normalize($object): array;

/**
* Return parent mapper.
Expand Down
8 changes: 4 additions & 4 deletions src/Relationships/EmbedsMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class EmbedsMany extends EmbedsOne
*
* @return array
*/
public function matchSingleResult(array $attributes) : array
public function matchSingleResult(array $attributes): array
{
$column = $this->relation;

Expand All @@ -38,7 +38,7 @@ public function matchSingleResult(array $attributes) : array
*
* @return array
*/
protected function matchAsArray(array $attributes) : array
protected function matchAsArray(array $attributes): array
{
// Extract the attributes with the key of the relation,
// which should be an array.
Expand Down Expand Up @@ -84,7 +84,7 @@ protected function buildEmbeddedCollection(array $rows): Collection
*
* @return array $columns
*/
public function normalize($objects) : array
public function normalize($objects): array
{
if (!$this->asArray) {
throw new MappingException('Cannot normalize an embedsMany relation as row columns');
Expand All @@ -102,7 +102,7 @@ public function normalize($objects) : array
*
* @return array
*/
protected function normalizeAsArray($objects) : array
protected function normalizeAsArray($objects): array
{
$key = $this->relation;

Expand Down
18 changes: 9 additions & 9 deletions src/Relationships/EmbedsOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class EmbedsOne extends EmbeddedRelationship
*
* @return array
*/
public function match(array $results) : array
public function match(array $results): array
{
return array_map([$this, 'matchSingleResult'], $results);
}
Expand All @@ -34,7 +34,7 @@ public function match(array $results) : array
*
* @return array
*/
public function matchSingleResult(array $attributes) : array
public function matchSingleResult(array $attributes): array
{
return $this->asArray ? $this->matchAsArray($attributes) : $this->matchAsAttributes($attributes);
}
Expand All @@ -49,7 +49,7 @@ public function matchSingleResult(array $attributes) : array
*
* @return array
*/
protected function matchAsArray(array $attributes) : array
protected function matchAsArray(array $attributes): array
{
// Extract the attributes with the key of the relation,
// which should be an array.
Expand All @@ -76,7 +76,7 @@ protected function matchAsArray(array $attributes) : array
*
* @return array
*/
protected function matchAsAttributes(array $attributes) : array
protected function matchAsAttributes(array $attributes): array
{
$attributesMap = $this->getAttributesDictionnary();

Expand Down Expand Up @@ -108,7 +108,7 @@ protected function matchAsAttributes(array $attributes) : array
*
* @return array
*/
protected function getAttributesDictionnary() : array
protected function getAttributesDictionnary(): array
{
// Get attributes that belongs to the embedded object
$embeddedAttributeKeys = $this->getEmbeddedObjectAttributes();
Expand All @@ -130,7 +130,7 @@ protected function getAttributesDictionnary() : array
*
* @return array $columns
*/
public function normalize($object) : array
public function normalize($object): array
{
return $this->asArray ? $this->normalizeAsArray($object) : $this->normalizeAsAttributes($object);
}
Expand All @@ -142,7 +142,7 @@ public function normalize($object) : array
*
* @return array
*/
protected function normalizeAsArray($object) : array
protected function normalizeAsArray($object): array
{
$wrapper = $this->factory->make($object);

Expand All @@ -162,7 +162,7 @@ protected function normalizeAsArray($object) : array
*
* @return array
*/
protected function normalizeAsAttributes($object) : array
protected function normalizeAsAttributes($object): array
{
if (is_null($object)) {
return $this->nullObjectAttributes();
Expand All @@ -186,7 +186,7 @@ protected function normalizeAsAttributes($object) : array
*
* @return array
*/
protected function nullObjectAttributes() : array
protected function nullObjectAttributes(): array
{
$attributesMap = $this->getAttributesDictionnary();

Expand Down
3 changes: 2 additions & 1 deletion src/Relationships/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Analogue\ORM\System\Mapper;
use Analogue\ORM\System\Query;
use Illuminate\Database\Query\Expression;
use Illuminate\Support\Collection;

class HasManyThrough extends Relationship
{
Expand Down Expand Up @@ -217,7 +218,7 @@ public function getResults($relation)
*
* @return EntityCollection
*/
public function get($columns = ['*'])
public function get($columns = ['*']): Collection
{
// First we'll add the proper select columns onto the query so it is run with
// the proper columns. Then, we will get the results and hydrate out pivot
Expand Down
Loading

0 comments on commit 8b47ef8

Please sign in to comment.