/** * @param array $settings * @return \FluidTYPO3\Flux\Form\Container\Section */ public static function create(array $settings) { /** @var ObjectManagerInterface $objectManager */ $objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); /** @var Section */ $section = $objectManager->get('FluidTYPO3\\Flux\\Form\\Container\\Section'); foreach ($settings as $settingName => $settingValue) { $setterMethodName = ObjectAccess::buildSetterMethodName($settingName); if (TRUE === method_exists($section, $setterMethodName)) { ObjectAccess::setProperty($section, $settingName, $settingValue); } } if (TRUE === isset($settings['objects'])) { foreach ($settings['objects'] as $fieldName => $objectSettings) { if (FALSE === isset($objectSettings['name'])) { $objectSettings['name'] = $fieldName; } $object = Object::create($objectSettings); $section->add($object); } } return $section; }
/** * Modifies the current Form Component by changing any properties * that were passed in $structure. If a component supports special * indices in $structure (for example a "fields" property) then * that component may specify its own `modify()` method and manually * process each of the specially supported keywords. * * For example, the AbstractFormContainer supports passing "fields" * and each field is then attempted fetched from children. If not * found, it is created (and the structure passed to the `create()` * function which uses the same structure syntax). If it already * exists, the `modify()` method is called on that object to trigger * the recursive modification of all child components. * * @param array $structure * @return FormInterface */ public function modify(array $structure) { if (TRUE === isset($structure['options']) && TRUE === is_array($structure['options'])) { foreach ($structure['options'] as $name => $value) { $this->setVariable($name, $value); } unset($structure['options']); } foreach ($structure as $propertyName => $propertyValue) { $setterMethodName = ObjectAccess::buildSetterMethodName($propertyName); if (TRUE === method_exists($this, $setterMethodName)) { ObjectAccess::setProperty($this, $propertyName, $propertyValue); } } return $this; }
/** * @test */ public function canCallAllGetterCounterpartsForChainableSetters() { $instance = $this->createInstance(); foreach ($this->chainProperties as $propertyName => $propertValue) { $setterMethodName = ObjectAccess::buildSetterMethodName($propertyName); $instance->{$setterMethodName}($propertValue); $result = ObjectAccess::getProperty($instance, $propertyName); $this->assertEquals($propertValue, $result); } }
/** * The type of a property is determined by the reflection service. * * @param string $targetType * @param string $propertyName * @param \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration * @return string * @throws \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException */ public function getTypeOfChildProperty($targetType, $propertyName, \TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface $configuration) { $configuredTargetType = $configuration->getConfigurationFor($propertyName)->getConfigurationValue(\TYPO3\CMS\Extbase\Property\TypeConverter\ObjectConverter::class, self::CONFIGURATION_TARGET_TYPE); if ($configuredTargetType !== null) { return $configuredTargetType; } $specificTargetType = $this->objectContainer->getImplementationClassName($targetType); if ($this->reflectionService->hasMethod($specificTargetType, \TYPO3\CMS\Extbase\Reflection\ObjectAccess::buildSetterMethodName($propertyName))) { $methodParameters = $this->reflectionService->getMethodParameters($specificTargetType, \TYPO3\CMS\Extbase\Reflection\ObjectAccess::buildSetterMethodName($propertyName)); $methodParameter = current($methodParameters); if (!isset($methodParameter['type'])) { throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Setter for property "' . $propertyName . '" had no type hint or documentation in target object of type "' . $specificTargetType . '".', 1303379158); } else { return $methodParameter['type']; } } else { $methodParameters = $this->reflectionService->getMethodParameters($specificTargetType, '__construct'); if (isset($methodParameters[$propertyName]) && isset($methodParameters[$propertyName]['type'])) { return $methodParameters[$propertyName]['type']; } else { throw new \TYPO3\CMS\Extbase\Property\Exception\InvalidTargetException('Property "' . $propertyName . '" had no setter or constructor argument in target object of type "' . $specificTargetType . '".', 1303379126); } } }
/** * @test * @param array $chainPropertiesAndValues * @return FieldInterface */ public function canChainAllChainableSetters($chainPropertiesAndValues = NULL) { if (NULL === $chainPropertiesAndValues) { $chainPropertiesAndValues = $this->chainProperties; } $instance = $this->createInstance(); foreach ($chainPropertiesAndValues as $propertyName => $propertValue) { $setterMethodName = ObjectAccess::buildSetterMethodName($propertyName); $chained = call_user_func_array(array($instance, $setterMethodName), array($propertValue)); $this->assertSame($instance, $chained, 'The setter ' . $setterMethodName . ' on ' . $this->getObjectClassName() . ' does not support chaining.'); if ($chained === $instance) { $instance = $chained; } } $this->performTestBuild($instance); return $instance; }
/** * @param array $settings * @return FormInterface */ public static function create(array $settings = array()) { /** @var ObjectManagerInterface $objectManager */ $objectManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); $className = get_called_class(); /** @var FormInterface $object */ $object = $objectManager->get($className); foreach ($settings as $settingName => $settingValue) { $setterMethodName = ObjectAccess::buildSetterMethodName($settingName); if (TRUE === method_exists($object, $setterMethodName)) { ObjectAccess::setProperty($object, $settingName, $settingValue); } } if (TRUE === $object instanceof FieldContainerInterface && TRUE === isset($settings['fields'])) { foreach ($settings['fields'] as $fieldName => $fieldSettings) { if (FALSE === isset($fieldSettings['name'])) { $fieldSettings['name'] = $fieldName; } $field = AbstractFormField::create($fieldSettings); $object->add($field); } } return $object; }