/**
  * @return ConcreteMethod
  */
 protected function generateConstructor()
 {
     $this->typeCheck->generateConstructor(func_get_args());
     $parameterNameIdentifier = new Identifier('parameterName');
     $parameterNameVariable = new Variable($parameterNameIdentifier);
     $indexIdentifier = new Identifier('index');
     $indexVariable = new Variable($indexIdentifier);
     $expectedTypeIdentifier = new Identifier('expectedType');
     $expectedTypeVariable = new Variable($expectedTypeIdentifier);
     $previousIdentifier = new Identifier('previous');
     $thisVariable = new Variable(new Identifier('this'));
     $thisParameterNameMember = new Member($thisVariable, new Constant($parameterNameIdentifier));
     $thisIndexMember = new Member($thisVariable, new Constant($indexIdentifier));
     $thisExpectedTypeMember = new Member($thisVariable, new Constant($expectedTypeIdentifier));
     $method = new ConcreteMethod(new Identifier('__construct'), AccessModifier::PUBLIC_());
     $method->addParameter(new Parameter($parameterNameIdentifier));
     $method->addParameter(new Parameter($indexIdentifier));
     $method->addParameter(new Parameter($expectedTypeIdentifier));
     $previousParameter = new Parameter($previousIdentifier, new ObjectTypeHint(QualifiedIdentifier::fromString('\\Exception')));
     $previousParameter->setDefaultValue(new Literal(null));
     $method->addParameter($previousParameter);
     $method->statementBlock()->add(new ExpressionStatement(new Assign($thisParameterNameMember, $parameterNameVariable)));
     $method->statementBlock()->add(new ExpressionStatement(new Assign($thisIndexMember, $indexVariable)));
     $method->statementBlock()->add(new ExpressionStatement(new Assign($thisExpectedTypeMember, $expectedTypeVariable)));
     $sprintfCall = new Call(QualifiedIdentifier::fromString('\\sprintf'));
     $sprintfCall->add(new Literal("Missing argument for parameter '%s' at index %d. Expected '%s'."));
     $sprintfCall->add($parameterNameVariable);
     $sprintfCall->add($indexVariable);
     $sprintfCall->add($expectedTypeVariable);
     $parentConstructCall = new Call(new StaticMember(new Constant(new Identifier('parent')), new Constant(new Identifier('__construct'))));
     $parentConstructCall->add($sprintfCall);
     $parentConstructCall->add(new Variable($previousIdentifier));
     $method->statementBlock()->add(new ExpressionStatement($parentConstructCall));
     return $method;
 }
Пример #2
0
 /**
  * @return ConcreteMethod
  */
 protected function generateTypeMethod()
 {
     $this->typeCheck->generateTypeMethod(func_get_args());
     $valueIdentifier = new Identifier('value');
     $valueVariable = new Variable($valueIdentifier);
     $maxIterationsIdentifier = new Identifier('maxIterations');
     $maxIterationsVariable = new Variable($maxIterationsIdentifier);
     $nativeTypeVariable = new Variable(new Identifier('nativeType'));
     $thisVariable = new Variable(new Identifier('this'));
     $method = new ConcreteMethod(new Identifier('type'), AccessModifier::PUBLIC_());
     $method->addParameter(new Parameter($valueIdentifier));
     $maxIterationsParameter = new Parameter($maxIterationsIdentifier);
     $maxIterationsParameter->setDefaultValue(new Literal(10));
     $method->addParameter($maxIterationsParameter);
     $gettypeCall = new Call(QualifiedIdentifier::fromString('\\gettype'));
     $gettypeCall->add($valueVariable);
     $method->statementBlock()->add(new ExpressionStatement(new Assign($nativeTypeVariable, $gettypeCall)));
     $arrayTypeIf = new IfStatement(new StrictEquals(new Literal('array'), $nativeTypeVariable));
     $arrayTypeCall = new Call(new Member($thisVariable, new Constant(new Identifier('arrayType'))));
     $arrayTypeCall->add($valueVariable);
     $arrayTypeCall->add($maxIterationsVariable);
     $arrayTypeIf->trueBranch()->add(new ReturnStatement($arrayTypeCall));
     $method->statementBlock()->add($arrayTypeIf);
     $doubleTypeIf = new IfStatement(new StrictEquals(new Literal('double'), $nativeTypeVariable));
     $doubleTypeIf->trueBranch()->add(new ReturnStatement(new Literal('float')));
     $method->statementBlock()->add($doubleTypeIf);
     $nullTypeIf = new IfStatement(new StrictEquals(new Literal('NULL'), $nativeTypeVariable));
     $nullTypeIf->trueBranch()->add(new ReturnStatement(new Literal('null')));
     $method->statementBlock()->add($nullTypeIf);
     $objectTypeIf = new IfStatement(new StrictEquals(new Literal('object'), $nativeTypeVariable));
     $objectTypeCall = new Call(new Member($thisVariable, new Constant(new Identifier('objectType'))));
     $objectTypeCall->add($valueVariable);
     $objectTypeCall->add($maxIterationsVariable);
     $objectTypeIf->trueBranch()->add(new ReturnStatement($objectTypeCall));
     $method->statementBlock()->add($objectTypeIf);
     $resourceTypeIf = new IfStatement(new StrictEquals(new Literal('resource'), $nativeTypeVariable));
     $resourceTypeCall = new Call(new Member($thisVariable, new Constant(new Identifier('resourceType'))));
     $resourceTypeCall->add($valueVariable);
     $resourceTypeIf->trueBranch()->add(new ReturnStatement($resourceTypeCall));
     $method->statementBlock()->add($resourceTypeIf);
     $method->statementBlock()->add(new ReturnStatement($nativeTypeVariable));
     return $method;
 }
 /**
  * @param RuntimeConfiguration $configuration
  *
  * @return ConcreteMethod
  */
 protected function generateConstructor(RuntimeConfiguration $configuration)
 {
     $this->typeCheck->generateConstructor(func_get_args());
     $indexIdentifier = new Identifier('index');
     $indexVariable = new Variable($indexIdentifier);
     $valueIdentifier = new Identifier('value');
     $valueVariable = new Variable($valueIdentifier);
     $previousIdentifier = new Identifier('previous');
     $previousVariable = new Variable($previousIdentifier);
     $typeInspectorIdentifier = new Identifier('typeInspector');
     $typeInspectorVariable = new Variable($typeInspectorIdentifier);
     $thisVariable = new Variable(new Identifier('this'));
     $thisIndexMember = new Member($thisVariable, new Constant($indexIdentifier));
     $thisValueMember = new Member($thisVariable, new Constant($valueIdentifier));
     $thisTypeInspectorMember = new Member($thisVariable, new Constant($typeInspectorIdentifier));
     $thisUnexpectedTypeMember = new Member($thisVariable, new Constant(new Identifier('unexpectedType')));
     $typeInspectorClassName = QualifiedIdentifier::fromString($configuration->validatorNamespace()->joinAtoms('TypeInspector')->string());
     $method = new ConcreteMethod(new Identifier('__construct'), AccessModifier::PUBLIC_());
     $method->addParameter(new Parameter($indexIdentifier));
     $method->addParameter(new Parameter($valueIdentifier));
     $previousParameter = new Parameter($previousIdentifier, new ObjectTypeHint(QualifiedIdentifier::fromString('\\Exception')));
     $previousParameter->setDefaultValue(new Literal(null));
     $method->addParameter($previousParameter);
     $typeInspectorParameter = new Parameter($typeInspectorIdentifier, new ObjectTypeHint($typeInspectorClassName));
     $typeInspectorParameter->setDefaultValue(new Literal(null));
     $method->addParameter($typeInspectorParameter);
     $nullTypeInspectorIf = new IfStatement(new StrictEquals(new Literal(null), $typeInspectorVariable));
     $nullTypeInspectorIf->trueBranch()->add(new ExpressionStatement(new Assign($typeInspectorVariable, new NewOperator(new Call($typeInspectorClassName)))));
     $method->statementBlock()->add($nullTypeInspectorIf);
     $method->statementBlock()->add(new ExpressionStatement(new Assign($thisIndexMember, $indexVariable)));
     $method->statementBlock()->add(new ExpressionStatement(new Assign($thisValueMember, $valueVariable)));
     $method->statementBlock()->add(new ExpressionStatement(new Assign($thisTypeInspectorMember, $typeInspectorVariable)));
     $typeInspectorTypeCall = new Call(new Member($typeInspectorVariable, new Constant(new Identifier('type'))));
     $typeInspectorTypeCall->add($thisValueMember);
     $method->statementBlock()->add(new ExpressionStatement(new Assign($thisUnexpectedTypeMember, $typeInspectorTypeCall)));
     $sprintfCall = new Call(QualifiedIdentifier::fromString('\\sprintf'));
     $sprintfCall->add(new Literal("Unexpected argument of type '%s' at index %d."));
     $sprintfCall->add($thisUnexpectedTypeMember);
     $sprintfCall->add($indexVariable);
     $parentConstructCall = new Call(new StaticMember(new Constant(new Identifier('parent')), new Constant(new Identifier('__construct'))));
     $parentConstructCall->add($sprintfCall);
     $parentConstructCall->add(new Variable($previousIdentifier));
     $method->statementBlock()->add(new ExpressionStatement($parentConstructCall));
     return $method;
 }
Пример #4
0
 /**
  * @param RuntimeConfiguration $configuration
  *
  * @return ConcreteMethod
  */
 protected function generateDefineValidatorMethod(RuntimeConfiguration $configuration)
 {
     $this->typeCheck->generateDefineValidatorMethod(func_get_args());
     $classNameIdentifier = new Identifier('className');
     $classNameVariable = new Variable($classNameIdentifier);
     $classGeneratorIdentifier = new Identifier('classGenerator');
     $classGeneratorVariable = new Variable($classGeneratorIdentifier);
     $classGeneratorClassIdentifier = QualifiedIdentifier::fromString('\\Eloquent\\Typhoon\\Generator\\ValidatorClassGenerator');
     $method = new ConcreteMethod(new Identifier('defineValidator'), AccessModifier::PROTECTED_(), true);
     $method->addParameter(new Parameter($classNameIdentifier));
     $classGeneratorParameter = new Parameter($classGeneratorIdentifier, new ObjectTypeHint($classGeneratorClassIdentifier));
     $classGeneratorParameter->setDefaultValue(new Literal(null));
     $method->addParameter($classGeneratorParameter);
     $nullClassGeneratorIf = new IfStatement(new StrictEquals(new Literal(null), $classGeneratorVariable));
     $nullClassGeneratorIf->trueBranch()->add(new ExpressionStatement(new Assign($classGeneratorVariable, new NewOperator($classGeneratorClassIdentifier))));
     $method->statementBlock()->add($nullClassGeneratorIf);
     $evalCall = new Call(QualifiedIdentifier::fromString('eval'));
     $generateFromClassCall = new Call(new Member($classGeneratorVariable, new Constant(new Identifier('generateFromClass'))));
     $generateFromClassCall->add(new Call(new StaticMember(new Constant(new Identifier('static')), new Constant(new Identifier('configuration')))));
     $newReflectorCall = new Call(QualifiedIdentifier::fromString('\\ReflectionClass'));
     $newReflectorCall->add($classNameVariable);
     $newReflector = new NewOperator($newReflectorCall);
     $generateFromClassCall->add($newReflector);
     $evalCall->add(new Concat(new Literal('?>'), $generateFromClassCall));
     $method->statementBlock()->add(new ExpressionStatement($evalCall));
     return $method;
 }
 /**
  * @return ConcreteMethod
  */
 protected function generateConstructor()
 {
     $this->typeCheck->generateConstructor(func_get_args());
     $messageIdentifier = new Identifier('message');
     $previousIdentifier = new Identifier('previous');
     $method = new ConcreteMethod(new Identifier('__construct'), AccessModifier::PUBLIC_());
     $method->addParameter(new Parameter($messageIdentifier));
     $previousParameter = new Parameter($previousIdentifier, new ObjectTypeHint(QualifiedIdentifier::fromString('\\Exception')));
     $previousParameter->setDefaultValue(new Literal(null));
     $method->addParameter($previousParameter);
     $parentConstructCall = new Call(new StaticMember(new Constant(new Identifier('parent')), new Constant(new Identifier('__construct'))));
     $parentConstructCall->add(new Variable($messageIdentifier));
     $parentConstructCall->add(new Literal(0));
     $parentConstructCall->add(new Variable($previousIdentifier));
     $method->statementBlock()->add(new ExpressionStatement($parentConstructCall));
     return $method;
 }