Skip to content

Commit fa57eb4

Browse files
committed
Merge branch 'chore/0.3.0-release-prep'
2 parents 6ef2ae2 + d9730ca commit fa57eb4

File tree

2 files changed

+82
-24
lines changed

2 files changed

+82
-24
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# CHANGELOG
22

3+
## 0.3.0
4+
5+
### Features
6+
7+
- Updated the minimum PHP runtime version to 7.0.0
8+
- Added scalar type declarations to parameters, where possible
9+
- Added return type declarations, where possible
10+
- Enabled strict typing on all files
11+
- Introduced a new 'Builder' and type-processor concept, to better support immutable values/models
12+
- Introduced a new 'Context' concept to hydrators, builders, and the process pipeline, to enable more advanced value/model processing in a more functional manner, without having to resort to holding state in a hydrator/builder
13+
14+
### Bug fixes
15+
16+
- Fixed various code style inconsistencies
17+
18+
319
## 0.2.1
420

521
### Bug fixes

README.md

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Still curious? Check out the [examples](#examples).
4040
The 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(
6969
Sure, 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(
114117
Let'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

Comments
 (0)