diff --git a/README.md b/README.md index d9c2b85..6c0da2f 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/O.php b/src/O.php index c7c9bfc..0ed0e1f 100644 --- a/src/O.php +++ b/src/O.php @@ -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); + } }