@@ -40,7 +40,7 @@ Still curious? Check out the [examples](#examples).
4040The easiest example to relate to in the PHP world? "Form" or HTTP request data:
4141
4242``` php
43- class UserHydrator implements Incoming\Hydrator\HydratorInterface
43+ class UserHydrator implements Incoming\Hydrator\Hydrator
4444{
4545 public function hydrate($input, $model)
4646 {
@@ -56,7 +56,7 @@ class UserHydrator implements Incoming\Hydrator\HydratorInterface
5656$incoming = new Incoming\Processor();
5757
5858// Process our raw form/request input into a User model
59- $user = $incoming->process (
59+ $user = $incoming->processForModel (
6060 $_POST, // Our HTTP form-data array
6161 new User(), // Our model to hydrate
6262 new UserHydrator() // The hydrator above
@@ -69,24 +69,25 @@ $user = $incoming->process(
6969Sure, that's a pretty contrived example. But what kind of power can we gain when we compose some pieces together?
7070
7171``` php
72- class BlogPostHydrator implements Incoming\Hydrator\HydratorInterface
72+ class BlogPostHydrator implements Incoming\Hydrator\ContextualHydrator
7373{
74- private $ user;
74+ const USER_CONTEXT_KEY = ' user' ;
7575
76- public function __construct(User $user)
77- {
78- $this->user = $user;
79- }
80-
81- public function hydrate($input, $model)
76+ public function hydrate($input, $model, Map $context = null)
8277 {
8378 $model->setBody($input['body']);
8479 $model->setCategories($input['categories']);
8580 $model->setTags($input['tags']);
8681
8782 // Only allow admin users to publish posts
88- if ($this->user->isAdmin()) {
89- $model->setPublished($input['published']);
83+ if (null !== $context && $context->exists(self::USER_CONTEXT_KEY)) {
84+ $user = $context->get(self::USER_CONTEXT_KEY);
85+
86+ $model->setAuthor($user->getName());
87+
88+ if ($user->isAdmin()) {
89+ $model->setPublished($input['published']);
90+ }
9091 }
9192
9293 return $model;
@@ -96,15 +97,17 @@ class BlogPostHydrator implements Incoming\Hydrator\HydratorInterface
9697// Create our incoming processor
9798$incoming = new Incoming\Processor();
9899
99- $hydrator = new BlogPostHydrator(
100- $this->getCurrentUser() // A user context for the hydrator
101- );
100+ // Create a context for the hydrator with active data
101+ $context = Map::fromArray([
102+ BlogPostHydrator::USER_CONTEXT_KEY => $this->getCurrentUser() // A user context
103+ ]);
102104
103105// Process our raw form/request input to update our BlogPost model
104- $post = $incoming->process (
106+ $post = $incoming->processForModel (
105107 $_POST, // Our HTTP form-data array
106108 BlogPost::find($_GET['id']), // Fetch our blog post to update and pass it in
107- $hydrator // The hydrator above
109+ new BlogPostHydrator(), // The hydrator above
110+ $context // Context data to enable more powerful conditional processing
108111);
109112
110113// Validate and save the blog post
@@ -114,19 +117,21 @@ $post = $incoming->process(
114117Let's try and filter our input first.
115118
116119``` php
117- class SpecialCharacterFilterTransformer implements Incoming\Transformer\TransformerInterface
120+ class SpecialCharacterFilterTransformer implements Incoming\Transformer\Transformer
118121{
119122 public function transform($input)
120123 {
121- foreach($input as & $string) {
122- $string = filter_var($string, FILTER_SANITIZE_STRING);
124+ $transformed = [];
125+
126+ foreach($input as $key => $string) {
127+ $transformed[$key] = filter_var($string, FILTER_SANITIZE_STRING);
123128 }
124129
125- return $input ;
130+ return $transformed ;
126131 }
127132}
128133
129- class UserHydrator implements Incoming\Hydrator\HydratorInterface
134+ class UserHydrator implements Incoming\Hydrator\Hydrator
130135{
131136 // Same as previous examples...
132137}
@@ -137,7 +142,7 @@ $incoming = new Incoming\Processor(
137142);
138143
139144// Process our raw form/request input into a User model
140- $user = $incoming->process (
145+ $user = $incoming->processForModel (
141146 $_POST, // Our HTTP form-data array
142147 new User(), // Our model to hydrate
143148 new UserHydrator() // The hydrator above
@@ -167,7 +172,7 @@ class UserHydrator extends Incoming\Hydrator\AbstractDelegateHydrator
167172$incoming = new Incoming\Processor();
168173
169174// Process our raw form/request input into a User model
170- $user = $incoming->process (
175+ $user = $incoming->processForModel (
171176 $_POST, // Our HTTP form-data array
172177 new User(), // Our model to hydrate
173178 new UserHydrator() // The hydrator above
@@ -177,6 +182,43 @@ $user = $incoming->process(
177182// ...
178183```
179184
185+ Immutable objects or objects with required data in the constructor? No problem!
186+
187+ ``` php
188+ class User
189+ {
190+ private $first_name;
191+ private $last_name;
192+
193+ public function __construct(string $first_name, string $last_name)
194+ {
195+ $this->first_name = $first_name;
196+ $this->last_name = $last_name;
197+ }
198+ }
199+
200+ class UserBuilder extends Incoming\Hydrator\AbstractDelegateBuilder
201+ {
202+ public function buildModel(Incoming\Structure\Map $input): User
203+ {
204+ return new User($input['first_name'], $input['last_name']);
205+ }
206+ }
207+
208+ // Create our incoming processor
209+ $incoming = new Incoming\Processor();
210+
211+ // Process our raw form/request input into a new User model
212+ $user = $incoming->processForType(
213+ $_POST, // Our HTTP form-data array
214+ User::class, // Our type to build
215+ new UserBuilder() // The builder above
216+ );
217+
218+ // Validate and save the user
219+ // ...
220+ ```
221+
180222
181223## Wait, what? Why not just use "x" or "y"?
182224
0 commit comments