PHP code example of azjezz / psl

1. Go to this page and download the library: Download azjezz/psl 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/ */

    

azjezz / psl example snippets


use Psl\Type;

$userType = Type\shape([
    'name' => Type\non_empty_string(),
    'age'  => Type\positive_int(),
    'tags' => Type\vec(Type\string()),
]);

$user = $userType->coerce($untrustedInput);
// array{name: non-empty-string, age: positive-int, tags: list<string>}

use Psl\Async;
use Psl\TCP;
use Psl\IO;

Async\main(static function(): int {
    [$a, $b] = Async\concurrently([
        static fn() => TCP\connect('api.example.com', 443),
        static fn() => TCP\connect('db.example.com', 5432),
    ]);

    IO\write_error_line('Both connections ready');

    return 0;
});

use Psl\Vec;
use Psl\Dict;
use Psl\Str;

$names = ['alice', 'bob', 'charlie'];

Vec\map($names, Str\uppercase(...));
// ['ALICE', 'BOB', 'CHARLIE']

Vec\filter($names, fn($n) => Str\length($n) > 3);
// ['alice', 'charlie']

Dict\pull($names, Str\uppercase(...), fn($n) => $n);
// {alice: 'ALICE', bob: 'BOB', charlie: 'CHARLIE'}

use Psl\Async;
use Psl\TCP;
use Psl\IO;

Async\main(static function(): int {
    $server = TCP\listen('127.0.0.1', 8080);
    IO\write_error_line('Listening on :8080');

    while (true) {
        $conn = $server->accept();
        Async\run(static function() use ($conn) {
            $conn->writeAll("Hello!\n");
            $conn->close();
        })->ignore();
    }
});
shell
composer