'Change the method to allow pass different values as parameters for the same variable

I have a method like below and it's working I have a $fields variable that then I need to merge it to another array.

public function definition(): array
    {

           $fields  = 
            NovaDependencyContainer::make([

                $this->listDefinition(
                    ‘Field’,
                    $this->getFields(),
                    2 // here can be 2 or 4
                )])
            ->dependsOn(‘is’_active, 0)
            ->meta()['fields'][0];

           return array_merge(
            [
                …
            ],
            $fields
        );
    }

However I want to have some way of having 2 variables $fields. Basically have something like below, which of course as it is doesn't work:

public function definition(): array
        {
    
               $fields  = 
                NovaDependencyContainer::make([
    
                    $this->listDefinition(
                        ‘Field’,
                        $this->getFields(),
                        2 // here can be 2 or 4
                    )])
                ->dependsOn(‘is’_active, 0)
                ->meta()['fields'][0];


               $fields  = 
                NovaDependencyContainer::make([
    
                    $this->listDefinition(
                        ‘Field’,
                        $this->getFields(),
                        4
                    )])
                ->dependsOn(‘is’_active, 1)
                ->meta()['fields'][0];

               return array_merge(
                [
                    …
                ],
                $fields
            );
        }

The only difference is the 2 or 4, that's the number of times the field will appear. And the other difference is the "dependsOn(‘is’_active, 1)" it can be 1 or 0 "dependsOn('is_checked', 0)".

I'm using a package "Epartment\NovaDependencyContainer" and basically if the checkbox "Is Checked" is selected I want to have "4", because with "4" it will show the fields 4 times otherwise I want to have 2.

Do you know how to properly change the method to allow for that?



Solution 1:[1]

Here you go

    function definition(int $fieldCount): array
    {
        
        $fields  = NovaDependencyContainer::make([$this->listDefinition('Field', $this->getFields(), $fieldCount)])
                ->dependsOn('is_active', ($fieldCount == 4 ? 1 : 0))
                ->meta()['fields'][0];
    
        return array_merge(
            [
                …
            ],
            $fields
        );
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gguney