/** * @param PropertyOutline $property * @param ClassTypeImplementation $classOptions * @param ImplementationInterface $implementation * * @return PropertyDefinition */ protected function createPropertyDefinition(PropertyOutline $property, ClassTypeImplementation $classOptions, ImplementationInterface $implementation) { $propertyType = $this->schemaLinker->generateType($property->getType(), $implementation); $propertyDefinition = new PropertyDefinition($property->getName(), $propertyType); $propertyImplementation = $classOptions->getImplementationForProperty($property->getName()); $targetPropertyName = $propertyImplementation->getTargetPropertyName(); if ($targetPropertyName) { $propertyDefinition->setTargetPropertyName($targetPropertyName); } // nullable $propertyDefinition->setIsNullable($property->isNullable()); // default value if ($property->hasDefaultValue()) { $propertyDefinition->setDefaultValue($property->getDefaultValue()); } // setters and getters $setter = $propertyImplementation->getSetter(); if ($setter) { $propertyDefinition->setSetterName($setter); } $getter = $propertyImplementation->getGetter(); if ($getter) { $propertyDefinition->setGetterName($getter); } return $propertyDefinition; }
/** * @depends testDefaultPropertyImplementationWithAccessorsButWithoutStyle */ public function testDefaultPropertyImplementationWithAccessorsAndStyle() { $this->object->setAccessorsEnabled(true)->setAccessorsStyle(new CamelCaseStyle()); $propertyName = 'inferior'; $propertyImplementation = $this->object->getImplementationForProperty($propertyName); $this->assertSame('setInferior', $propertyImplementation->getSetter(), 'setter name should be null'); $this->assertSame('getInferior', $propertyImplementation->getGetter(), 'getter name should be null'); }
/** * @depends testDefaultClassImplementationIsCreatedWhenThereIsNoDefinedClassTypeImplementation */ public function testDefaultClassImplementationIsCreatedBasedOnGlobalClassTypeImplementation() { $this->globalClassOptions->setAccessorsEnabled(true)->setAccessorsStyle(new CamelCaseStyle()); $this->classMap->registerClass('helicopter', 'ConcreteXylophone'); $implementation = new ClassTypeImplementation('helicopter'); $implementation->setClassName('ConcreteXylophone')->setAccessorsEnabled(true)->setAccessorsStyle(new CamelCaseStyle()); $this->assertEquals($implementation, $this->object->getClassTypeImplementation('helicopter')); }
private function useExceptionImplementation() { $this->getClassMap()->registerClass('Exception', '\\Exception'); $implementation = new ClassTypeImplementation('Exception'); $causePropertyImplementation = new PropertyImplementation('cause'); $causePropertyImplementation->setTargetPropertyName('previous'); $implementation->addPropertyImplementation($causePropertyImplementation); $this->registerClassTypeImplementation($implementation); }
public function registerClassTypeImplementation(ClassTypeImplementation $classImplementation) { $this->classImplementations[$classImplementation->getName()] = $classImplementation; return $this; }
protected function createPropertyImplementation($propertyName, $propertyOptions, ClassTypeImplementation $classImplementation) { $propertyImplementation = new PropertyImplementation($propertyName); $targetPropertyName = $propertyName; if (isset($propertyOptions['as'])) { $propertyImplementation->setTargetPropertyName($propertyOptions['as']); $targetPropertyName = $propertyOptions['as']; } // accessors enabled $accessorsEnabled = $classImplementation->isAccessorsEnabled(); if (isset($propertyOptions['accessors']['enabled'])) { $accessorsEnabled = (bool) $propertyOptions['accessors']['enabled']; } // accessors names if (array_key_exists('setter', $propertyOptions)) { $propertyImplementation->setSetter($propertyOptions['setter']); } else { if ($accessorsEnabled) { $style = $classImplementation->getAccessorsStyle(); if (!$style) { $msg = 'Cannot generate setter name for property "' . $propertyName . '". Define that name or naming style of setters and getters'; throw new \BadMethodCallException($msg); } $propertyImplementation->setSetter($style->generateSetterName($targetPropertyName)); } } if (array_key_exists('getter', $propertyOptions)) { $propertyImplementation->setGetter($propertyOptions['getter']); } elseif ($accessorsEnabled) { $style = $classImplementation->getAccessorsStyle(); if (!$style) { $msg = 'Cannot generate getter name for property "' . $propertyName . '". Define that name or naming style of setters and getters'; throw new \BadMethodCallException($msg); } $propertyImplementation->setGetter($style->generateGetterName($targetPropertyName)); } return $propertyImplementation; }
public function testGenerateClass() { $parentClass = new ClassOutline('vacuumBadger'); $outline = new ClassOutline('vacuum', array(), $parentClass); $outline->addProperty($this->propertyOutline('zoo')->setIsNullable(true))->addProperty($this->propertyOutline('bridge')->setIsNullable(true))->addProperty($this->propertyOutline('galley'))->addProperty($this->propertyOutline('granddaughter')->setDefaultValue('is young'))->addProperty($this->propertyOutline('everyNumber')); $this->classMap->registerClass('vacuum', 'VacuumZooBridge')->registerClass('vacuumBadger', 'VacuumBadger'); $classImplementation = new ClassTypeImplementation('vacuum'); $classImplementation->addPropertyImplementation($this->propertyImplementation('zoo'))->addPropertyImplementation($this->propertyImplementation('bridge')->setSetter('setBridge')->setGetter('getBridge'))->addPropertyImplementation($this->propertyImplementation('galley')->setSetter('setGalley'))->addPropertyImplementation($this->propertyImplementation('granddaughter')->setGetter('getGranddaughter'))->addPropertyImplementation($this->propertyImplementation('everyNumber')->setTargetPropertyName('notEveryNumber')); $this->implementation->registerClassTypeImplementation($classImplementation); $parentClassExpected = new ClassType('vacuumBadger', 'VacuumBadger'); $expected = new ClassType('vacuum', 'VacuumZooBridge', $parentClassExpected); $expected->addProperty($this->property('zoo')->setIsNullable(true))->addProperty($this->property('bridge')->setGetterName('getBridge')->setSetterName('setBridge')->setIsNullable(true))->addProperty($this->property('galley')->setSetterName('setGalley'))->addProperty($this->property('granddaughter')->setDefaultValue('is young')->setGetterName('getGranddaughter'))->addProperty($this->property('everyNumber')->setTargetPropertyName('notEveryNumber')); $result = $this->object->generate($outline, $this->implementation); $this->assertEquals($expected, $result); }