/**
  * @test
  */
 public function mergeShouldMergeTwoResults()
 {
     $notice1 = $this->getMockMessage('Notice');
     $notice2 = $this->getMockMessage('Notice');
     $notice3 = $this->getMockMessage('Notice');
     $warning1 = $this->getMockMessage('Warning');
     $warning2 = $this->getMockMessage('Warning');
     $warning3 = $this->getMockMessage('Warning');
     $error1 = $this->getMockMessage('Error');
     $error2 = $this->getMockMessage('Error');
     $error3 = $this->getMockMessage('Error');
     $otherResult = new \TYPO3\Flow\Error\Result();
     $otherResult->addNotice($notice1);
     $otherResult->forProperty('foo.bar')->addNotice($notice2);
     $this->result->forProperty('foo')->addNotice($notice3);
     $otherResult->addWarning($warning1);
     $this->result->addWarning($warning2);
     $this->result->addWarning($warning3);
     $otherResult->forProperty('foo')->addError($error1);
     $otherResult->forProperty('foo')->addError($error2);
     $otherResult->addError($error3);
     $this->result->merge($otherResult);
     $this->assertSame(array($notice1), $this->result->getNotices(), 'Notices are not merged correctly without recursion');
     $this->assertSame(array($notice3), $this->result->forProperty('foo')->getNotices(), 'Original sub-notices are overridden.');
     $this->assertSame(array($notice2), $this->result->forProperty('foo')->forProperty('bar')->getNotices(), 'Sub-notices are not copied.');
     $this->assertSame(array($warning2, $warning3, $warning1), $this->result->getWarnings());
     $this->assertSame(array($error3), $this->result->getErrors());
     $this->assertSame(array($error1, $error2), $this->result->forProperty('foo')->getErrors());
 }
 /**
  * Checks if the given value is valid according to the property validators.
  *
  * @param mixed $object The value that should be validated
  * @return void
  * @api
  */
 protected function isValid($object)
 {
     $messages = new ErrorResult();
     foreach ($this->propertyValidators as $propertyName => $validators) {
         $propertyValue = $this->getPropertyValue($object, $propertyName);
         $result = $this->checkProperty($propertyValue, $validators);
         if ($result !== null) {
             $messages->forProperty($propertyName)->merge($result);
         }
     }
     $this->result = $messages;
 }
 /**
  * Internal function which actually does the property mapping.
  *
  * @param mixed $source the source data to map. MUST be a simple type, NO object allowed!
  * @param string $targetType The type of the target; can be either a class name or a simple type.
  * @param \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration Configuration for the property mapping.
  * @param array $currentPropertyPath The property path currently being mapped; used for knowing the context in case an exception is thrown.
  * @return mixed an instance of $targetType
  * @throws \TYPO3\Flow\Property\Exception\TypeConverterException
  * @throws \TYPO3\Flow\Property\Exception\InvalidPropertyMappingConfigurationException
  */
 protected function doMapping($source, $targetType, \TYPO3\Flow\Property\PropertyMappingConfigurationInterface $configuration, &$currentPropertyPath)
 {
     if (is_object($source)) {
         $targetClass = TypeHandling::truncateElementType($targetType);
         if ($source instanceof $targetClass) {
             return $source;
         }
     }
     if ($source === null) {
         $source = '';
     }
     $typeConverter = $this->findTypeConverter($source, $targetType, $configuration);
     $targetType = $typeConverter->getTargetTypeForSource($source, $targetType, $configuration);
     if (!is_object($typeConverter) || !$typeConverter instanceof \TYPO3\Flow\Property\TypeConverterInterface) {
         throw new Exception\TypeConverterException('Type converter for "' . $source . '" -> "' . $targetType . '" not found.');
     }
     $convertedChildProperties = array();
     foreach ($typeConverter->getSourceChildPropertiesToBeConverted($source) as $sourcePropertyName => $sourcePropertyValue) {
         $targetPropertyName = $configuration->getTargetPropertyName($sourcePropertyName);
         if ($configuration->shouldSkip($targetPropertyName)) {
             continue;
         }
         if (!$configuration->shouldMap($targetPropertyName)) {
             if ($configuration->shouldSkipUnknownProperties()) {
                 continue;
             }
             throw new Exception\InvalidPropertyMappingConfigurationException('It is not allowed to map property "' . $targetPropertyName . '". You need to use $propertyMappingConfiguration->allowProperties(\'' . $targetPropertyName . '\') to enable mapping of this property.', 1335969887);
         }
         $targetPropertyType = $typeConverter->getTypeOfChildProperty($targetType, $targetPropertyName, $configuration);
         $subConfiguration = $configuration->getConfigurationFor($targetPropertyName);
         $currentPropertyPath[] = $targetPropertyName;
         $targetPropertyValue = $this->doMapping($sourcePropertyValue, $targetPropertyType, $subConfiguration, $currentPropertyPath);
         array_pop($currentPropertyPath);
         if (!$targetPropertyValue instanceof \TYPO3\Flow\Error\Error) {
             $convertedChildProperties[$targetPropertyName] = $targetPropertyValue;
         }
     }
     $result = $typeConverter->convertFrom($source, $targetType, $convertedChildProperties, $configuration);
     if ($result instanceof \TYPO3\Flow\Error\Error) {
         $this->messages->forProperty(implode('.', $currentPropertyPath))->addError($result);
     }
     return $result;
 }
 /**
  * Get all property mapping / validation errors
  *
  * @return Result
  */
 public function getValidationResults()
 {
     $results = new Result();
     foreach ($this as $argument) {
         $argumentValidationResults = $argument->getValidationResults();
         if ($argumentValidationResults === null) {
             continue;
         }
         $results->forProperty($argument->getName())->merge($argumentValidationResults);
     }
     return $results;
 }
 /**
  * Validate a single configuration type
  *
  * @param string $configurationType the configuration typr to validate
  * @param string $path configuration path to validate, or NULL.
  * @param array $loadedSchemaFiles will be filled with a list of loaded schema files
  * @return \TYPO3\Flow\Error\Result
  * @throws Exception\SchemaValidationException
  */
 protected function validateSingleType($configurationType, $path, &$loadedSchemaFiles)
 {
     $availableConfigurationTypes = $this->configurationManager->getAvailableConfigurationTypes();
     if (in_array($configurationType, $availableConfigurationTypes) === false) {
         throw new Exception\SchemaValidationException('The configuration type "' . $configurationType . '" was not found. Only the following configuration types are supported: "' . implode('", "', $availableConfigurationTypes) . '"', 1364984886);
     }
     $configuration = $this->configurationManager->getConfiguration($configurationType);
     // find schema files for the given type and path
     $schemaFileInfos = [];
     $activePackages = $this->packageManager->getActivePackages();
     foreach ($activePackages as $package) {
         $packageKey = $package->getPackageKey();
         $packageSchemaPath = Files::concatenatePaths([$package->getResourcesPath(), 'Private/Schema']);
         if (is_dir($packageSchemaPath)) {
             foreach (Files::getRecursiveDirectoryGenerator($packageSchemaPath, '.schema.yaml') as $schemaFile) {
                 $schemaName = substr($schemaFile, strlen($packageSchemaPath) + 1, -strlen('.schema.yaml'));
                 $schemaNameParts = explode('.', str_replace('/', '.', $schemaName), 2);
                 $schemaType = $schemaNameParts[0];
                 $schemaPath = isset($schemaNameParts[1]) ? $schemaNameParts[1] : null;
                 if ($schemaType === $configurationType && ($path === null || strpos($schemaPath, $path) === 0)) {
                     $schemaFileInfos[] = ['file' => $schemaFile, 'name' => $schemaName, 'path' => $schemaPath, 'packageKey' => $packageKey];
                 }
             }
         }
     }
     if (count($schemaFileInfos) === 0) {
         throw new Exception\SchemaValidationException('No schema files found for configuration type "' . $configurationType . '"' . ($path !== null ? ' and path "' . $path . '".' : '.'), 1364985056);
     }
     $result = new Result();
     foreach ($schemaFileInfos as $schemaFileInfo) {
         $loadedSchemaFiles[] = $schemaFileInfo['file'];
         if ($schemaFileInfo['path'] !== null) {
             $data = Arrays::getValueByPath($configuration, $schemaFileInfo['path']);
         } else {
             $data = $configuration;
         }
         if (empty($data)) {
             $result->addNotice(new Notice('No configuration found, skipping schema "%s".', 1364985445, [substr($schemaFileInfo['file'], strlen(FLOW_PATH_ROOT))]));
         } else {
             $parsedSchema = Yaml::parse($schemaFileInfo['file']);
             $validationResultForSingleSchema = $this->schemaValidator->validate($data, $parsedSchema);
             if ($schemaFileInfo['path'] !== null) {
                 $result->forProperty($schemaFileInfo['path'])->merge($validationResultForSingleSchema);
             } else {
                 $result->merge($validationResultForSingleSchema);
             }
         }
     }
     return $result;
 }
 /**
  * Shows the status of the current mapping
  *
  * @param string $object Class name of a domain object. If given, will only work on this single object
  * @param boolean $conductUpdate Set to TRUE to conduct the required corrections
  * @param string $clientName The client name to use
  */
 public function statusCommand($object = null, $conductUpdate = false, $clientName = null)
 {
     $result = new ErrorResult();
     $client = $this->clientFactory->create($clientName);
     $classesAndAnnotations = $this->indexInformer->getClassesAndAnnotations();
     if ($object !== null) {
         if (!isset($classesAndAnnotations[$object])) {
             $this->outputFormatted("Error: Object '<b>%s</b>' is not configured correctly, check the Indexable annotation.", array($object));
             $this->quit(1);
         }
         $classesAndAnnotations = array($object => $classesAndAnnotations[$object]);
     }
     array_walk($classesAndAnnotations, function (Indexable $annotation, $className) use($result, $client, $conductUpdate) {
         $this->outputFormatted("Object %s", array($className), 4);
         $this->outputFormatted("Index <b>%s</b> Type <b>%s</b>", array($annotation->indexName, $annotation->typeName), 8);
         $count = $client->findIndex($annotation->indexName)->findType($annotation->typeName)->count();
         if ($count === null) {
             $result->forProperty($className)->addError(new Error('ElasticSearch was unable to retrieve a count for the type "%s" at index "%s". Probably these don\' exist.', 1340289921, array($annotation->typeName, $annotation->indexName)));
         }
         $this->outputFormatted("Documents in Search: <b>%s</b>", array($count !== null ? $count : "Error"), 8);
         try {
             $count = $this->persistenceManager->createQueryForType($className)->count();
         } catch (\Exception $exception) {
             $count = null;
             $result->forProperty($className)->addError(new Error('The persistence backend was unable to retrieve a count for the type "%s". The exception message was "%s".', 1340290088, array($className, $exception->getMessage())));
         }
         $this->outputFormatted("Documents in Persistence: <b>%s</b>", array($count !== null ? $count : "Error"), 8);
         if (!$result->forProperty($className)->hasErrors()) {
             $states = $this->getModificationsNeededStatesAndIdentifiers($client, $className);
             if ($conductUpdate) {
                 $inserted = 0;
                 $updated = 0;
                 foreach ($states[ObjectIndexer::ACTION_TYPE_CREATE] as $identifier) {
                     try {
                         $this->objectIndexer->indexObject($this->persistenceManager->getObjectByIdentifier($identifier, $className));
                         $inserted++;
                     } catch (\Exception $exception) {
                         $result->forProperty($className)->addError(new Error('An error occurred while trying to add an object to the ElasticSearch backend. The exception message was "%s".', 1340356330, array($exception->getMessage())));
                     }
                 }
                 foreach ($states[ObjectIndexer::ACTION_TYPE_UPDATE] as $identifier) {
                     try {
                         $this->objectIndexer->indexObject($this->persistenceManager->getObjectByIdentifier($identifier, $className));
                         $updated++;
                     } catch (\Exception $exception) {
                         $result->forProperty($className)->addError(new Error('An error occurred while trying to update an object to the ElasticSearch backend. The exception message was "%s".', 1340358590, array($exception->getMessage())));
                     }
                 }
                 $this->outputFormatted("Objects inserted: <b>%s</b>", array($inserted), 8);
                 $this->outputFormatted("Objects updated: <b>%s</b>", array($updated), 8);
             } else {
                 $this->outputFormatted("Modifications needed: <b>create</b> %d, <b>update</b> %d", array(count($states[ObjectIndexer::ACTION_TYPE_CREATE]), count($states[ObjectIndexer::ACTION_TYPE_UPDATE])), 8);
             }
         }
     });
     if ($result->hasErrors()) {
         $this->outputLine();
         $this->outputLine('The following errors occurred:');
         /** @var $error \TYPO3\Flow\Error\Error */
         foreach ($result->getFlattenedErrors() as $className => $errors) {
             foreach ($errors as $error) {
                 $this->outputLine();
                 $this->outputFormatted("<b>Error</b> for %s:", array($className), 8);
                 $this->outputFormatted((string) $error, array(), 4);
             }
         }
     }
 }
 /**
  * Validate a single configuration type
  *
  * @param string $configurationType the configuration typr to validate
  * @param string $path configuration path to validate, or NULL.
  * @param array $loadedSchemaFiles will be filled with a list of loaded schema files
  * @return \TYPO3\Flow\Error\Result
  * @throws Exception\SchemaValidationException
  */
 protected function validateSingleType($configurationType, $path, &$loadedSchemaFiles)
 {
     $availableConfigurationTypes = $this->configurationManager->getAvailableConfigurationTypes();
     if (in_array($configurationType, $availableConfigurationTypes) === FALSE) {
         throw new Exception\SchemaValidationException('The configuration type "' . $configurationType . '" was not found. Only the following configuration types are supported: "' . implode('", "', $availableConfigurationTypes) . '"', 1364984886);
     }
     $configuration = $this->configurationManager->getConfiguration($configurationType);
     // find schema files for the given type and path
     $schemaFileInfos = array();
     $activePackages = $this->packageManager->getActivePackages();
     foreach ($activePackages as $package) {
         $packageKey = $package->getPackageKey();
         $packageSchemaPath = \TYPO3\Flow\Utility\Files::concatenatePaths(array($package->getResourcesPath(), 'Private/Schema'));
         if (is_dir($packageSchemaPath)) {
             $packageSchemaFiles = \TYPO3\Flow\Utility\Files::readDirectoryRecursively($packageSchemaPath, '.schema.yaml');
             foreach ($packageSchemaFiles as $schemaFile) {
                 $schemaName = substr($schemaFile, strlen($packageSchemaPath) + 1, -strlen('.schema.yaml'));
                 $schemaNameParts = explode('.', str_replace('/', '.', $schemaName), 2);
                 $schemaType = $schemaNameParts[0];
                 $schemaPath = isset($schemaNameParts[1]) ? $schemaNameParts[1] : NULL;
                 if ($schemaType === $configurationType && ($path === NULL || strpos($schemaPath, $path) === 0)) {
                     $schemaFileInfos[] = array('file' => $schemaFile, 'name' => $schemaName, 'path' => $schemaPath, 'packageKey' => $packageKey);
                 }
             }
         }
     }
     if (count($schemaFileInfos) === 0) {
         throw new Exception\SchemaValidationException('No schema files found for configuration type "' . $configurationType . '"' . ($path !== NULL ? ' and path "' . $path . '".' : '.'), 1364985056);
     }
     $result = new Result();
     foreach ($schemaFileInfos as $schemaFileInfo) {
         $loadedSchemaFiles[] = $schemaFileInfo['file'];
         if ($schemaFileInfo['path'] !== NULL) {
             $data = \TYPO3\Flow\Utility\Arrays::getValueByPath($configuration, $schemaFileInfo['path']);
         } else {
             $data = $configuration;
         }
         if (empty($data)) {
             throw new Exception\SchemaValidationException('The schema file "' . $schemaFileInfo['file'] . '" is empty.', 1364985445);
         } else {
             $parsedSchema = \Symfony\Component\Yaml\Yaml::parse($schemaFileInfo['file']);
             $validationResultForSingleSchema = $this->schemaValidator->validate($data, $parsedSchema);
             if ($schemaFileInfo['path'] !== NULL) {
                 $result->forProperty($schemaFileInfo['path'])->merge($validationResultForSingleSchema);
             } else {
                 $result->merge($validationResultForSingleSchema);
             }
         }
     }
     return $result;
 }
 /**
  * Validate a dictionary value with the given schema
  *
  * The following properties are handled in given $schema:
  * - properties : array of keys and schemas that have to validate
  * - formatProperties : dictionary of schemas, the schemas are used to validate all keys that match the string-format
  * - patternProperties : dictionary of schemas, the schemas are used to validate all keys that match the string-pattern
  * - additionalProperties : if FALSE is given all additionalProperties are forbidden, if a schema is given all additional properties have to match the schema
  *
  * @param mixed $value
  * @param array $schema
  * @return ErrorResult
  */
 protected function validateDictionaryType($value, array $schema)
 {
     $result = new ErrorResult();
     if (is_array($value) === false || $this->isDictionary($value) === false) {
         $result->addError($this->createError('type=dictionary', 'type=' . gettype($value)));
         return $result;
     }
     $propertyKeysToHandle = array_keys($value);
     if (isset($schema['properties'])) {
         foreach ($schema['properties'] as $propertyName => $propertySchema) {
             if (array_key_exists($propertyName, $value)) {
                 $propertyValue = $value[$propertyName];
                 $subresult = $this->validate($propertyValue, $propertySchema);
                 if ($subresult->hasErrors()) {
                     $result->forProperty($propertyName)->merge($subresult);
                 }
                 $propertyKeysToHandle = array_diff($propertyKeysToHandle, [$propertyName]);
             } else {
                 // is subproperty required
                 if (is_array($propertySchema) && $this->isSchema($propertySchema) && isset($propertySchema['required']) && $propertySchema['required'] === true) {
                     $result->addError($this->createError('Property ' . $propertyName . ' is required'));
                 }
             }
         }
     }
     if (isset($schema['patternProperties']) && count($propertyKeysToHandle) > 0 && $this->isDictionary($schema['patternProperties'])) {
         foreach (array_values($propertyKeysToHandle) as $propertyKey) {
             foreach ($schema['patternProperties'] as $propertyPattern => $propertySchema) {
                 $keyResult = $this->validateStringType($propertyKey, ['pattern' => $propertyPattern]);
                 if ($keyResult->hasErrors() === false) {
                     $subresult = $this->validate($value[$propertyKey], $propertySchema);
                     if ($subresult->hasErrors()) {
                         $result->forProperty($propertyKey)->merge($subresult);
                     }
                     $propertyKeysToHandle = array_diff($propertyKeysToHandle, [$propertyKey]);
                 }
             }
         }
     }
     if (isset($schema['formatProperties']) && count($propertyKeysToHandle) > 0 && $this->isDictionary($schema['formatProperties'])) {
         foreach (array_values($propertyKeysToHandle) as $propertyKey) {
             foreach ($schema['formatProperties'] as $propertyPattern => $propertySchema) {
                 $keyResult = $this->validateStringType($propertyKey, ['format' => $propertyPattern]);
                 if ($keyResult->hasErrors() === false) {
                     $subresult = $this->validate($value[$propertyKey], $propertySchema);
                     if ($subresult->hasErrors()) {
                         $result->forProperty($propertyKey)->merge($subresult);
                     }
                     $propertyKeysToHandle = array_diff($propertyKeysToHandle, [$propertyKey]);
                 }
             }
         }
     }
     if (isset($schema['additionalProperties']) && $schema['additionalProperties'] !== true && count($propertyKeysToHandle) > 0) {
         if ($schema['additionalProperties'] === false) {
             foreach ($propertyKeysToHandle as $propertyKey) {
                 $result->forProperty($propertyKey)->addError($this->createError('This property is not allowed here, check the spelling if you think it belongs here.'));
             }
         } else {
             foreach ($propertyKeysToHandle as $propertyKey) {
                 $subresult = $this->validate($value[$propertyKey], $schema['additionalProperties']);
                 if ($subresult->hasErrors()) {
                     $result->forProperty($propertyKey)->merge($subresult);
                 }
             }
         }
     }
     return $result;
 }