File: multisearch.php

package info (click to toggle)
php-laravel-prompts 0.1.25-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: php: 5,928; xml: 14; makefile: 8
file content (43 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php

use function Laravel\Prompts\multisearch;

require __DIR__.'/../vendor/autoload.php';

$users = collect([
    'taylor' => 'Taylor Otwell',
    'dries' => 'Dries Vints',
    'james' => 'James Brooks',
    'nuno' => 'Nuno Maduro',
    'mior' => 'Mior Muhammad Zaki',
    'jess' => 'Jess Archer',
    'guus' => 'Guus Leeuw',
    'tim' => 'Tim MacDonald',
    'joe' => 'Joe Dixon',
]);

$selected = multisearch(
    label: 'Which users should receive the email?',
    placeholder: 'Search...',
    options: function ($value) use ($users) {
        // Comment to show all results by default.
        if (strlen($value) === 0) {
            return [];
        }

        usleep(100 * 1000); // Simulate a DB query.

        return $users->when(
            strlen($value),
            fn ($users) => $users->filter(fn ($name) => str_contains(strtolower($name), strtolower($value)))
        )->all();
    },
    required: true,
    validate: function ($values) {
        if (in_array('jess', $values)) {
            return 'Jess cannot receive emails';
        }
    },
);

var_dump($selected);