diff --git a/CHANGELOG.md b/CHANGELOG.md index 032aa9b..8d0badf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 0.3.8 - 2024-08-03 +- Implementing the `applyField()` method + ## 0.3.7 - 2024-07-28 - Implementing the `forEach()` method diff --git a/README.md b/README.md index 49936d6..e537b75 100644 --- a/README.md +++ b/README.md @@ -193,7 +193,17 @@ The `set()` method supports keys with the dot (or custom) notation for setting v If a key doesn't exist, the `set()` method creates one and sets the value. If a key already exists, the `set()` method will replace the value related to the key. -For example: +#### Parameters + +- `key` (int|string): The key to which the value should be assigned. If a string is provided, you can use dot notation to set nested values. +- `value` (mixed): The value to be assigned to the specified key. +- `charNestedKey` (string, optional): The character used for dot notation in nested keys. Defaults to `.`. + +#### Returns + +- `self`: Returns the instance of the class for method chaining. + +#### Example Usage ```php $articleText = "Some words as a sample sentence"; @@ -614,7 +624,7 @@ $posts = Block::fromJsonUrl($url) // Load the Block from the remote URL // $posts->get("0.tags"); // 3 ``` -## Validating Data 🆕 +## Validating Data You can validate the data in the Block object with JSON schema. JSON Schema is a vocabulary used to annotate and validate JSON documents. @@ -723,6 +733,34 @@ $data->validateJsonWithSchema( ); ``` +## Applying functions + +The `applyField()` method applies a callable function to the value of a specified field and sets the result to another field. This method supports method chaining. + +### Parameters + +- `key` (string|int): The key of the field whose value will be processed. +- `targetKey` (string|int): The key where the result of the callable function should be stored. +- `callable` (callable): The function to apply to the field value. This function should accept a single argument (the value of the field) and return the processed value. + +### Returns + +- `self`: Returns the instance of the class for method chaining. + +### Example Usage + +```php +set('name', 'John Doe') + ->applyField('name', 'uppercase_name', function($value) { + return strtoupper($value); + }); + +echo $object->get('uppercase_name'); // Outputs: JOHN DOE + ## Testing ```bash diff --git a/src/Block.php b/src/Block.php index a35b050..43806d1 100644 --- a/src/Block.php +++ b/src/Block.php @@ -146,7 +146,7 @@ public function getBlockNullable(mixed $key, mixed $defaultValue = null, string * You can use the dot notation for setting a nested value. * @param non-empty-string $charNestedKey */ - public function set(int|string $key, mixed $value, string $charNestedKey = "."): void + public function set(int|string $key, mixed $value, string $charNestedKey = "."): self { if (is_string($key)) { $array = &$this->data; @@ -165,9 +165,10 @@ public function set(int|string $key, mixed $value, string $charNestedKey = "."): } $array[array_shift($keys)] = $value; - return; + return $this; } $this->data[$key] = $value; + return $this; } @@ -223,6 +224,24 @@ public function hasKey(string|int $key): bool return in_array($key, $keys); } + /** + * Applies a callable function to a field and sets the result to a target field. + * + * @param string|int $key The key of the field to be processed. + * @param string|int $targetKey The key where the result should be stored. + * @param callable $callable The function to apply to the field value. + * + * @return self Returns the instance of the class for method chaining. + */ + public function applyField( + string|int $key, + string|int $targetKey, + callable $callable, + ): self { + $this->set($targetKey, $callable($this->get($key))); + return $this; + } + diff --git a/tests/Unit/BidimensionalTest.php b/tests/Unit/BidimensionalTest.php index d825dfd..59f6c00 100644 --- a/tests/Unit/BidimensionalTest.php +++ b/tests/Unit/BidimensionalTest.php @@ -185,3 +185,35 @@ function (): void { expect($rel->get("0"))->toHaveCount(2); }, ); + +test( + 'Apply field', + function (): void { + $table = Block::make( + [ + "title" => "Title", + "number" => 11, + ], + ); + $table->applyField( + "number", + "newfield", + fn($value): int|float => $value * 2, + ); + expect($table)->toHaveCount(3); + expect($table->get("newfield"))->toBe(22); + expect($table->get("number"))->toBe(11); + }, +); + +test( + 'Apply field2', + function (): void { + $object = Block::make(); + $object->set('name', 'John Doe') + ->applyField('name', 'uppercase_name', fn($value): string => strtoupper((string) $value)); + expect($object)->toHaveCount(2); + expect($object->get("uppercase_name"))->toBe("JOHN DOE"); + expect($object->get("name"))->toBe('John Doe'); + }, +);