/** * @param RuntimeConfiguration $configuration * * @return NewOperator */ public function generate(RuntimeConfiguration $configuration) { $this->typeCheck->generate(func_get_args()); $validatorNamespaceCall = new Call(new StaticMember(QualifiedIdentifier::fromString('\\Eloquent\\Cosmos\\ClassName'), new Constant(new Identifier('fromString')))); $validatorNamespaceCall->add(new Literal($configuration->validatorNamespace()->string())); $newConfigurationCall = new Call(QualifiedIdentifier::fromString('\\Eloquent\\Typhoon\\Configuration\\RuntimeConfiguration')); $newConfigurationCall->add($validatorNamespaceCall); $newConfigurationCall->add(new Literal($configuration->useNativeCallable())); return new NewOperator($newConfigurationCall); }
/** * @param RuntimeConfiguration $configuration * @param null &$className * * @return SyntaxTree */ public function generateSyntaxTree(RuntimeConfiguration $configuration, &$className = null) { $this->typeCheck->generateSyntaxTree(func_get_args()); $className = $configuration->validatorNamespace()->joinAtoms('DummyValidator'); $classDefinition = new ClassDefinition(new Identifier($className->shortName()->string())); $classDefinition->setParentName(QualifiedIdentifier::fromString('AbstractValidator')); $classDefinition->add($this->generateCallMethod()); $primaryBlock = new PhpBlock(); $primaryBlock->add(new NamespaceStatement(QualifiedIdentifier::fromString($className->parent()->toRelative()->string()))); $primaryBlock->add($classDefinition); $syntaxTree = new SyntaxTree(); $syntaxTree->add($primaryBlock); return $syntaxTree; }
/** * @param RuntimeConfiguration $configuration * @param null &$className * * @return SyntaxTree */ public function generateSyntaxTree(RuntimeConfiguration $configuration, &$className = null) { $this->typeCheck->generateSyntaxTree(func_get_args()); $className = $configuration->validatorNamespace()->joinAtoms('Exception', 'UnexpectedInputException'); $classDefinition = new ClassDefinition(new Identifier($className->shortName()->string()), ClassModifier::ABSTRACT_()); $classDefinition->setParentName(QualifiedIdentifier::fromString('\\InvalidArgumentException')); $classDefinition->add($this->generateConstructor()); $primaryBlock = new PhpBlock(); $primaryBlock->add(new NamespaceStatement(QualifiedIdentifier::fromString($className->parent()->toRelative()->string()))); $primaryBlock->add($classDefinition); $syntaxTree = new SyntaxTree(); $syntaxTree->add($primaryBlock); return $syntaxTree; }
/** * @param string $outputPath * @param array<string> $sourcePaths */ public function __construct($outputPath, array $sourcePaths) { $this->typeCheck = TypeCheck::get(__CLASS__, func_get_args()); parent::__construct(); $this->setOutputPath($outputPath); $this->setSourcePaths($sourcePaths); $this->loaderPaths = array('vendor/autoload.php'); }
/** * @param RuntimeConfiguration $configuration * @param null &$className * * @return SyntaxTree */ public function generateSyntaxTree(RuntimeConfiguration $configuration, &$className = null) { $this->typeCheck->generateSyntaxTree(func_get_args()); $className = $configuration->validatorNamespace()->joinAtoms('Exception', 'MissingArgumentException'); $classDefinition = new ClassDefinition(new Identifier($className->shortName()->string()), ClassModifier::FINAL_()); $classDefinition->setParentName(QualifiedIdentifier::fromString('UnexpectedInputException')); $classDefinition->add($this->generateConstructor()); $classDefinition->add($this->generateParameterNameMethod()); $classDefinition->add($this->generateIndexMethod()); $classDefinition->add($this->generateExpectedTypeMethod()); $classDefinition->add(new Property(new Identifier('parameterName'), AccessModifier::PRIVATE_())); $classDefinition->add(new Property(new Identifier('index'), AccessModifier::PRIVATE_())); $classDefinition->add(new Property(new Identifier('expectedType'), AccessModifier::PRIVATE_())); $primaryBlock = new PhpBlock(); $primaryBlock->add(new NamespaceStatement(QualifiedIdentifier::fromString($className->parent()->toRelative()->string()))); $primaryBlock->add($classDefinition); $syntaxTree = new SyntaxTree(); $syntaxTree->add($primaryBlock); return $syntaxTree; }
/** * @param RuntimeConfiguration $configuration * @param null &$className * * @return SyntaxTree */ public function generateSyntaxTree(RuntimeConfiguration $configuration, &$className = null) { $this->typeCheck->generateSyntaxTree(func_get_args()); $className = $configuration->validatorNamespace()->joinAtoms('TypeInspector'); $classDefinition = new ClassDefinition(new Identifier($className->shortName()->string())); $classDefinition->add($this->generateTypeMethod()); $classDefinition->add($this->generateArrayTypeMethod()); $classDefinition->add($this->generateObjectTypeMethod()); $classDefinition->add($this->generateTraversableSubTypesMethod()); $classDefinition->add($this->generateResourceTypeMethod()); $classDefinition->add($this->generateStreamTypeMethod()); $primaryBlock = new PhpBlock(); $primaryBlock->add(new NamespaceStatement(QualifiedIdentifier::fromString($className->parent()->toRelative()->string()))); $primaryBlock->add($classDefinition); $syntaxTree = new SyntaxTree(); $syntaxTree->add($primaryBlock); return $syntaxTree; }
/** * @param RuntimeConfiguration $configuration * * @return boolean */ public function useNativeCallable(RuntimeConfiguration $configuration) { $this->typeCheck->useNativeCallable(func_get_args()); return $configuration->useNativeCallable() && $this->nativeCallableAvailable(); }
/** * @param RuntimeConfiguration $configuration * * @return ConcreteMethod */ protected function generateCreateValidatorMethod(RuntimeConfiguration $configuration) { $this->typeCheck->generateCreateValidatorMethod(func_get_args()); $classNameIdentifier = new Identifier('className'); $classNameVariable = new Variable($classNameIdentifier); $validatorClassNameVariable = new Variable(new Identifier('validatorClassName')); $method = new ConcreteMethod(new Identifier('createValidator'), AccessModifier::PROTECTED_(), true); $method->addParameter(new Parameter($classNameIdentifier)); $validatorClassNameConcatenation = new Concat(new Literal(sprintf('%s\\', $configuration->validatorNamespace()->joinAtoms('Validator')->string())), $classNameVariable); $validatorClassNameConcatenation->add(new Literal('TypeCheck')); $method->statementBlock()->add(new ExpressionStatement(new Assign($validatorClassNameVariable, $validatorClassNameConcatenation))); $staticConstant = new Constant(new Identifier('static')); $runtimeGenerationCall = new Call(new StaticMember($staticConstant, new Constant(new Identifier('runtimeGeneration')))); $classExistsCall = new Call(QualifiedIdentifier::fromString('\\class_exists')); $classExistsCall->add($validatorClassNameVariable); $runtimeGenerationIf = new IfStatement(new LogicalAnd($runtimeGenerationCall, new LogicalNot($classExistsCall))); $staticDummyModeVariable = new StaticMember($staticConstant, new Variable(new Identifier('dummyMode'))); $runtimeGenerationIf->trueBranch()->add(new ExpressionStatement(new Assign($staticDummyModeVariable, new Literal(true)))); $defineValidatorCall = new Call(new StaticMember($staticConstant, new Constant(new Identifier('defineValidator')))); $defineValidatorCall->add($classNameVariable); $runtimeGenerationIf->trueBranch()->add(new ExpressionStatement($defineValidatorCall)); $runtimeGenerationIf->trueBranch()->add(new ExpressionStatement(new Assign($staticDummyModeVariable, new Literal(false)))); $method->statementBlock()->add($runtimeGenerationIf); $method->statementBlock()->add(new ReturnStatement(new NewOperator($validatorClassNameVariable))); 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; }
/** * @param RuntimeConfiguration $configuration * @param ClassDefinition $classDefinition * * @return ClassName */ protected function validatorClassName(RuntimeConfiguration $configuration, ClassDefinition $classDefinition) { $this->typeCheck->validatorClassName(func_get_args()); $classNameAtoms = $configuration->validatorNamespace()->atoms(); $classNameAtoms[] = 'Validator'; if ($classDefinition->className()->hasParent()) { $classNameAtoms = array_merge($classNameAtoms, $classDefinition->className()->parent()->atoms()); } $classNameAtoms[] = sprintf('%sTypeCheck', $classDefinition->className()->shortName()->string()); return ClassName::fromAtoms($classNameAtoms, true); }