@@ -36,6 +36,13 @@ public function __construct(
3636 ) {}
3737
3838 /**
39+ * Sets the target class for mapping operations.
40+ *
41+ * ### Example
42+ * ```php
43+ * $factory->forClass(Author::class)->from(['name' => 'Jon Doe']);
44+ * ```
45+ *
3946 * @template T of object
4047 * @param T|class-string<T> $objectOrClass
4148 * @return self<T>
@@ -47,6 +54,14 @@ public function forClass(mixed $objectOrClass): self
4754 return $ this ;
4855 }
4956
57+ /**
58+ * Sets the source data for mapping.
59+ *
60+ * ### Example
61+ * ```php
62+ * $factory->withData(['name' => 'Jon Doe'])->to(Author::class);
63+ * ```
64+ */
5065 public function withData (mixed $ data ): self
5166 {
5267 $ this ->from = $ data ;
@@ -55,6 +70,18 @@ public function withData(mixed $data): self
5570 }
5671
5772 /**
73+ * Marks the mapping operation to process an array of objects instead of a single object.
74+ *
75+ * ### Example
76+ * ```php
77+ * make(Author::class)
78+ * ->collection()
79+ * ->from([
80+ * ['name' => 'Jon Doe'],
81+ * ['name' => 'Jane Smith'],
82+ * ]);
83+ * ```
84+ *
5885 * @return self<ClassType[]>
5986 */
6087 public function collection (): self
@@ -65,7 +92,14 @@ public function collection(): self
6592 }
6693
6794 /**
68- * Sets the context for mapping.
95+ * Sets the context for mapping, allowing context-specific mappers to be used.
96+ *
97+ * ### Example
98+ * ```php
99+ * make(Author::class)
100+ * ->in(Context::API)
101+ * ->from(['name' => 'Jon Doe']);
102+ * ```
69103 *
70104 * @return self<ClassType>
71105 */
@@ -78,6 +112,16 @@ public function in(Context|UnitEnum|string|null $context): self
78112 }
79113
80114 /**
115+ * Maps the given data to the target class.
116+ *
117+ * ### Example
118+ * ```php
119+ * $author = make(Author::class)->from([
120+ * 'first_name' => 'Jon',
121+ * 'last_name' => 'Doe',
122+ * ]);
123+ * ```
124+ *
81125 * @return ClassType
82126 */
83127 public function from (mixed $ data ): mixed
@@ -90,6 +134,19 @@ public function from(mixed $data): mixed
90134 }
91135
92136 /**
137+ * Specifies custom mappers to use for the mapping operation.
138+ *
139+ * ### Example
140+ * ```php
141+ * map(['name' => 'Jon Doe'])
142+ * ->with(CustomMapper::class)
143+ * ->to(Author::class);
144+ *
145+ * map($data)
146+ * ->with(fn (SomeMapper $mapper) => $mapper->map($data))
147+ * ->do();
148+ * ```
149+ *
93150 * @template MapperType of \Tempest\Mapper\Mapper
94151 * @param Closure(MapperType $mapper, mixed $from): mixed|class-string<\Tempest\Mapper\Mapper> ...$mappers
95152 * @return self<ClassType>
@@ -102,6 +159,16 @@ public function with(Closure|string ...$mappers): self
102159 }
103160
104161 /**
162+ * Maps the source data to the specified target class.
163+ *
164+ * ### Example
165+ * ```php
166+ * $author = map([
167+ * 'first_name' => 'Jon',
168+ * 'last_name' => 'Doe',
169+ * ])->to(Author::class);
170+ * ```
171+ *
105172 * @template T of object
106173 * @param T|class-string<T>|string $to
107174 * @return T|T[]|mixed
@@ -115,6 +182,17 @@ public function to(mixed $to): mixed
115182 );
116183 }
117184
185+ /**
186+ * Executes the mapping using explicitly specified mappers.
187+ *
188+ * ### Example
189+ * ```php
190+ * $result = map($data)
191+ * ->with(ObjectToArrayMapper::class)
192+ * ->with(ArrayToJsonMapper::class)
193+ * ->do();
194+ * ```
195+ */
118196 public function do (): mixed
119197 {
120198 if ($ this ->with === []) {
@@ -134,6 +212,15 @@ public function do(): mixed
134212 return $ result ;
135213 }
136214
215+ /**
216+ * Converts the source data to an array.
217+ *
218+ * ### Example
219+ * ```php
220+ * $array = map($author)->toArray();
221+ * $arrays = map($authors)->collection()->toArray();
222+ * ```
223+ */
137224 public function toArray (): array
138225 {
139226 if (is_object ($ this ->from )) {
@@ -158,6 +245,15 @@ public function toArray(): array
158245 throw new DataCouldNotBeMapped ($ this ->from , 'array ' );
159246 }
160247
248+ /**
249+ * Converts the source data to a JSON string.
250+ *
251+ * ### Example
252+ * ```php
253+ * $json = map($author)->toJson();
254+ * $json = map(['name' => 'Jon Doe'])->toJson();
255+ * ```
256+ */
161257 public function toJson (): string
162258 {
163259 if (is_object ($ this ->from )) {
@@ -172,6 +268,13 @@ public function toJson(): string
172268 }
173269
174270 /**
271+ * Maps data from one format to another.
272+ *
273+ * ### Example
274+ * ```php
275+ * $author = $factory->map(['name' => 'Jon Doe'], to: Author::class);
276+ * ```
277+ *
175278 * @template T of object
176279 * @param T|class-string<T>|string $to
177280 * @return T|mixed
0 commit comments