Skip to content

Commit

Permalink
Adding has and hasKey methods
Browse files Browse the repository at this point in the history
  • Loading branch information
roberto-butti committed Jun 26, 2024
1 parent 2480906 commit f807d34
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Changelog

## 0.3.1 - 2024-06-26
- Add `has()` and `hasKey()` method

## 0.3.0 - 2024-06-24
- Add validation with JSON Schema https://json-schema.org/


## 0.2.0 - 2024-06-22
- Add Yaml capabilities (loading from Yaml and exporting to Yaml)

Expand Down
20 changes: 20 additions & 0 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ public function entries(): self
return self::make($pairs);
}

/**
* @return Block object that contains the key/value pairs for each index in the array
*/
public function values(): self
{
$pairs = $this->data;
return self::make($pairs);
}

/**
* Returns a new array [] or a new Blcok object that contains the keys
* for each index in the Block object
Expand All @@ -202,6 +211,17 @@ public function keys(bool $returnBlockClass = false): int|string|array|Block
return array_keys($this->data);
}

public function has(mixed $value): bool
{
return in_array($value, $this->values()->toArray());
}

public function hasKey(string|int $key): bool
{
/** @var array<int, int|string> $keys */
$keys = $this->keys();
return in_array($key, $keys);
}



Expand Down
12 changes: 12 additions & 0 deletions tests/Feature/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,15 @@


});

it('has some value', function (): void {
$file = "./composer.json";
$composer = Block::fromJsonFile($file);
expect($composer->getBlock("require"))->toBeInstanceOf(Block::class);
expect($composer->getBlock("require.php"))->toBeInstanceOf(Block::class);
expect($composer->get("require.php"))->toBeString();

expect($composer->getBlock("require")->has("^8.1|^8.2|^8.3"))->toBeTrue();
expect($composer->getBlock("require")->hasKey("php"))->toBeTrue();
expect($composer->getBlock("require-dev")->hasKey("pestphp/pest"))->toBeTrue();
});

0 comments on commit f807d34

Please sign in to comment.