Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add keys and values modifiers #10185

Merged
merged 4 commits into from
May 24, 2024
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
20 changes: 20 additions & 0 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,16 @@ public function keyBy($value, $params)
return is_array($value) ? $rekeyed->all() : $rekeyed;
}

/**
* Get the keys of an array.
*
* @return array|Collection
*/
public function keys($value)
{
return is_array($value) ? array_keys($value) : $value->keys();
}

/**
* Returns the last $params[0] characters of a string, or the last element of an array.
*
Expand Down Expand Up @@ -2768,6 +2778,16 @@ public function parse_url($value, $params)
return parse_url($value, $component);
}

/**
* Get the values of an array.
*
* @return array|Collection
*/
public function values($value)
{
return is_array($value) ? array_values($value) : $value->values();
}

/**
* Get the date difference in weeks.
*
Expand Down
40 changes: 40 additions & 0 deletions tests/Modifiers/KeysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Modifiers;

use Illuminate\Support\Collection;
use Statamic\Modifiers\Modify;
use Tests\TestCase;

class KeysTest extends TestCase
{
/** @test */
public function it_gets_the_keys_of_an_array(): void
{
$input = [
'chicken' => 'nuggets',
'nuggets' => 'Denver',
];

$modified = $this->modify($input);
$this->assertEquals(['chicken', 'nuggets'], $modified);
}

/** @test */
public function it_gets_the_keys_of_a_collection(): void
{
$input = collect([
'chicken' => 'nuggets',
'nuggets' => 'Denver',
]);

$modified = $this->modify($input);
$this->assertInstanceOf(Collection::class, $modified);
$this->assertEquals(['chicken', 'nuggets'], $modified->all());
}

private function modify($value)
{
return Modify::value($value)->keys()->fetch();
}
}
40 changes: 40 additions & 0 deletions tests/Modifiers/ValuesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Tests\Modifiers;

use Illuminate\Support\Collection;
use Statamic\Modifiers\Modify;
use Tests\TestCase;

class ValuesTest extends TestCase
{
/** @test */
public function it_gets_the_values_of_an_array(): void
{
$input = [
'chicken' => 'nuggets',
'nuggets' => 'Denver',
];

$modified = $this->modify($input);
$this->assertEquals(['nuggets', 'Denver'], $modified);
}

/** @test */
public function it_gets_the_values_of_a_collection(): void
{
$input = collect([
'chicken' => 'nuggets',
'nuggets' => 'Denver',
]);

$modified = $this->modify($input);
$this->assertInstanceOf(Collection::class, $modified);
$this->assertEquals(['nuggets', 'Denver'], $modified->all());
}

private function modify($value)
{
return Modify::value($value)->values()->fetch();
}
}
Loading