/** * @param string $dataSourceIdentifier * @param NodeInterface $node * @return string * @throws NeosException */ public function indexAction($dataSourceIdentifier, NodeInterface $node = null) { $dataSources = static::getDataSources($this->objectManager); if (!isset($dataSources[$dataSourceIdentifier])) { throw new NeosException(sprintf('Data source with identifier "%s" does not exist.', $dataSourceIdentifier), 1414088186); } /** @var $dataSource DataSourceInterface */ $dataSource = new $dataSources[$dataSourceIdentifier](); if (ObjectAccess::isPropertySettable($dataSource, 'controllerContext')) { ObjectAccess::setProperty($dataSource, 'controllerContext', $this->controllerContext); } $arguments = $this->request->getArguments(); unset($arguments['dataSourceIdentifier']); unset($arguments['node']); $values = $dataSource->getData($node, $arguments); $this->view->assign('value', $values); }
/** * Factory method which creates the specified transport with the given options. * * @param string $transportType Object name of the transport to create * @param array $transportOptions Options for the transport * @param array $transportArguments Constructor arguments for the transport * @return \Neos\SwiftMailer\TransportInterface The created transport instance * @throws Exception */ public function create($transportType, array $transportOptions = array(), array $transportArguments = null) { if (!class_exists($transportType)) { throw new Exception('The specified transport backend "' . $transportType . '" does not exist.', 1269351207); } if (is_array($transportArguments)) { $class = new \ReflectionClass($transportType); $transport = $class->newInstanceArgs($transportArguments); } else { $transport = new $transportType(); } foreach ($transportOptions as $optionName => $optionValue) { if (ObjectAccess::isPropertySettable($transport, $optionName)) { ObjectAccess::setProperty($transport, $optionName, $optionValue); } } return $transport; }
/** * Set a specific option of this object * * @param string $optionName * @param mixed $value * @return void */ public function setOption($optionName, $value) { $this->options[$optionName] = $value; if (ObjectAccess::isPropertySettable($this, $optionName)) { ObjectAccess::setProperty($this, $optionName, $value); } }
/** * Convert an object from $source to an entity or a value object. * * @param mixed $source * @param string $targetType * @param array $convertedChildProperties * @param PropertyMappingConfigurationInterface $configuration * @return object|TargetNotFoundError the converted entity/value object or an instance of TargetNotFoundError if the object could not be resolved * @throws \InvalidArgumentException|InvalidTargetException */ public function convertFrom($source, $targetType, array $convertedChildProperties = [], PropertyMappingConfigurationInterface $configuration = null) { if (is_array($source)) { if ($this->reflectionService->isClassAnnotatedWith($targetType, ValueObject::class)) { if (isset($source['__identity']) && count($source) > 1) { // @TODO fix that in the URI building and transfer VOs as values instead as with their identities // Unset identity for value objects to use constructor mapping, since the identity is determined from // property values after construction unset($source['__identity']); } } $object = $this->handleArrayData($source, $targetType, $convertedChildProperties, $configuration); if ($object instanceof TargetNotFoundError) { return $object; } } elseif (is_string($source)) { if ($source === '') { return null; } $object = $this->fetchObjectFromPersistence($source, $targetType); if ($object === null) { return new TargetNotFoundError(sprintf('Object of type "%s" with identity "%s" not found.', $targetType, $source), 1412283033); } } else { throw new \InvalidArgumentException('Only strings and arrays are accepted.', 1305630314); } $objectConstructorArguments = $this->getConstructorArgumentsForClass(TypeHandling::getTypeForValue($object)); foreach ($convertedChildProperties as $propertyName => $propertyValue) { // We need to check for "immutable" constructor arguments that have no setter and remove them. if (isset($objectConstructorArguments[$propertyName]) && !ObjectAccess::isPropertySettable($object, $propertyName)) { $currentPropertyValue = ObjectAccess::getProperty($object, $propertyName); if ($currentPropertyValue === $propertyValue) { continue; } else { $exceptionMessage = sprintf('Property "%s" having a value of type "%s" could not be set in target object of type "%s". The property has no setter and is not equal to the value in the object, in that case it would have been skipped.', $propertyName, is_object($propertyValue) ? TypeHandling::getTypeForValue($propertyValue) : gettype($propertyValue), $targetType); throw new InvalidTargetException($exceptionMessage, 1421498771); } } $result = ObjectAccess::setProperty($object, $propertyName, $propertyValue); if ($result === false) { $exceptionMessage = sprintf('Property "%s" having a value of type "%s" could not be set in target object of type "%s". Make sure that the property is accessible properly, for example via an appropriate setter method.', $propertyName, is_object($propertyValue) ? TypeHandling::getTypeForValue($propertyValue) : gettype($propertyValue), $targetType); throw new InvalidTargetException($exceptionMessage, 1297935345); } } return $object; }
/** * @test */ public function isPropertySettableWorksOnStdClass() { $stdClassObject = new \stdClass(); $stdClassObject->property = 'foo'; $this->assertTrue(ObjectAccess::isPropertySettable($stdClassObject, 'property')); $this->assertFalse(ObjectAccess::isPropertySettable($stdClassObject, 'undefinedProperty')); }
/** * Sets the specified property. * If the node has a content object attached, the property will be set there * if it is settable. * * @param string $propertyName Name of the property * @param mixed $value Value of the property * @return void */ public function setProperty($propertyName, $value) { if (!is_object($this->contentObjectProxy)) { switch ($this->getNodeType()->getPropertyType($propertyName)) { case 'references': $nodeIdentifiers = array(); if (is_array($value)) { foreach ($value as $nodeIdentifier) { if ($nodeIdentifier instanceof NodeInterface || $nodeIdentifier instanceof AbstractNodeData) { $nodeIdentifiers[] = $nodeIdentifier->getIdentifier(); } elseif (preg_match(UuidValidator::PATTERN_MATCH_UUID, $nodeIdentifier) !== 0) { $nodeIdentifiers[] = $nodeIdentifier; } } } $value = $nodeIdentifiers; break; case 'reference': $nodeIdentifier = null; if ($value instanceof NodeInterface || $value instanceof AbstractNodeData) { $nodeIdentifier = $value->getIdentifier(); } elseif (preg_match(UuidValidator::PATTERN_MATCH_UUID, $value) !== 0) { $nodeIdentifier = $value; } $value = $nodeIdentifier; break; } $this->persistRelatedEntities($value); if (isset($this->properties[$propertyName]) && $this->properties[$propertyName] === $value) { return; } $this->properties[$propertyName] = $value; $this->addOrUpdate(); } elseif (ObjectAccess::isPropertySettable($this->contentObjectProxy->getObject(), $propertyName)) { $contentObject = $this->contentObjectProxy->getObject(); ObjectAccess::setProperty($contentObject, $propertyName, $value); $this->updateContentObject($contentObject); } }