@@ -22,12 +22,16 @@ $data = new GetValue(data: $array, transformerStrategy: new NoTransformerStrateg
2222
2323## Transformers
2424
25- > Transformers argument in get* methods overrides transformers from strategy.
25+ > Transformers argument in get* methods overrides transformers from strategy.
2626
2727To disable default transformers set ` transformers ` argument to empty array.
2828
2929``` php
30+ $getValue = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
31+ 'key' => ' ',
32+ ]));
3033$value = $getValue->getString('key', []);
34+ // $value === ' '
3135```
3236
3337### TransformToBool
@@ -36,7 +40,11 @@ Transforms most used representations of boolean in string or number ('yes','no',
3640it to bool ** before** validation starts.
3741
3842``` php
39- $getValue->getBool('key', [new \Wrkflow\GetValue\Transformers\TransformToBool()]);
43+ $getValue = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
44+ 'key' => 'yes',
45+ ]));
46+ $value = $getValue->getBool('key', [new \Wrkflow\GetValue\Transformers\TransformToBool()]);
47+ // $value === true
4048```
4149
4250### TrimAndEmptyStringToNull
@@ -46,7 +54,11 @@ $getValue->getBool('key', [new \Wrkflow\GetValue\Transformers\TransformToBool()]
4654Ensures that string is trimmed and transformed to null (if empty string is provided) ** before** validation starts.
4755
4856``` php
49- $getValue->getString('key', [new \Wrkflow\GetValue\Transformers\TrimAndEmptyStringToNull()]);
57+ $getValue = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
58+ 'key' => '',
59+ ]));
60+ $value = $getValue->getString('key', [new \Wrkflow\GetValue\Transformers\TrimAndEmptyStringToNull()]);
61+ // $value === null
5062```
5163
5264### TrimString
@@ -55,7 +67,11 @@ Ensures that string is trimmed **before** validation starts.
5567
5668``` php
5769// Get trimmed string (no '' to null transformation)
58- $getValue->getString('key', [new \Wrkflow\GetValue\Transformers\TrimString()]);
70+ $getValue = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
71+ 'key' => 'Marco Polo ',
72+ ]));
73+ $value = $getValue->getString('key', [new \Wrkflow\GetValue\Transformers\TrimString()]);
74+ // $value === 'Marco Polo'
5975```
6076
6177### ClosureTransformer
@@ -65,6 +81,9 @@ $getValue->getString('key', [new \Wrkflow\GetValue\Transformers\TrimString()]);
6581Transforms the value using closure. Ensure you are returning correct type based on the ` get ` method you have choosed.
6682
6783``` php
84+ $getValue = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
85+ 'key' => 'Marco Polo',
86+ ]));
6887$transformer = new ClosureTransformer(function (mixed $value, string $key): ?string {
6988 if ($value === null) {
7089 return null;
@@ -82,6 +101,9 @@ $md5 = $getValue->getString('key', [$transformer]);
82101Transforms valid array using closure. Always return an array.
83102
84103``` php
104+ $getValue = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
105+ 'key' => ['Marco', 'Polo']
106+ ]));
85107$transformer = new ArrayTransformer(function (array $value, string $key): array {
86108 return array_map(fn (string $value) => md5($value), $value);
87109});
@@ -96,6 +118,9 @@ $values = $getValue->getArray('key', [$transformer]);
96118Transforms ** each value in an array** using closure.
97119
98120``` php
121+ $getValue = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
122+ 'key' => ['Marco', 'Polo']
123+ ]));
99124$transformer = new ArrayItemTransformer( function (mixed $value, string $key): string {
100125 if (is_string($value) !== null) {
101126 throw new ValidationFailedException($key, 'array value not a string');
@@ -114,9 +139,31 @@ $values = $getValue->getArray('key', [$transformer]);
114139Transforms an ** array that contains array values** in a closure that receives wrapped array in GetValue.
115140
116141``` php
142+ $getValue = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
143+ 'key' => [['test' => 'Marco'], ['test' => 'Polo']]
144+ ]));
117145$transformer = new ArrayItemGetterTransformer( function (\Wrkflow\GetValue\GetValue $value, string $key): string {
118146 return [
119- self::KeyValue => $value->getRequiredString(self::KeyValue),
147+ 'test' => $value->getRequiredString('test'),
148+ ];
149+ });
150+
151+ $values = $getValue->getArray('key', [$transformer]);
152+ ```
153+
154+ ### ArrayItemGetterTransformer
155+
156+ > Can be used only with get\* Array\* methods.
157+
158+ Transforms an ** array** in a closure that receives wrapped array in GetValue.
159+
160+ ``` php
161+ $getValue = new \Wrkflow\GetValue\GetValue(new \Wrkflow\GetValue\DataHolders\ArrayData([
162+ 'key' => ['test' => 'Value!']
163+ ]));
164+ $transformer = new ArrayGetterTransformer( function (\Wrkflow\GetValue\GetValue $value, string $key): string {
165+ return [
166+ 'test' => $value->getRequiredString('test'),
120167 ];
121168});
122169
@@ -131,10 +178,10 @@ You can create your own transformer by extending:
131178- Reset of values ` Wrkflow\GetValue\Contracts\TransformerContract `
132179
133180Then implement ` public function transform(mixed $value, string $key): mixed; ` . Expect invalid value and make do not
134- transform the value if it is invalid. Just return it.
181+ transform the value if it is invalid. Just return it.
135182
136183Then implement ` public function beforeValidation(mixed $value, string $key): bool; ` which ensures that transformation
137- is not done before validation.
184+ is not done before validation.
138185
139186Then change the strategy:
140187
0 commit comments