transform

The transform function executes a closure on a given value if the value is not blank and then returns the return value of the closure:
<?php $callback = function ($value) {
    return $value * 2;
};

$result = transform(5, $callback);
<?php     protected function transform($value, callable $callback, $default = null)

    {

        return transform(

            $value, $callback, func_num_args() === 3 ? $default : new MissingValue

        );

    }

}
<?php     public function testTransform()

    {

        $this->assertEquals(10, transform(5, function ($value) {

            return $value * 2;

        }));



        $this->assertNull(transform(null, function () {

            return 10;
<?php             return $value * 2;

        }));



        $this->assertNull(transform(null, function () {

            return 10;

        }));

    }



    public function testTransformDefaultWhenBlank()
<?php     public function testTransformDefaultWhenBlank()

    {

        $this->assertSame('baz', transform(null, function () {

            return 'bar';

        }, 'baz'));



        $this->assertSame('baz', transform('', function () {

            return 'bar';
<?php             return 'bar';

        }, 'baz'));



        $this->assertSame('baz', transform('', function () {

            return 'bar';

        }, function () {

            return 'baz';

        }));

    }



    public function testWith()
<?php }));




assertType('int|null', transform(optional(), fn () => 1));


assertType('int|string', transform(optional(), fn () => 1, fn () => 'string'));
<?php assertType('int|null', transform(optional(), fn () => 1));


assertType('int|string', transform(optional(), fn () => 1, fn () => 'string'));




assertType('int', transform('filled', fn () => 1));
<?php assertType('int|string', transform(optional(), fn () => 1, fn () => 'string'));




assertType('int', transform('filled', fn () => 1));

assertType('int', transform(['filled'], fn () => 1));

assertType('int', transform(new User(), fn () => 1));
<?php assertType('int', transform('filled', fn () => 1));

assertType('int', transform(['filled'], fn () => 1));

assertType('int', transform(new User(), fn () => 1));
<?php assertType('int', transform('filled', fn () => 1));

assertType('int', transform(['filled'], fn () => 1));

assertType('int', transform(new User(), fn () => 1));




assertType('null', transform(null, fn () => 1));
<?php assertType('int', transform(new User(), fn () => 1));




assertType('null', transform(null, fn () => 1));

assertType('null', transform('', fn () => 1));

assertType('null', transform([], fn () => 1));
<?php assertType('null', transform(null, fn () => 1));

assertType('null', transform('', fn () => 1));

assertType('null', transform([], fn () => 1));



assertType('int|null', rescue(fn () => 123));
<?php assertType('null', transform(null, fn () => 1));

assertType('null', transform('', fn () => 1));

assertType('null', transform([], fn () => 1));



assertType('int|null', rescue(fn () => 123));

assertType('int', rescue(fn () => 123, 345));