Skip to content

Commit

Permalink
Added O::has()
Browse files Browse the repository at this point in the history
  • Loading branch information
meszaros-lajos-gyorgy committed Apr 13, 2021
1 parent dc8ac6e commit 255c8d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ Every method is abide to the following rules ( or at least they should. if they
$point2d = O::dissoc("z", 30, $point3d); // {"x": 10, "y": 20}
```

- **has** - checks presence of a key inside an object and an associative array

uses `array_key_exists()` internally

```php
$data = new stdClass();
$data->x = 10;

O::has('x', $data); // true
O::has('y', $data); // false
```

## Future plans

I keep adding methods as I come across the need for them, so if you're missing a method you'd use, then 1) PRs are welcome, 2) Issues are welcome.
Expand Down
7 changes: 6 additions & 1 deletion src/O.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ public static function assoc(string $key, $value, object $data): object {
return $data;
}

// O::dissoc('foo')
// O::dissoc('foo', {foo: 'bar', fizz: 'buzz'}) -> {fizz: 'buzz'}
public static function dissoc(string $key, object $data): object {
unset($data->{$key});
return $data;
}

// O::has('x', {x:10, y:20}) -> true
public static function has(string $key, $data): bool {
return array_key_exists($key, $data);
}
}

0 comments on commit 255c8d2

Please sign in to comment.