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

Add directive @bool to Blade #53179

Merged
merged 7 commits into from
Oct 16, 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
19 changes: 15 additions & 4 deletions src/Illuminate/View/Compilers/Concerns/CompilesConditionals.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,14 @@ public function compileEndOnce()
}

/**
* Compile a selected block into valid PHP.
* Compile a boolean value into a raw true / false value for embedding into HTML attributes or JavaScript.
*
* @param string $condition
* @param bool $condition
* @return string
*/
protected function compileSelected($condition)
protected function compileBool($condition)
{
return "<?php if{$condition}: echo 'selected'; endif; ?>";
return "<?php echo ($condition ? 'true' : 'false'); ?>";
}

/**
Expand Down Expand Up @@ -360,6 +360,17 @@ protected function compileReadonly($condition)
return "<?php if{$condition}: echo 'readonly'; endif; ?>";
}

/**
* Compile a selected block into valid PHP.
*
* @param string $condition
* @return string
*/
protected function compileSelected($condition)
{
return "<?php if{$condition}: echo 'selected'; endif; ?>";
}

/**
* Compile the push statements into valid PHP.
*
Expand Down
67 changes: 67 additions & 0 deletions tests/View/Blade/BladeBoolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeBoolTest extends AbstractBladeTestCase
{
public function testBool()
Copy link
Contributor

Choose a reason for hiding this comment

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

This is probably one of the few cases where an eval() test would have found the error above, like so:

public function testCompileBool(): void
    {
        $someViewVarTruthy = 123;
        $compiled = $this->compiler->compileString('@bool($someViewVarTruthy)');

        ob_start();
        eval(substr($compiled, 6, -3));
        $this->assertEquals('true', ob_get_clean());

        $someViewVarFalsey = '0';
        $compiled = $this->compiler->compileString('@bool($someViewVarFalsey)');

        ob_start();
        eval(substr($compiled, 6, -3));
        $this->assertEquals('false', ob_get_clean());

    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, it didn't consider a Falsey values, i added a Falsey support and a couple of test for NULL and Class instances.

{
// For Javascript object{'isBool' : true}
$string = "{'isBool' : @bool(true)}";
$expected = "{'isBool' : <?php echo ((true) ? 'true' : 'false'); ?>}";
$this->assertEquals($expected, $this->compiler->compileString($string));

// For Javascript object{'isBool' : false}
$string = "{'isBool' : @bool(false)}";
$expected = "{'isBool' : <?php echo ((false) ? 'true' : 'false'); ?>}";
$this->assertEquals($expected, $this->compiler->compileString($string));

// For Alpine.js x-show attribute
$string = "<input type='text' x-show='@bool(true)' />";
$expected = "<input type='text' x-show='<?php echo ((true) ? 'true' : 'false'); ?>' />";
$this->assertEquals($expected, $this->compiler->compileString($string));

// For Alpine.js x-show attribute
$string = "<input type='text' x-show='@bool(false)' />";
$expected = "<input type='text' x-show='<?php echo ((false) ? 'true' : 'false'); ?>' />";
$this->assertEquals($expected, $this->compiler->compileString($string));
}

public function testCompileBool(): void
{
$someViewVarTruthy = 123;
$compiled = $this->compiler->compileString('@bool($someViewVarTruthy)');

ob_start();
eval(substr($compiled, 6, -3));
$this->assertEquals('true', ob_get_clean());

$someViewVarFalsey = '0';
$compiled = $this->compiler->compileString('@bool($someViewVarFalsey)');

ob_start();
eval(substr($compiled, 6, -3));
$this->assertEquals('false', ob_get_clean());

$anotherSomeViewVarTruthy = new SomeClass();
$compiled = $this->compiler->compileString('@bool($anotherSomeViewVarTruthy)');

ob_start();
eval(substr($compiled, 6, -3));
$this->assertEquals('true', ob_get_clean());

$anotherSomeViewVarFalsey = null;
$compiled = $this->compiler->compileString('@bool($anotherSomeViewVarFalsey)');

ob_start();
eval(substr($compiled, 6, -3));
$this->assertEquals('false', ob_get_clean());
}
}

class SomeClass
{
public function someMethod()
{
}
}