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

CodeBlock is not working inside an array of blocks/attachments with blockId null #39

Open
AdryanSignor opened this issue Mar 15, 2023 · 0 comments

Comments

@AdryanSignor
Copy link

Q A
Slack Block Kit for PHP version 2.0.0
PHP version 8.1.16

The Slack Block Kit for PHP is not working when use CodeBlock inside an array of blocks/attachments with blockId null.

use SlackPhp\BlockKit\Kit;

echo 'foobar1:' . PHP_EOL;
echo Kit::message(
    attachments: [
        Kit::attachment([
            Kit::codeBlock(
                '$this->foobar1();',
                //blockId: 'foobar1'
            )
        ])
    ]
)->toJson() . PHP_EOL;

echo PHP_EOL;

echo 'foobar2:' . PHP_EOL;
echo Kit::message(
        blocks: [
            Kit::codeBlock(
                '$this->foobar2();',
            //blockId: 'foobar2'
            )
        ]
    )->toJson() . PHP_EOL;

echo PHP_EOL;

echo 'foobar3:' . PHP_EOL;
echo Kit::message(
    blocks: Kit::codeBlock(
        '$this->foobar3();',
        //blockId: 'foobar3'
    )
)->toJson() . PHP_EOL;

Expected result

foobar1:
{"attachments":[{"blocks":[{"type":"section","text":{"type":"mrkdwn","text":"```\n$this->foobar1();\n```"}}]}]}

foobar2:
{"blocks":[{"type":"section","text":{"type":"mrkdwn","text":"```\n$this->foobar2();\n```"}}]}

foobar3:
{"blocks":[{"type":"section","text":{"type":"mrkdwn","text":"```\n$this->foobar3();\n```"}}]}

Current result

foobar1:
{"attachments":[[]]}

foobar2:
[]

foobar3:
{"blocks":[{"type":"section","text":{"type":"mrkdwn","text":"```\n$this->foobar3();\n```"}}]}

Possible Solution

I think the problem is on SlackPhp\BlockKit\Blocks\Virtual\VirtualBlock::getIterator inherited by SlackPhp\BlockKit\Blocks\Virtual\CodeBlock.
It must yield from $blocks instead of return the current generator when blockId is null.

public function getIterator(): Traversable
{
    $blocks = parent::getIterator();
    if ($this->blockId === null) {
        return yield from $blocks;
        // or
        // yield from $blocks;
        // return; // It make no diference, just change the return of $generator->getReturn() 
        // https://www.php.net/manual/en/generator.getreturn.php
    }

    // ...
}

I do not know if this change breaks other components/classes, I just tested here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant