fromArray() public static method

Generate from array
public static fromArray ( array $array ) : MethodGenerator
$array array
return MethodGenerator
コード例 #1
0
 /**
  * @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
0
 /**
  * @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
0
 /**
  * @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
0
 /**
  * @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
0
 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
0
 /**
  * @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
0
 /**
  * @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
0
ファイル: GetterAssembler.php プロジェクト: phpro/soap-client
 /**
  * @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
0
 /**
  * @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
0
 /**
  * @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
0
 /**
  * @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
0
 /**
  * @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
0
ファイル: MigrationManager.php プロジェクト: naxel/zfctool
 /**
  * @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
0
ファイル: ClassGenerator.php プロジェクト: brikou/zend_code
 /**
  *
  *
  * @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
0
 /**
  * @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;
 }
コード例 #16
0
 public function testCreateFromArray()
 {
     $methodGenerator = MethodGenerator::fromArray(array('name' => 'SampleMethod', 'body' => 'foo', 'docblock' => array('shortdescription' => 'foo'), 'abstract' => true, 'final' => true, 'static' => true, 'visibility' => MethodGenerator::VISIBILITY_PROTECTED));
     $this->assertEquals('SampleMethod', $methodGenerator->getName());
     $this->assertEquals('foo', $methodGenerator->getBody());
     $this->assertInstanceOf('Zend\\Code\\Generator\\DocBlockGenerator', $methodGenerator->getDocBlock());
     $this->assertTrue($methodGenerator->isAbstract());
     $this->assertTrue($methodGenerator->isFinal());
     $this->assertTrue($methodGenerator->isStatic());
     $this->assertEquals(MethodGenerator::VISIBILITY_PROTECTED, $methodGenerator->getVisibility());
 }
コード例 #17
0
 /**
  * @param ClassGenerator $class
  * @param Property       $firstProperty
  *
  * @throws \Zend\Code\Generator\Exception\InvalidArgumentException
  */
 private function implementGetIterator($class, $firstProperty)
 {
     $methodName = 'getIterator';
     $class->removeMethod($methodName);
     $class->addMethodFromGenerator(MethodGenerator::fromArray(['name' => $methodName, 'parameters' => [], 'visibility' => MethodGenerator::VISIBILITY_PUBLIC, 'body' => sprintf('return new \\ArrayIterator(is_array($this->%1$s) ? $this->%1$s : []);', $firstProperty->getName()), 'docblock' => DocBlockGenerator::fromArray(['tags' => [['name' => 'return', 'description' => '\\ArrayIterator']]])]));
 }
コード例 #18
0
 /**
  * Method for create method __construct
  *
  * @return MethodGenerator
  */
 private function createMethodConstruct()
 {
     return MethodGenerator::fromArray(['name' => '__construct', 'body' => PHP_EOL . 'use Message;' . PHP_EOL . PHP_EOL . $this->getInputs(), 'docblock' => $this->getDocBlockMethodConstruct()]);
 }
コード例 #19
0
 /**
  * @param ReflectionMethod $method
  * @return MethodGenerator
  */
 private function getMethodDetails(ReflectionMethod $method) : MethodGenerator
 {
     $name = $method->getName();
     $methodDetails = array("name" => $name, "docblock" => array("shortDescription" => '{@inheritdoc}'));
     $paramNames = array();
     $params = array();
     foreach ($method->getParameters() as $param) {
         $params[] = $this->getMethodParamDetails($param);
         $paramNames[] = '$' . $param->getName();
     }
     $methodDetails["parameters"] = $params;
     if (count($params) === 0) {
         $bodyMethodCall = sprintf('%s()', $name);
     } else {
         $bodyMethodCall = sprintf('%1$s(%2$s)', $name, implode(', ', $paramNames));
     }
     $methodDetails["body"] = sprintf('return $this->_getInstance()->%s;', $bodyMethodCall);
     $newMethod = MethodGenerator::fromArray($methodDetails);
     if ($method->hasReturnType()) {
         $returnType = $method->getReturnType();
         if ($returnType === null) {
             $newMethod->setReturnType(new ValueGenerator(null, ValueGenerator::TYPE_NULL));
         } else {
             $newMethod->setReturnType((string) $returnType);
         }
     }
     return $newMethod;
 }
コード例 #20
0
 /**
  * @param \Protobuf\Compiler\Entity $entity
  *
  * @return string
  */
 protected function generateRegisterAllExtensionsMethod(Entity $entity)
 {
     $lines = [];
     $fields = [];
     $descriptor = $entity->getDescriptor();
     $extensions = $descriptor->getExtensionList() ?: [];
     $messages = $descriptor->getMessageTypeList() ?: [];
     foreach ($messages as $message) {
         if (!$message->hasExtensionList()) {
             continue;
         }
         foreach ($message->getExtensionList() as $extension) {
             $fields[] = $extension;
         }
     }
     foreach ($fields as $field) {
         $type = $field->getTypeName();
         $name = $this->getCamelizedName($field);
         $ref = $this->getEntity($type);
         $class = $ref->getNamespacedName();
         $lines[] = '$registry->add(' . $class . '::' . $name . '());';
     }
     foreach ($extensions as $field) {
         $type = $field->getTypeName();
         $name = $this->getCamelizedName($field);
         $lines[] = '$registry->add(self::' . $name . '());';
     }
     $body = implode(PHP_EOL, $lines);
     $method = MethodGenerator::fromArray(['static' => true, 'body' => $body, 'name' => 'registerAllExtensions', 'parameters' => [['name' => 'registry', 'type' => '\\Protobuf\\Extension\\ExtensionRegistry']], 'docblock' => ['shortDescription' => "Register all extensions", 'tags' => [['name' => 'param', 'description' => '\\Protobuf\\Extension\\ExtensionRegistry']]]]);
     return $method;
 }
コード例 #21
0
 /**
  * Method for create method __construct
  *
  * @return MethodGenerator
  */
 private function createMethodConstruct()
 {
     return MethodGenerator::fromArray(['name' => '__construct', 'body' => $this->getConstructContent(), 'docblock' => $this->getDocBlockMethodConstruct()]);
 }
コード例 #22
0
 /**
  * Method for create method __construct
  *
  * @return MethodGenerator
  */
 private function createMethodPrepareElements()
 {
     return MethodGenerator::fromArray(['name' => 'prepareElements', 'body' => $this->getPrepareElementsContent(), 'docblock' => $this->getDocBlockMethodPrepareElements()]);
 }
コード例 #23
0
ファイル: Generator.php プロジェクト: naxel/classes-generator
    /**
     * @param string $modelName
     * @return array
     */
    protected function getMethodsForCollection($modelName)
    {
        return array(MethodGenerator::fromArray(array('name' => 'add', 'parameters' => array(lcfirst($modelName)), 'body' => '
if (is_array($' . lcfirst($modelName) . ')) {
    $this->collection[] = new ' . ucfirst($modelName) . '($' . lcfirst($modelName) . ');
} elseif (is_object($' . lcfirst($modelName) . ') && $' . lcfirst($modelName) . ' instanceof ' . ucfirst($modelName) . ') {
    $this->collection[] = $' . lcfirst($modelName) . ';
}

return $this;
', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Add item', 'longDescription' => null, new Tag\ParamTag($modelName, ucfirst($modelName)), new Tag\ReturnTag(array('datatype' => '$this')))))), MethodGenerator::fromArray(array('name' => 'getAll', 'body' => 'return $this->collection;', 'docblock' => DocBlockGenerator::fromArray(array('shortDescription' => 'Get items', 'longDescription' => null, new Tag\ParamTag($modelName, ucfirst($modelName)), new Tag\ReturnTag(array('datatype' => '$this')))))));
    }
コード例 #24
0
 /**
  * Method for create method __construct
  *
  * @return MethodGenerator
  */
 private function createMethodConstruct()
 {
     $arrParameters = array_keys($this->arrForeignKeys);
     return MethodGenerator::fromArray(['name' => '__construct', 'parameters' => $arrParameters, 'body' => $this->getInputs(), 'docblock' => $this->getDocBlockMethodConstruct()]);
 }