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.3] Ability to get message using implicit keys from MessageBag #15063

Merged
merged 2 commits into from
Aug 26, 2016
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
16 changes: 13 additions & 3 deletions src/Illuminate/Support/MessageBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,23 @@ public function first($key = null, $format = null)
public function get($key, $format = null)
{
// If the message exists in the container, we will transform it and return
// the message. Otherwise, we'll return an empty array since the entire
// methods is to return back an array of messages in the first place.
// the message. Otherwise, we'll check if the key is implicit & collect
// all messages that match the given key and output it as an array.
if (array_key_exists($key, $this->messages)) {
return $this->transform($this->messages[$key], $this->checkFormat($format), $key);
}

return [];
$output = [];

if (Str::contains($key, '*')) {
foreach ($this->messages as $messageKey => $messages) {
if (Str::is($key, $messageKey)) {
$output[$messageKey] = $this->transform($messages, $this->checkFormat($format), $messageKey);
}
}
}

return $output;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following refactor might be more suitable:

public function get($key, $format = null)
{
    // If the message exists in the container, we will transform it and return
    // the message. Otherwise, we'll return an empty array since the entire
    // methods is to return back an array of messages in the first place.
    if (array_key_exists($key, $this->messages)) {
        return $this->transform($this->messages[$key], $this->checkFormat($format), $key);
    }

    $output = [];

     if (Str::contains($key, '*')) {
        foreach ($this->messages as $messageKey => $messages) {
            if (Str::is($key, $messageKey)) {
                $output[$messageKey] = $this->transform($messages, $this->checkFormat($format), $messageKey);
            }
        }
    }

    return $output;
}

}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Support/SupportMessageBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ public function testGetReturnsArrayOfMessagesByKey()
$this->assertEquals(['bar', 'baz'], $container->get('foo'));
}

public function testGetReturnsArrayOfMessagesByImplicitKey()
{
$container = new MessageBag;
$container->setFormat(':message');
$container->add('foo.1', 'bar');
$container->add('foo.2', 'baz');
$this->assertEquals(['foo.1' => ['bar'], 'foo.2' => ['baz']], $container->get('foo.*'));
}

public function testFirstReturnsSingleMessage()
{
$container = new MessageBag;
Expand Down