Zend\Code\Generator\MethodGenerator::setParameter PHP Method

setParameter() public method

public setParameter ( ParameterGenerator | array | string $parameter ) : MethodGenerator
$parameter ParameterGenerator | array | string
return MethodGenerator
    public function setParameter($parameter)
    {
        if (is_string($parameter)) {
            $parameter = new ParameterGenerator($parameter);
        }
        if (is_array($parameter)) {
            $parameter = ParameterGenerator::fromArray($parameter);
        }
        if (!$parameter instanceof ParameterGenerator) {
            throw new Exception\InvalidArgumentException(sprintf('%s is expecting either a string, array or an instance of %s\\ParameterGenerator', __METHOD__, __NAMESPACE__));
        }
        $this->parameters[$parameter->getName()] = $parameter;
        return $this;
    }

Usage Example

    /**
     * @param  State|\Scaffold\State $state
     * @return State|void
     */
    public function build(State $state)
    {
        $model = $state->getModel('options-factory');
        $generator = new ClassGenerator($model->getName());
        $generator->setImplementedInterfaces(['FactoryInterface']);
        $model->setGenerator($generator);
        $generator->addUse('Zend\\ServiceManager\\FactoryInterface');
        $generator->addUse('Zend\\ServiceManager\\ServiceLocatorInterface');
        $options = $state->getModel('options');
        $key = $options->getServiceName();
        $key = substr($key, 0, -7);
        $body = <<<EOF
\$config = \$serviceLocator->get('Config');
return new {$options->getClassName()}(
    isset(\$config['{$key}'])
        ? \$config['{$key}']
        : []
);
EOF;
        $method = new MethodGenerator('createService');
        $method->setParameter(new ParameterGenerator('serviceLocator', 'ServiceLocatorInterface'));
        $method->setBody($body);
        $doc = new DocBlockGenerator('');
        $doc->setTag(new Tag(['name' => 'inhertidoc']));
        $method->setDocBlock($doc);
        $generator->addMethodFromGenerator($method);
    }
All Usage Examples Of Zend\Code\Generator\MethodGenerator::setParameter