1338/smart-cli-select

v1.2.1 2021-11-11 13:15 UTC

This package is auto-updated.

Last update: 2024-11-11 20:31:35 UTC


README

Symfony selection helper

Select array/objects while being able to add new options

How to call:

    $smartCliSelect = new SmartCliSelect($inputInterface, $outputInterface, $questionHelper);
    
    $result = $smartSelector->smartSelect(
        "Question",
        ["preselected choices indexes"],
        ["choices"],
        false, // force preselection choices
        ["structure to create new options"]
    );

examples

Object

    $result = $smartSelector->smartSelect(
        "Select products",
        [$allProductsIndexedBySkus[0]],
        $allProductsIndexedBySkus,
        false, // force preselection choices
        ['type' => 'object']
    );

    $selectedProductIndices = $result['selected'];
    
    foreach ($result['new'] => $newProduct) {
        // do something to new objects, like for example:
        $this->entityManager->persist($newProduct);
        // 
    }
    
    $allProductsIndexedBySkus += $result['new']; // add new products 

    $selectedProducts = [];
    foreach ($selectedProductIndices as $index) {
        $selectedProducts[$index] = $allProductsIndexedBySkus[$index];
    }
    

Object with a subset of options to be filled

    $result = $smartSelector->smartSelect(
        "Select products",
        [$allProductsIndexedBySkus[0]],
        $allProductsIndexedBySkus,
        false, // force preselection choices
        [
            'type' => 'object',
            'options' => [
                'name', 'sku'
            ]
        ]
    );

    $selectedProductIndices = $result['selected'];
    
    foreach ($result['new'] => $newProduct) {
        // do something to new objects, like for example:
        $this->entityManager->persist($newProduct);
        // 
    }
    
    $allProductsIndexedBySkus += $result['new']; // add new products 

    $selectedProducts = [];
    foreach ($selectedProductIndices as $index) {
        $selectedProducts[$index] = $allProductsIndexedBySkus[$index];
    }

Array

            $hostResults = $smartSelector->smartSelect(
            'Select hosts',
            array_keys($this->instances),
            $this->input->getOption('host'),
            false, // force preselection choices
            [
                'options' => ['host', 'username', 'pass', 'port']
            ]
        );