本文整理匯總了PHP中Zend\Code\Generator\MethodGenerator::fromArray方法的典型用法代碼示例。如果您正苦於以下問題:PHP MethodGenerator::fromArray方法的具體用法?PHP MethodGenerator::fromArray怎麽用?PHP MethodGenerator::fromArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Zend\Code\Generator\MethodGenerator
的用法示例。
在下文中一共展示了MethodGenerator::fromArray方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: generateMethod
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return string
*/
protected function generateMethod(Entity $entity)
{
$lines = $this->generateBody($entity);
$body = implode(PHP_EOL, $lines);
$method = MethodGenerator::fromArray(['name' => 'fromArray', 'body' => $body, 'static' => true, 'parameters' => [['name' => 'values', 'type' => 'array']], 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
return $method;
}
示例2: generateMethod
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return MethodGenerator
*/
protected function generateMethod(Entity $entity)
{
$lines = $this->generateBody($entity);
$body = implode(PHP_EOL, $lines);
$method = MethodGenerator::fromArray(['name' => 'merge', 'body' => $body, 'parameters' => [['name' => 'message', 'type' => '\\Protobuf\\Message']], 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
return $method;
}
示例3: generateConstructorMethod
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return \Zend\Code\Generator\GeneratorInterface
*/
protected function generateConstructorMethod(Entity $entity)
{
$lines = $this->generateBody($entity);
$body = implode(PHP_EOL, $lines);
$method = MethodGenerator::fromArray(['name' => '__construct', 'body' => $body, 'parameters' => [['name' => 'stream', 'type' => 'mixed', 'defaultValue' => null], ['name' => 'configuration', 'type' => '\\Protobuf\\Configuration', 'defaultValue' => null]], 'docblock' => ['shortDescription' => '{@inheritdoc}']]);
return $method;
}
示例4: generateClearMethod
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return \Zend\Code\Generator\GeneratorInterface
*/
protected function generateClearMethod(Entity $entity)
{
$lines = $this->generateBody($entity);
$body = implode(PHP_EOL, $lines);
$method = MethodGenerator::fromArray(['name' => 'clear', 'body' => $body, 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
return $method;
}
示例5: generate
private function generate($version)
{
$generator = new ClassGenerator();
$docblock = DocBlockGenerator::fromArray(array('shortDescription' => 'PDO Simple Migration Class', 'longDescription' => 'Add your queries below'));
$generator->setName('PDOSimpleMigration\\Migrations\\Migration' . $version)->setExtendedClass('AbstractMigration')->addUse('PDOSimpleMigration\\Library\\AbstractMigration')->setDocblock($docblock)->addProperties(array(array('description', 'Migration description', PropertyGenerator::FLAG_STATIC)))->addMethods(array(MethodGenerator::fromArray(array('name' => 'up', 'parameters' => array(), 'body' => '//$this->addSql(/*Sql instruction*/);', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Migrate up', 'longDescription' => null)))), MethodGenerator::fromArray(array('name' => 'down', 'parameters' => array(), 'body' => '//$this->addSql(/*Sql instruction*/);', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Migrate down', 'longDescription' => null))))));
$file = FileGenerator::fromArray(array('classes' => array($generator)));
return $file->generate();
}
示例6: generateMethod
/**
* @param \Protobuf\Compiler\Entity $entity
* @param \google\protobuf\MethodDescriptorProto $method
*
* @return string
*/
protected function generateMethod(Entity $entity, MethodDescriptorProto $method)
{
$inputClass = $this->getMethodInputTypeHint($method);
$inputDoc = $this->getMethodInputDocblock($method);
$outputDoc = $this->getMethodOutputDocblock($method);
$methodName = $this->getCamelizedValue($method->getName());
$method = MethodGenerator::fromArray(['name' => $methodName, 'parameters' => [['name' => 'input', 'type' => $inputClass]], 'docblock' => ['tags' => [['name' => 'param', 'description' => $inputDoc . ' $input'], ['name' => 'return', 'description' => $outputDoc]]]]);
return $method;
}
示例7: implementGetResult
/**
* @param ContextInterface $context
* @param ClassGenerator $class
* @param Property $property
*
* @throws \Zend\Code\Generator\Exception\InvalidArgumentException
*/
private function implementGetResult(ContextInterface $context, ClassGenerator $class, Property $property)
{
$useAssembler = new UseAssembler($this->wrapperClass ?: ResultInterface::class);
if ($useAssembler->canAssemble($context)) {
$useAssembler->assemble($context);
}
$methodName = 'getResult';
$class->removeMethod($methodName);
$class->addMethodFromGenerator(MethodGenerator::fromArray(['name' => $methodName, 'parameters' => [], 'visibility' => MethodGenerator::VISIBILITY_PUBLIC, 'body' => $this->generateGetResultBody($property), 'docblock' => DocBlockGenerator::fromArray(['tags' => [['name' => 'return', 'description' => $this->generateGetResultReturnTag($property)]]])]));
}
示例8: assemble
/**
* @param ContextInterface|PropertyContext $context
*
* @throws AssemblerException
*/
public function assemble(ContextInterface $context)
{
$class = $context->getClass();
$property = $context->getProperty();
try {
$methodName = Normalizer::generatePropertyMethod('get', $property->getName());
$class->removeMethod($methodName);
$class->addMethodFromGenerator(MethodGenerator::fromArray(['name' => $methodName, 'parameters' => [], 'visibility' => MethodGenerator::VISIBILITY_PUBLIC, 'body' => sprintf('return $this->%s;', $property->getName()), 'docblock' => DocBlockGenerator::fromArray(['tags' => [['name' => 'return', 'description' => $property->getType()]]])]));
} catch (\Exception $e) {
throw AssemblerException::fromException($e);
}
}
示例9: assembleConstructor
/**
* @param Type $type
*
* @return MethodGenerator
* @throws \Zend\Code\Generator\Exception\InvalidArgumentException
*/
private function assembleConstructor(Type $type)
{
$body = [];
$constructor = MethodGenerator::fromArray(['name' => '__construct', 'visibility' => MethodGenerator::VISIBILITY_PUBLIC]);
$docblock = DocBlockGenerator::fromArray(['shortdescription' => 'Constructor']);
foreach ($type->getProperties() as $property) {
$body[] = sprintf('$this->%1$s = $%1$s;', $property->getName());
$constructor->setParameter(['name' => $property->getName()]);
$docblock->setTag(['name' => 'var', 'description' => sprintf('%s $%s', $property->getType(), $property->getName())]);
}
$constructor->setDocBlock($docblock);
$constructor->setBody(implode($constructor::LINE_FEED, $body));
return $constructor;
}
示例10: generateProxyMethod
/**
* @return MethodGenerator
*/
private function generateProxyMethod(\ReflectionMethod $method, $preSource, $postSource, $exceptionSource)
{
$methodReflection = new MethodReflection($method->getDeclaringClass()->getName(), $method->getName());
if ('__construct' === $methodReflection->getName()) {
$methodGenerator = MethodGenerator::fromArray(['name' => $methodReflection->getName(), 'body' => '']);
} else {
$methodGenerator = MethodGenerator::fromReflection($methodReflection);
$parametersString = '(';
$i = count($method->getParameters());
foreach ($method->getParameters() as $parameter) {
$parametersString .= '$' . $parameter->getName() . (--$i > 0 ? ',' : '');
}
$parametersString .= ')';
if ('' === $preSource && '' === $postSource && '' === $exceptionSource) {
$body = 'return $this->proxy_realSubject->' . $method->getName() . $parametersString . ";\n";
} else {
$body = "try {\n" . $preSource . "\n" . '$data = $this->proxy_realSubject->' . $method->getName() . $parametersString . ";\n" . $postSource . "\n" . "return \$data;\n" . "} catch(\\Exception \$e){\n" . $exceptionSource . "\n" . "throw \$e;\n" . '};';
}
$methodGenerator->setBody($body);
}
return $methodGenerator;
}
示例11: generateValueOfMethod
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return string
*/
public function generateValueOfMethod(Entity $entity)
{
$body = [];
$descriptor = $entity->getDescriptor();
$class = $entity->getNamespacedName();
$values = $descriptor->getValueList() ?: [];
$body[] = 'switch ($value) {';
foreach ($values as $value) {
$name = $value->getName();
$number = $value->getNumber();
$body[] = ' case ' . $number . ': return self::' . $name . '();';
}
$body[] = ' default: return null;';
$body[] = '}';
$method = MethodGenerator::fromArray(['static' => true, 'name' => 'valueOf', 'body' => implode(PHP_EOL, $body), 'parameters' => [['name' => 'value', 'type' => 'int']], 'docblock' => ['tags' => [['name' => 'param', 'description' => 'int $value'], ['name' => 'return', 'description' => $class]]]]);
$method->getDocblock()->setWordWrap(false);
return $method;
}
示例12: generateGetExtensionsMethod
/**
* @param \Protobuf\Compiler\Entity $entity
*
* @return string
*/
protected function generateGetExtensionsMethod(Entity $entity)
{
$lines = [];
$descriptor = $entity->getDescriptor();
$fieldName = $this->getUniqueFieldName($descriptor, 'extensions');
$lines[] = 'if ( $this->' . $fieldName . ' !== null) {';
$lines[] = ' return $this->' . $fieldName . ';';
$lines[] = '}';
$lines[] = null;
$lines[] = 'return $this->' . $fieldName . ' = new \\Protobuf\\Extension\\ExtensionFieldMap(__CLASS__);';
return MethodGenerator::fromArray(['name' => 'extensions', 'body' => implode(PHP_EOL, $lines), 'docblock' => ['shortDescription' => "{@inheritdoc}"]]);
}
示例13: create
/**
* @param null|string $module
* @param null|string $migrationBody
* @return string
*/
public function create($module = null, $migrationBody = null)
{
$path = $this->getMigrationsDirectoryPath($module);
list(, $mSec) = explode(".", microtime(true));
$migrationName = date('Ymd_His_') . substr($mSec, 0, 2);
$methodUp = array('name' => 'up', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Upgrade', 'longDescription' => null)));
$methodDown = array('name' => 'down', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Degrade', 'longDescription' => null)));
if ($migrationBody) {
if (isset($migrationBody['up'])) {
$upBody = '';
foreach ($migrationBody['up'] as $query) {
$upBody .= '$this->query("' . $query . '");' . PHP_EOL;
}
$methodUp['body'] = $upBody;
}
if (isset($migrationBody['down'])) {
$downBody = '';
foreach ($migrationBody['down'] as $query) {
$downBody .= '$this->query("' . $query . '");' . PHP_EOL;
}
$methodDown['body'] = $downBody;
}
}
$class = new ClassGenerator();
$class->setName('Migration_' . $migrationName)->setExtendedClass('AbstractMigration')->addUse('ZFCTool\\Service\\Migration\\AbstractMigration')->addMethods(array(MethodGenerator::fromArray($methodUp), MethodGenerator::fromArray($methodDown)));
$file = new FileGenerator(array('classes' => array($class)));
$code = $file->generate();
$migrationPath = $path . '/' . $migrationName . '.php';
file_put_contents($migrationPath, $code);
return $migrationPath;
}
示例14: fromArray
/**
*
*
* @configkey name string [required] Class Name
* @configkey filegenerator FileGenerator File generator that holds this class
* @configkey namespacename string The namespace for this class
* @configkey docblock string The docblock information
* @configkey flags int Flags, one of ClassGenerator::FLAG_ABSTRACT ClassGenerator::FLAG_FINAL
* @configkey extendedclass string Class which this class is extending
* @configkey implementedinterfaces
* @configkey properties
* @configkey methods
*
*
* @static
* @throws Exception\InvalidArgumentException
* @param array $array
* @return ClassGenerator
*/
public static function fromArray(array $array)
{
if (!isset($array['name'])) {
throw new Exception\InvalidArgumentException('Class generator requires that a name is provided for this object');
}
$cg = new static($array['name']);
foreach ($array as $name => $value) {
// normalize key
switch (strtolower(str_replace(array('.', '-', '_'), '', $name))) {
case 'containingfile':
$cg->setContainingFileGenerator($value);
break;
case 'namespacename':
$cg->setNamespaceName($value);
break;
case 'docblock':
$cg->setDocblock(!$value instanceof DocblockGenerator ?: DocblockGenerator::fromArray($value));
break;
case 'flags':
$cg->setFlags($value);
break;
case 'extendedclass':
$cg->setExtendedClass($value);
break;
case 'implementedinterfaces':
$cg->setImplementedInterfaces($value);
break;
case 'properties':
foreach ($value as $pValue) {
$cg->setProperty(!$pValue instanceof PropertyGenerator ?: PropertyGenerator::fromArray($pValue));
}
break;
case 'methods':
foreach ($value as $mValue) {
$cg->setMethod(!$mValue instanceof MethodGenerator ?: MethodGenerator::fromArray($mValue));
}
break;
}
}
return $cg;
}
示例15: generateExtensionMethod
/**
* @param \Protobuf\Compiler\Entity $entity
* @param \google\protobuf\FieldDescriptorProto $field
*
* @return string
*/
protected function generateExtensionMethod(Entity $entity, FieldDescriptorProto $field)
{
$fieldName = $field->getName();
$methodName = $this->getCamelizedName($field);
$lines = $this->generateBody($entity, $field);
$body = implode(PHP_EOL, $lines);
$method = MethodGenerator::fromArray(['static' => true, 'body' => $body, 'name' => $methodName, 'docblock' => ['shortDescription' => "Extension field : {$fieldName}", 'tags' => [['name' => 'return', 'description' => '\\Protobuf\\Extension\\ExtensionField']]]]);
$method->getDocblock()->setWordWrap(false);
return $method;
}