Skip to content

Commit

Permalink
feat(Array): add contains
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Dec 2, 2024
1 parent d1036a6 commit 8d50018
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
11 changes: 6 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@
}
},
"require-dev": {
"nette/tester": "^2.4",
"phpstan/phpstan": "^1.8",
"phpstan/phpstan-strict-rules": "^1.4",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpstan/phpstan-deprecation-rules": "^2.0",
"tracy/tracy": "^2.9"
},
"config": {
"sort-packages": true
},
"scripts": {
"coverage": "vendor/bin/tester --coverage coverage.html --coverage-src src/ --colors 1 -s -C tests/src",
"qa": "composer stan && composer tests",
"stan": "vendor/bin/phpstan analyse",
"tests": "vendor/bin/tester --colors 1 -s -C tests/src",
"coverage": "vendor/bin/tester --coverage coverage.html --coverage-src src/ --colors 1 -s -C tests/src"
"tests": "vendor/bin/tester --colors 1 -s -C tests/src"
}
}
15 changes: 12 additions & 3 deletions src/Basic/Arrays.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public static function startWith(string $haystack, string ...$needle): bool
}


/**
* strict in_array
* @param list<mixed>|array<string|int, mixed> $haystack
*/
public static function contains(string $needle, array $haystack): bool
{
return in_array($needle, $haystack, true);
}


/**
* @param array<scalar|Stringable|null> $array
* @deprecated use join
Expand All @@ -69,14 +79,13 @@ public static function join(array $array, string $delimiter = ','): string

/**
* The original explode(',', '') return [''] right is [].
* @param non-empty-string $delimiter
*
* @return array<string>
*/
public static function explode(string $value, string $delimiter = ','): array
{
if ($delimiter === '') {
throw new DataType\Exceptions\InvalidArgumentsException('Delimiter like empty string is not allowed.');
} elseif ($value === '') {
if ($value === '') {
return [];
}

Expand Down
2 changes: 0 additions & 2 deletions tests/src/Unit/Basic/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ public function testExplode(): void
{
Assert::same(['a', 'b'], Arrays::explode('a,b'));
Assert::same([], Arrays::explode(''));
Assert::exception(fn (
) => Arrays::explode('a,b', ''), h4kuna\DataType\Exceptions\InvalidArgumentsException::class);
}


Expand Down

0 comments on commit 8d50018

Please sign in to comment.