VGirol /
JsonApi-Faker
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace VGirol\JsonApiFaker\Factory; |
||
| 6 | |||
| 7 | use VGirol\JsonApiConstant\Members; |
||
| 8 | use VGirol\JsonApiFaker\Contract\RelationshipContract; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Add "relationships" member to a factory. |
||
| 12 | */ |
||
| 13 | trait HasRelationships |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The "relationships" member |
||
| 17 | * |
||
| 18 | * @var array An array of RelationshipFactory |
||
| 19 | */ |
||
| 20 | protected $relationships; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get the relationships collection |
||
| 24 | * |
||
| 25 | * @return array|null |
||
| 26 | */ |
||
| 27 | 9 | public function getRelationships(): ?array |
|
| 28 | { |
||
| 29 | 9 | return $this->relationships; |
|
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Add a single relationship |
||
| 34 | * |
||
| 35 | * @param string $name |
||
| 36 | * @param RelationshipContract $relationship |
||
| 37 | * |
||
| 38 | * @return static |
||
| 39 | */ |
||
| 40 | 27 | public function addRelationship(string $name, $relationship) |
|
| 41 | { |
||
| 42 | 27 | $this->addToObject(Members::RELATIONSHIPS, $name, $relationship); |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 43 | |||
| 44 | 27 | return $this; |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Fill the "relationships" member with fake values. |
||
| 49 | * |
||
| 50 | * @param integer $count The number of relationships to generate. |
||
| 51 | * |
||
| 52 | * @return static |
||
| 53 | */ |
||
| 54 | 18 | public function fakeRelationships($count = 2) |
|
| 55 | { |
||
| 56 | 18 | $faker = \Faker\Factory::create(); |
|
| 57 | |||
| 58 | 18 | for ($i = 0; $i < $count; $i++) { |
|
| 59 | 18 | $this->addRelationship( |
|
| 60 | 18 | $faker->unique()->numerify('relationship##'), |
|
| 61 | 18 | $this->generator->relationship()->fake() |
|
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | 18 | return $this; |
|
| 66 | } |
||
| 67 | } |
||
| 68 |