Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"require-dev": {
"laravel/pint": "^1.2",
"phpunit/phpunit": "^11.0",
"phpstan/phpstan": "^1.6",
"rector/rector": "^1"
"phpstan/phpstan": "^2.0",
"rector/rector": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
28 changes: 12 additions & 16 deletions src/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ final class Arr implements Iterator, ArrayAccess, Countable
{
use Calculable;

/** @var array<int|string, mixed> */
private array $arr;

/** @param array<int|string, mixed> $arr */
public function __construct(array $arr = [])
public function __construct(private array $arr = [])
{
$this->arr = $arr;
}

public static function fromFunction(callable $callable, int $count): Arr
Expand Down Expand Up @@ -111,7 +107,7 @@ public function arr(): array
*
* @param non-empty-string $charNestedKey
*/
public function get(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed
public function get(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): mixed
{
if (is_string($key)) {
$keyString = strval($key);
Expand All @@ -137,7 +133,7 @@ public function get(mixed $key, mixed $defaultValue = null, string $charNestedKe
* In the case the $key doesn't exist, an empty Arr can be returned
* @param non-empty-string $charNestedKey
*/
public function getArr(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr
public function getArr(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr
{
$value = $this->getArrNullable($key, $defaultValue, $charNestedKey);
if (is_null($value)) {
Expand All @@ -152,7 +148,7 @@ public function getArr(mixed $key, mixed $defaultValue = null, string $charNeste
* In the case the $key doesn't exist, null can be returned
* @param non-empty-string $charNestedKey
*/
public function getArrNullable(mixed $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr|null
public function getArrNullable(int|string $key, mixed $defaultValue = null, string $charNestedKey = "."): Arr|null
{
$value = $this->get($key, $defaultValue, $charNestedKey);
if (is_null($value)) {
Expand Down Expand Up @@ -195,7 +191,10 @@ public function set(int|string $key, mixed $value, string $charNestedKey = "."):
$array = &$array[$key];
}

$array[array_shift($keys)] = $value;
$lastKey = array_shift($keys);
if ($lastKey !== null) {
$array[$lastKey] = $value;
}
return;

}
Expand All @@ -205,7 +204,7 @@ public function set(int|string $key, mixed $value, string $charNestedKey = "."):
/**
* Unset an array element by their key if it exists
*/
public function unset(mixed $key): bool
public function unset(int|string $key): bool
{
if ($this->get($key)) {
unset($this->arr[$key]);
Expand Down Expand Up @@ -315,9 +314,9 @@ public function forEach(callable $callback): self
* It returns Arr or [] depending on $returnArrClass value
*
* @param bool $returnArrClass true if you need Arr object
* @return int|string|array<int|string, mixed>|Arr
* @return array<int|string, mixed>|Arr
*/
public function keys(bool $returnArrClass = false): int|string|array|Arr
public function keys(bool $returnArrClass = false): array|Arr
{
if ($returnArrClass) {
return self::make(array_keys($this->arr));
Expand Down Expand Up @@ -504,8 +503,6 @@ public function some(callable $callback): bool
/**
* Determines whether the array includes a certain value $element among its entries,
* returning true or false as appropriate
*
* @param int|null $fromIndex
*/
public function includes(mixed $element, ?int $fromIndex = null): bool
{
Expand Down Expand Up @@ -767,15 +764,14 @@ public function find(callable $callback): mixed
* The copyWithin() method shallow copies part of an array to another
* location in the same array and returns it without modifying its length.
*
* @param int|null $end
* @return array<int|string, mixed>
*/
public function copyWithin(int $target, int $start = 0, ?int $end = null): array
{
$arrayLength = $this->length();
$chuck = $this->slice($start, $end);
if ($target < 0) {
$target = $arrayLength - (int) abs($target);
$target = $arrayLength - abs($target);
}

foreach ($chuck as $value) {
Expand Down
10 changes: 7 additions & 3 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,17 @@ public function groupBy(string|int $field): Table
foreach ($this->rows as $value) {
$property = $value->get($field);
$property = $this->castVariableForStrval($property);
if (!$property) {
if ($property === null) {
continue;
}
if (array_key_exists(strval($property), $result)) {
if ($property === false) {
continue;
}
$result[$property] = $value;
$key = is_bool($property) || is_float($property) ? strval($property) : $property;
if (array_key_exists(strval($key), $result)) {
continue;
}
$result[$key] = $value;
}

return self::make(array_values($result));
Expand Down
14 changes: 0 additions & 14 deletions tests/ArrSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,6 @@ public function test_basic_set(): void
public function test_nested_set_array(): void
{
$articleText = "Some words as a sample sentence";
$textFieldArray = [
"type" => "doc",
"content" => [
[
"content" => [
[
"text" => $articleText,
"type" => "text"
]
],
"type" => "paragraph"
]
]
];
$textField = Arr::make();
$textField->set("type", "doc");
$textField->set("content.0.content.0.text", $articleText);
Expand Down