@@ -35,7 +35,8 @@ return [
35
35
36
36
## Usage
37
37
38
- In any model that contains sensitive data use ` Anonymizable ` trait and implement ` anonymizableAttributes ` method:
38
+ ### Model based definitions
39
+ In any model that contains sensitive data use the ` Anonymizable ` trait and implement the ` anonymizableAttributes ` method:
39
40
40
41
``` php
41
42
<?php
@@ -50,10 +51,10 @@ class User extends Authenticatable
50
51
return [
51
52
'email' => $this->id . '@custom.dev',
52
53
'password' => 'secret',
53
- 'firstname' => $faker->firstName,
54
- 'surname' => $faker->lastName,
55
- 'phone' => $faker->e164PhoneNumber,
56
- 'position' => $faker->jobTitle,
54
+ 'firstname' => $faker->firstName() ,
55
+ 'surname' => $faker->lastName() ,
56
+ 'phone' => $faker->e164PhoneNumber() ,
57
+ 'position' => $faker->jobTitle() ,
57
58
'token' => null,
58
59
];
59
60
}
@@ -65,6 +66,133 @@ class User extends Authenticatable
65
66
}
66
67
}
67
68
```
69
+
70
+ ### Factory based definitions
71
+ To reduce the amount of necessary code, the anonymizable attributes may also be defined on your factories.
72
+ Do note that the model still needs to implement the ` Anonymizable ` to work with these definitions.
73
+
74
+ #### Attributes based on the factory's definition
75
+ It is possible to use the factory's definition to use as anonymizable values.
76
+ Implement the ` anonymizableAttributes ` method on your factory and return an array with keys that corresponds to the definition of the factory:
77
+
78
+ ``` php
79
+ <?php
80
+
81
+ class UserFactory extends Factory
82
+ {
83
+ public function definition(): array
84
+ {
85
+ return [
86
+ 'email' => $this->faker()->numberBetween(1, 100_000) . '@custom.dev',
87
+ 'password' => 'secret',
88
+ 'firstname' => $this->faker->firstName(),
89
+ 'surname' => $this->faker->lastName(),
90
+ 'phone' => $this->faker->e164PhoneNumber(),
91
+ 'position' => $this->faker->jobTitle(),
92
+ 'token' => null,
93
+ ]
94
+ }
95
+
96
+ public function anonymizableAttributes(): array
97
+ {
98
+ return [
99
+ 'email',
100
+ 'password',
101
+ 'firstname',
102
+ 'surname',
103
+ 'phone',
104
+ 'position',
105
+ 'token',
106
+ ]
107
+ }
108
+ }
109
+ ```
110
+ This will use reduce the amount of code you need to write if the data you want to anonymize is the same as your factory's definition.
111
+
112
+ Note that only the defined keys will be anonymized!
113
+
114
+ #### Attributes based on a custom definition
115
+ If you prefer to still use custom values, you can still define them on the factory.
116
+ Implement the ` anonymizableDefinition ` method on your factory, and return a keyed array:
117
+
118
+ ``` php
119
+ <?php
120
+
121
+ class UserFactory extends Factory
122
+ {
123
+ public function definition(): array
124
+ {
125
+ return [
126
+ 'email' => $this->faker()->numberBetween(1, 100_000) . '@custom.dev',
127
+ 'password' => 'secret',
128
+ 'firstname' => $faker->firstName(),
129
+ 'surname' => $faker->lastName(),
130
+ 'phone' => $faker->e164PhoneNumber(),
131
+ 'position' => $faker->jobTitle(),
132
+ 'token' => null,
133
+ ]
134
+ }
135
+
136
+ public function anonymizableDefinition(): array
137
+ {
138
+ return [
139
+ 'email' => $this->faker()->numberBetween(1, 100_000) . '@custom.dev',
140
+ 'password' => 'secret',
141
+ 'firstname' => $faker->firstName(),
142
+ 'surname' => $faker->lastName(),
143
+ 'phone' => $faker->e164PhoneNumber(),
144
+ 'position' => $faker->jobTitle(),
145
+ 'token' => $this->faker->md5(),
146
+ ]
147
+ }
148
+ }
149
+ ```
150
+
151
+ Defining custom values is mostly useful when used in tandem with the ` anonymizableAttributes ` method.
152
+ This allows certain attributes from the factory to be used, while still defining custom attributes when necessary:
153
+ ``` php
154
+ <?php
155
+
156
+ class UserFactory extends Factory
157
+ {
158
+ public function definition(): array
159
+ {
160
+ return [
161
+ 'email' => $this->faker()->numberBetween(1, 100_000) . '@custom.dev',
162
+ 'password' => 'secret',
163
+ 'firstname' => $faker->firstName(),
164
+ 'surname' => $faker->lastName(),
165
+ 'phone' => $faker->e164PhoneNumber(),
166
+ 'position' => $faker->jobTitle(),
167
+ 'token' => null,
168
+ ]
169
+ }
170
+
171
+ public function anonymizableAttributes(): array
172
+ {
173
+ return [
174
+ 'email',
175
+ 'password',
176
+ 'firstname',
177
+ 'surname',
178
+ 'phone',
179
+ 'position',
180
+ ]
181
+ }
182
+
183
+ public function anonymizableDefinition(): array
184
+ {
185
+ return [
186
+ 'token' => $this->faker->md5(),
187
+ ]
188
+ }
189
+ }
190
+ ```
191
+
192
+ ### Important note
193
+ > The data from the ` anonymizable ` methods on the factory will overwrite the data defined in the ` anonymizableAttributes ` method on the model!
194
+
195
+ ### Running the anonymizer
68
196
Anonymization is performed using command:
69
197
70
198
``` bash
@@ -78,4 +206,4 @@ php artisan db:anonymize --model=\\App\User --model=\\App\\Profile
78
206
79
207
## License
80
208
81
- The MIT License (MIT). Please see [ License File] ( LICENSE ) for more information.
209
+ The MIT License (MIT). Please see [ License File] ( LICENSE ) for more information.
0 commit comments