PHP code example of joe.szeto / capsule

1. Go to this page and download the library: Download joe.szeto/capsule library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

joe.szeto / capsule example snippets


$me = capsule(
    #[Setter('name')]
    fn() => 'szeto',
    #[Setter('age')]
    fn() => 30,
    #[Setter('sex')]
    fn() => 'man'
)->thenReturn(fn(string $name, int $age, string $sex) => [ $name, $age, $sex ]);
// to be ['szeto', 30, 'man']

$me = capsule()
    ->set('name', fn() => 'szeto')
    ->set('age', fn() => 30)
    ->set('sex', fn() => 'man')
    ->thenReturn(fn(string $name, int $age, string $sex) => [ $name, $age, $sex ]);
// to be ['szeto', 30, 'man']

$name = capsule()
    ->set('name', 'szeto')
    ->thenReturn(fn(string $myName) => $myName);

capsule()
    ->thenReturn(fn(WhenEmpty $empty) => expect($empty)->toBeInstanceOf(WhenEmpty::class));

$name = capsule()
    ->set('name', fn() => null)
    ->through(
        #[OnBlank('name'), Setter('name')]
        fn() => 'szeto' // this closure only call when the value of 'name' is null
    )->thenReturn('name');

 $name = capsule()
        ->set('name', fn() => null)
        ->through(
            #[SetOnBlank('name')]
            fn() => 'szeto'
        )
        ->thenReturn('name');

$name = capsule()
    ->set('name', fn() => null)
    ->setOnBlank('name', fn() => 'szeto')
    ->thenReturn('name');

    $name = capsule()
        ->set('name', fn() => 'szeto')
        ->thenReturn(fn(Closure $name) => $name());
    expect($name)->toBe('szeto');

    $name = capsule()
        ->set('name', fn() => 'szeto')
        ->thenReturn(
            fn(string $name) => $name // now name is szeto, not a closure
        );
    expect($name)->toBe('szeto');

    $name = capsule()
        ->set('prefix', fn() => 'Joe')
        ->set('name', fn(string $prefix) => $prefix. ' szeto')
        ->thenReturn(
            fn(Closure $name, string $prefix) => $name($prefix) // params is Joe
        );

capsule()
    ->set('prefix', fn() => 'Joe')
    ->set('name', fn(string $prefix) => $prefix. ' szeto')
    ->thenReturn(
        fn(Evaluable $name) => $name() // params is Joe
    );

$names = capsule()
    ->set('names', fn() => ['szeto', 'joe'])
    ->through(
        #[Each('names' as: 'name')]
        fn(string $name) => $name // 'szeto', 'joe'
    )->run();

capsule()
    ->through(
        fn() => throw new Exception('should not run'),
        #[Only]
        fn() => expect(true)->toBeTrue()
    )
    ->run();

capsule()
    ->through(
        fn() => expect(true)->toBeTrue(),
        #[Skip]
        fn() => throw new Exception('should not run'),
    )
    ->run();

public static function mock(string $key, mixed $value): void

Capsule::mock('name', 'szeto');

Capsule::mock('name', fn() => 'szeto');

Capsule::mock('name', new OnBlank('szeto'));

Capsule::mock(OnBlank::class, new Sequence(
    new OnBlank('szeto'), new OnBlank('joe')
));
capsule()
    ->through(
        fn(OnBlank $name) => expect($name->getKey())->toBe('szeto'),
        fn(OnBlank $name) => expect($name->getKey())->toBe('joe')
    )->run();

    capsule(
        fn() => throw new Exception('foo'),
        #[Cat(Exception::class)]
        fn($message) => expect($message)->toBe('foo')
    )->run();

    capsule()
        ->namespace('some:namespace')
        ->set('name', 'szeto')->run();

    capsule()
        ->namespace('some:namespace')
        ->through(
            fn(string $name) => expect($name)->toBe('szeto')
        )->run();

    $capsule = capsule()->through(
        #[Setter('name')]
        fn() => 'szeto'
    );

    $name = $capsule->append(
        #[Setter('name')]
        fn($name) => 'joe ' . $name
    )->thenReturn('name');

    expect($name)->toBe('joe szeto');

    capsule()->namespace('abc:foo')->append(
        #[Setter('name')]
        fn(string $name) => 'joe ' . $name
    );

    $name = capsule()
        ->namespace('abc:foo')
        ->through(
            #[Setter('name')]
            fn() => 'szeto'
        )->thenReturn('name');

    // now name is joe szeto

return capsule()
    ->namespace($namespace)
    ->set($data)
    ->thenReturn($return);

return massage($namespace, $data, $return);

return massage($namespace, $data);

massage(Foo::class.':handle:name', ['name' => 'szeto']);

capsule()
    ->namespace(Foo::class.':handle:name')
    ->set('name', 'szeto')
    ->thenReturn('name');