Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
Vasiliy-Makogon committed Oct 4, 2023
1 parent f9d74e6 commit 0e4ab1b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
25 changes: 23 additions & 2 deletions src/CoverArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,14 @@ public function __unserialize(array $data): void
}

/**
* Возвращает массив с элементами в обратном порядке
*
* @return array
* @see array_reverse
*/
final public function reverse(): array
final public function reverse(bool $preserve_keys = false): array
{
return array_reverse($this->data);
return array_reverse($this->data, $preserve_keys);
}

/**
Expand Down Expand Up @@ -239,6 +241,8 @@ final public function get(string $path): mixed
}

/**
* Фильтрует элементы массива с помощью callback-функции
*
* @param callable|null $callback
* @param int $mode
* @return static
Expand All @@ -250,8 +254,11 @@ final public function filter(?callable $callback = null, int $mode = 0): static
}

/**
* Объединяет элементы массива в строку
*
* @param string $separator
* @return string
* @see implode
*/
final public function implode(string $separator): string
{
Expand All @@ -277,12 +284,26 @@ final public function mapAssociative(callable $callback): static
*
* @param callable $callback
* @return static
* @see array_map
*/
final public function map(callable $callback): static
{
return new static(array_map($callback, $this->data));
}

/**
* Проверяет, присутствует ли в массиве значение
*
* @param mixed $needle
* @param bool $strict
* @return bool
* @see in_array
*/
final public function in(mixed $needle, bool $strict = false): bool
{
return in_array($needle, $this->data, $strict);
}

/**
* Возвращает объект текущего типа, если переданным в метод значением является массив.
* Вложенные элементы массива так же становятся объектом текущего типа.
Expand Down
29 changes: 27 additions & 2 deletions tests/unit/CoverArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,17 +291,28 @@ public function testUnserializeMethod(): void
*/
public function testReverseMethod(): void
{
$this->assertSame(['MySql', 'PHP'], $this->data->get('langs.backend')->reverse());
$this->assertSame([0 => 'MySql', 1 => 'PHP'], $this->data->get('langs.backend')->reverse(false));
$this->assertSame([1 => 'MySql', 0 => 'PHP'], $this->data->get('langs.backend')->reverse(true));
}

/**
* @see CoverArray::filter()
*/
public function testFilterMethod(): void
{
// pass value as the only argument to callback instead
$this->assertSame(['PHP'], $this->data->get('langs.backend')->filter(function ($value) {
return preg_match('~P~', $value);
})->getDataAsArray());

$this->assertSame(['backend' => ['PHP', 'MySql']], $this->data->get('langs')->filter(function ($key) {
return $key === 'backend';
}, ARRAY_FILTER_USE_KEY)->getDataAsArray());

// pass both value and key as arguments to callback instead of the value
$this->assertSame(['backend' => ['PHP', 'MySql']], $this->data->get('langs')->filter(function ($value, $key) {
return $key === 'backend' && $value->in('PHP');
}, ARRAY_FILTER_USE_BOTH)->getDataAsArray());
}

/**
Expand All @@ -326,7 +337,7 @@ public function testMapAssociativeMethod(): void
}

/**
* @see CoverArray::mapAssociative()
* @see CoverArray::map()
*/
public function testMapMethod(): void
{
Expand All @@ -337,4 +348,18 @@ public function testMapMethod(): void
)->getDataAsArray()
);
}

/**
* @see CoverArray::in()
*/
public function testInMethod(): void
{
$this->data->setData(['age' => 40]);

$this->assertTrue($this->data->in(40, true));
$this->assertTrue($this->data->in(40, false));

$this->assertTrue($this->data->in('40', false));
$this->assertFalse($this->data->in('40', true));
}
}

0 comments on commit 0e4ab1b

Please sign in to comment.