1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace VGirol\JsonApiFaker\Factory; |
||||||
6 | |||||||
7 | use VGirol\JsonApiConstant\Members; |
||||||
8 | |||||||
9 | /** |
||||||
10 | * Add "attributes" member to a factory. |
||||||
11 | */ |
||||||
12 | trait HasAttributes |
||||||
13 | { |
||||||
14 | /** |
||||||
15 | * The "attributes" object. |
||||||
16 | * |
||||||
17 | * @var array |
||||||
18 | */ |
||||||
19 | protected $attributes; |
||||||
20 | |||||||
21 | /** |
||||||
22 | * Set all the attributes. |
||||||
23 | * |
||||||
24 | * @param array $attributes |
||||||
25 | * |
||||||
26 | * @return static |
||||||
27 | */ |
||||||
28 | 30 | public function setAttributes(array $attributes) |
|||||
29 | { |
||||||
30 | 30 | $this->attributes = $attributes; |
|||||
31 | |||||||
32 | 30 | return $this; |
|||||
33 | } |
||||||
34 | |||||||
35 | /** |
||||||
36 | * Get all the attributes. |
||||||
37 | * |
||||||
38 | * @return array|null |
||||||
39 | */ |
||||||
40 | 15 | public function getAttributes(): ?array |
|||||
41 | { |
||||||
42 | 15 | return $this->attributes; |
|||||
43 | } |
||||||
44 | |||||||
45 | /** |
||||||
46 | * Add many attributes. |
||||||
47 | * |
||||||
48 | * @param array $attributes |
||||||
49 | * |
||||||
50 | * @return static |
||||||
51 | */ |
||||||
52 | 3 | public function addAttributes(array $attributes) |
|||||
53 | { |
||||||
54 | 3 | foreach ($attributes as $name => $value) { |
|||||
55 | 3 | $this->addAttribute($name, $value); |
|||||
56 | } |
||||||
57 | |||||||
58 | 3 | return $this; |
|||||
59 | } |
||||||
60 | |||||||
61 | /** |
||||||
62 | * Add a single attribute. |
||||||
63 | * |
||||||
64 | * @param string $name |
||||||
65 | * @param mixed $value |
||||||
66 | * |
||||||
67 | * @return static |
||||||
68 | */ |
||||||
69 | 6 | public function addAttribute(string $name, $value) |
|||||
70 | { |
||||||
71 | 6 | $this->addToObject(Members::ATTRIBUTES, $name, $value); |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
72 | |||||||
73 | 6 | return $this; |
|||||
74 | } |
||||||
75 | |||||||
76 | /** |
||||||
77 | * Fill the "attributes" object with fake members and values. |
||||||
78 | * |
||||||
79 | * @param integer $count The number of attributes to generate. |
||||||
80 | * |
||||||
81 | * @return static |
||||||
82 | */ |
||||||
83 | 18 | public function fakeAttributes(int $count = 5) |
|||||
84 | { |
||||||
85 | 18 | $this->setAttributes( |
|||||
86 | 18 | $this->fakeMembers($count) |
|||||
0 ignored issues
–
show
It seems like
fakeMembers() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
87 | ); |
||||||
88 | |||||||
89 | 18 | return $this; |
|||||
90 | } |
||||||
91 | } |
||||||
92 |