Skip to content

Commit

Permalink
Refactor relations
Browse files Browse the repository at this point in the history
  • Loading branch information
JaZo committed Jan 21, 2019
1 parent 5270798 commit a7d1b48
Show file tree
Hide file tree
Showing 16 changed files with 208 additions and 279 deletions.
18 changes: 17 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

* Nothing
This release includes changes to some interfaces. This is a breaking change if you use these interfaces in your own code.

### Added

* Added `OneRelationInterface` and `ManyRelationInterface` to differentiate between singular and plural relations.

### Changed

* Moved `setType` and `getType` from `RelationInterface` to a separate interface; `TypedRelationInterface`.
* Added type hints to `ItemInterface::setRelation`.
* Added return type hints to `hasAttribute`, `hasOne`, `hasMany`, `morphTo` and `morphToMany` methods of `Item`.

### Removed

* Removed `RelationInterface` in favor of `OneRelationInterface` and `ManyRelationInterface`.
* Removed `setId` and `getId` from `HasOneRelation` and `MorphToRelation`. These operations should be performed on the included item.
* Removed `setType` and `getType` from morph relations. Use regular relations if you want to set the type.

## [0.13.0] - 2019-01-14

Expand Down
8 changes: 3 additions & 5 deletions src/Interfaces/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,16 @@ public function getAvailableRelations(): array;
/**
* Set the specific relationship in the model.
*
* @param string $relation
* @param mixed $value
* @param string $relation
* @param DataInterface $value
*
* @return static
*/
public function setRelation($relation, $value);
public function setRelation(string $relation, DataInterface $value);

/**
* @TODO: MEGA TODO. Set up a serializer for the Item so that we can remove this, getRelationships etc
*
* @throws \Exception
*
* @return \Swis\JsonApi\Client\Collection
*/
public function getIncluded();
Expand Down
42 changes: 42 additions & 0 deletions src/Interfaces/ManyRelationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Swis\JsonApi\Client\Interfaces;

use Swis\JsonApi\Client\Collection;

interface ManyRelationInterface
{
/**
* @param \Swis\JsonApi\Client\Collection $included
*
* @return static
*/
public function associate(Collection $included);

/**
* @return static
*/
public function dissociate();

/**
* @return bool
*/
public function hasIncluded(): bool;

/**
* @return \Swis\JsonApi\Client\Collection
*/
public function getIncluded(): Collection;

/**
* @param bool $omitIncluded
*
* @return static
*/
public function setOmitIncluded(bool $omitIncluded);

/**
* @return bool
*/
public function shouldOmitIncluded(): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,39 @@

namespace Swis\JsonApi\Client\Interfaces;

interface RelationInterface
interface OneRelationInterface
{
/**
* @return string
*/
public function getType(): string;

/**
* @param string $type
*
* @return static
*/
public function setType(string $type);

/**
* @param \Swis\JsonApi\Client\Interfaces\DataInterface $included
* @param \Swis\JsonApi\Client\Interfaces\ItemInterface $included
*
* @return static
*/
public function associate(DataInterface $included);
public function associate(ItemInterface $included);

/**
* @return static
*/
public function dissociate();

/**
* @return \Swis\JsonApi\Client\Collection|\Swis\JsonApi\Client\Interfaces\DataInterface|\Swis\JsonApi\Client\Interfaces\ItemInterface|null
*/
public function getIncluded();

/**
* @return bool
*/
public function hasIncluded(): bool;

/**
* @return bool
* @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null
*/
public function shouldOmitIncluded(): bool;
public function getIncluded();

/**
* @param bool $omitIncluded
*
* @return static
*/
public function setOmitIncluded(bool $omitIncluded);

/**
* @return bool
*/
public function shouldOmitIncluded(): bool;
}
18 changes: 18 additions & 0 deletions src/Interfaces/TypedRelationInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Swis\JsonApi\Client\Interfaces;

interface TypedRelationInterface
{
/**
* @return string
*/
public function getType(): string;

/**
* @param string $type
*
* @return static
*/
public function setType(string $type);
}
Loading

0 comments on commit a7d1b48

Please sign in to comment.