|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Swis\JsonApi\Client\Concerns; |
| 4 | + |
| 5 | +use Illuminate\Support\Str; |
| 6 | +use Swis\JsonApi\Client\Collection; |
| 7 | +use Swis\JsonApi\Client\Interfaces\DataInterface; |
| 8 | +use Swis\JsonApi\Client\Interfaces\ManyRelationInterface; |
| 9 | +use Swis\JsonApi\Client\Interfaces\OneRelationInterface; |
| 10 | +use Swis\JsonApi\Client\Links; |
| 11 | +use Swis\JsonApi\Client\Meta; |
| 12 | +use Swis\JsonApi\Client\Relations\HasManyRelation; |
| 13 | +use Swis\JsonApi\Client\Relations\HasOneRelation; |
| 14 | +use Swis\JsonApi\Client\Relations\MorphToManyRelation; |
| 15 | +use Swis\JsonApi\Client\Relations\MorphToRelation; |
| 16 | + |
| 17 | +trait HasRelations |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface[]|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface[] |
| 21 | + */ |
| 22 | + protected $relations = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a singular relation to another item. |
| 26 | + * |
| 27 | + * @param string $itemClass |
| 28 | + * @param string|null $name |
| 29 | + * |
| 30 | + * @return \Swis\JsonApi\Client\Relations\HasOneRelation |
| 31 | + */ |
| 32 | + public function hasOne(string $itemClass, string $name = null): OneRelationInterface |
| 33 | + { |
| 34 | + $name = $name ?: Str::snake(debug_backtrace()[1]['function']); |
| 35 | + |
| 36 | + if (!array_key_exists($name, $this->relations)) { |
| 37 | + $this->relations[$name] = new HasOneRelation((new $itemClass())->getType()); |
| 38 | + } |
| 39 | + |
| 40 | + return $this->relations[$name]; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Create a plural relation to another item. |
| 45 | + * |
| 46 | + * @param string $itemClass |
| 47 | + * @param string|null $name |
| 48 | + * |
| 49 | + * @return \Swis\JsonApi\Client\Relations\HasManyRelation |
| 50 | + */ |
| 51 | + public function hasMany(string $itemClass, string $name = null): ManyRelationInterface |
| 52 | + { |
| 53 | + $name = $name ?: Str::snake(debug_backtrace()[1]['function']); |
| 54 | + |
| 55 | + if (!array_key_exists($name, $this->relations)) { |
| 56 | + $this->relations[$name] = new HasManyRelation((new $itemClass())->getType()); |
| 57 | + } |
| 58 | + |
| 59 | + return $this->relations[$name]; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Create a singular relation. |
| 64 | + * |
| 65 | + * @param string|null $name |
| 66 | + * |
| 67 | + * @return \Swis\JsonApi\Client\Relations\MorphToRelation |
| 68 | + */ |
| 69 | + public function morphTo(string $name = null): OneRelationInterface |
| 70 | + { |
| 71 | + $name = $name ?: Str::snake(debug_backtrace()[1]['function']); |
| 72 | + |
| 73 | + if (!array_key_exists($name, $this->relations)) { |
| 74 | + $this->relations[$name] = new MorphToRelation(); |
| 75 | + } |
| 76 | + |
| 77 | + return $this->relations[$name]; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Create a plural relation. |
| 82 | + * |
| 83 | + * @param string|null $name |
| 84 | + * |
| 85 | + * @return \Swis\JsonApi\Client\Relations\MorphToManyRelation |
| 86 | + */ |
| 87 | + public function morphToMany(string $name = null): ManyRelationInterface |
| 88 | + { |
| 89 | + $name = $name ?: Str::snake(debug_backtrace()[1]['function']); |
| 90 | + |
| 91 | + if (!array_key_exists($name, $this->relations)) { |
| 92 | + $this->relations[$name] = new MorphToManyRelation(); |
| 93 | + } |
| 94 | + |
| 95 | + return $this->relations[$name]; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @return array |
| 100 | + */ |
| 101 | + public function getRelations(): array |
| 102 | + { |
| 103 | + return $this->relations; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @param string $name |
| 108 | + * |
| 109 | + * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface|null |
| 110 | + */ |
| 111 | + public function getRelation(string $name) |
| 112 | + { |
| 113 | + return $this->relations[$name] ?? null; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get the relationship data (included). |
| 118 | + * |
| 119 | + * @param string $name |
| 120 | + * |
| 121 | + * @return \Swis\JsonApi\Client\Interfaces\DataInterface|null |
| 122 | + */ |
| 123 | + public function getRelationValue(string $name): ?DataInterface |
| 124 | + { |
| 125 | + // If the "attribute" exists as a method on the model, we will just assume |
| 126 | + // it is a relationship and will load and return the included items in the relationship |
| 127 | + $method = Str::camel($name); |
| 128 | + if (method_exists($this, $method)) { |
| 129 | + return $this->$method()->getIncluded(); |
| 130 | + } |
| 131 | + |
| 132 | + // If the "attribute" exists as a relationship on the model, we will return |
| 133 | + // the included items in the relationship |
| 134 | + if ($this->hasRelation($name)) { |
| 135 | + return $this->getRelation($name)->getIncluded(); |
| 136 | + } |
| 137 | + |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Set the specific relationship on the model. |
| 143 | + * |
| 144 | + * @param string $name |
| 145 | + * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data |
| 146 | + * @param \Swis\JsonApi\Client\Links|null $links |
| 147 | + * @param \Swis\JsonApi\Client\Meta|null $meta |
| 148 | + * |
| 149 | + * @return static |
| 150 | + */ |
| 151 | + public function setRelation(string $name, DataInterface $data, Links $links = null, Meta $meta = null) |
| 152 | + { |
| 153 | + $method = Str::camel($name); |
| 154 | + if (method_exists($this, $method)) { |
| 155 | + /** @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationObject */ |
| 156 | + $relationObject = $this->$method(); |
| 157 | + } elseif ($data instanceof Collection) { |
| 158 | + $relationObject = $this->morphToMany($name); |
| 159 | + } else { |
| 160 | + $relationObject = $this->morphTo($name); |
| 161 | + } |
| 162 | + |
| 163 | + $relationObject->associate($data); |
| 164 | + $relationObject->setLinks($links); |
| 165 | + $relationObject->setMeta($meta); |
| 166 | + |
| 167 | + return $this; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @param string $name |
| 172 | + * |
| 173 | + * @return bool |
| 174 | + */ |
| 175 | + public function hasRelation(string $name): bool |
| 176 | + { |
| 177 | + return array_key_exists($name, $this->relations); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * @param $name |
| 182 | + * |
| 183 | + * @return static |
| 184 | + */ |
| 185 | + public function unsetRelation(string $name) |
| 186 | + { |
| 187 | + unset($this->relations[$name]); |
| 188 | + |
| 189 | + return $this; |
| 190 | + } |
| 191 | +} |
0 commit comments