Skip to content

Commit

Permalink
Merge pull request #16 from Hi-Folks/feat/apply-field
Browse files Browse the repository at this point in the history
Feat/apply field
  • Loading branch information
roberto-butti authored Aug 3, 2024
2 parents f7ae52e + 14305b3 commit b0960f1
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
<?php

// Assuming $object is an instance of the class that contains the applyField method
$object
->set('name', 'John Doe')
->applyField('name', 'uppercase_name', function($value) {
return strtoupper($value);
});

echo $object->get('uppercase_name'); // Outputs: JOHN DOE

## Testing

```bash
Expand Down
23 changes: 21 additions & 2 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}


Expand Down Expand Up @@ -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;
}




Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/BidimensionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
},
);

0 comments on commit b0960f1

Please sign in to comment.