Skip to content

[5.x] Support dotted keys in data get #11848

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

Closed
Closed
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
2 changes: 1 addition & 1 deletion src/Data/ContainsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait ContainsData

public function get($key, $fallback = null)
{
return $this->data[$key] ?? $fallback;
return data_get($this->data, $key, $fallback);
}

public function has($key)
Expand Down
17 changes: 17 additions & 0 deletions tests/Data/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,22 @@ public function it_sets_gets_and_removes_data_values()
$this->assertFalse($entry->has('foo'));
}

#[Test]
public function it_gets_nested_data_values()
{
$collection = tap(Collection::make('test'))->save();
$entry = (new Entry)->collection('test');

$return = $entry->set('foo', ['bar' => 'baz']);

$this->assertEquals($entry, $return);
$this->assertTrue($entry->has('foo'));
$this->assertEquals(['bar' => 'baz'], $entry->get('foo'));
$this->assertEquals(['bar' => 'baz'], $entry->value('foo'));
$this->assertEquals('baz', $entry->get('foo.bar'));
$this->assertEquals('fallback', $entry->get('unknown', 'fallback'));
}

#[Test]
public function it_sets_data_values_using_magic_properties()
{
Expand Down Expand Up @@ -836,6 +852,7 @@ public function augment($value)
$relationshipFieldtype = new class extends Fieldtype
{
protected static $handle = 'relationship';

protected $relationship = true;

public function augment($values)
Expand Down