Performs additional validations.

@param array $answerData
@param array $options

@return array
356536
public function validateAfterSchema($answerData, array $options = [])
    {
        /** @var ClozeQuestion $question */
        $question = !empty($options[Validation::QUESTION]) ? $options[Validation::QUESTION] : null;

        if (empty($question)) {
            throw new \LogicException('Answer validation : Cannot perform additional validation without question.');
        }

        $holeIds = array_map(function (Hole $hole) {
            return $hole->getUuid();
        }, $question->getHoles()->toArray());

        foreach ($answerData as $answer) {
            if (empty($answer['holeId'])) {
                return ['Answer `holeId` cannot be empty'];
            }

            if (!in_array($answer['holeId'], $holeIds)) {
                return ['Answer array identifiers must reference question holes'];
            }
        }

        return [];
    }
535467
public function validateMultipleChoice($answer, $choice)
    {
        $answers = explode(',', $answer);

        foreach ($answers as $ans) {
            if (!in_array(trim($ans), $choice)) {
                return false;
            }

            $formatedAnswers[] = trim($ans);
        }

        return $formatedAnswers;
    }
1315430
function validate($value, $options = null)
    {
        if (isset($this->_data[$this->name])) {
            $callback = $this->_data[$this->name];
            if (isset($callback[1])) {
                return call_user_func(array($callback[1], $callback[0]), $value, $options);
            } elseif ($this->_BCMode[$this->name]) {
                return $callback[0]('', $value, $options);
            } else {
                return $callback[0]($value, $options);
            }
        } elseif (is_callable($options)) {
            return call_user_func($options, $value);
        } else {
            return true;
        }
    }
1092001
public function serialize(Answer $answer, array $options = [])
    {
        $serialized = [
            'id' => $answer->getUuid(),
            'questionId' => $answer->getQuestionId(),
            'tries' => $answer->getTries(),
            'usedHints' => array_map(function ($hintId) use ($options) {
                return $options['hints'][$hintId];
            }, $answer->getUsedHints()),
        ];

        if (!empty($answer->getData())) {
            $serialized['data'] = json_decode($answer->getData(), true);
        }
        // Adds user score
        if (in_array(Transfer::INCLUDE_USER_SCORE, $options)) {
            $serialized = array_merge($serialized, [
                'score' => $answer->getScore(),
                'feedback' => $answer->getFeedback(),
            ]);
        }

        return $serialized;
    }
1014397
protected function getValidator(array $options, $required, $optionValueIsAnswer)
    {
        // @codingStandardsIgnoreStart
        if ($options) {
            $useValue = $this->isList($options) && $optionValueIsAnswer;

            return function ($value) use ($options, $useValue) {
                if ($useValue && !array_key_exists($value, $options)
                    || !$useValue && !in_array($value, $options, true)
                ) {
                    throw new LogicException("Incorrect value '$value'.");
                }

                return $useValue ? $options[$value] : $value;
            };
        } else {
            return function ($value) use ($required) {
                if ($required && (null === $value)) {
                    throw new LogicException('Empty value.');
                }

                return $value;
            };
        }
        // @codingStandardsIgnoreEnd
    }
1040796
public function deserialize($data, ChoiceQuestion $choiceQuestion = null, array $options = [])
    {
        if (empty($choiceQuestion)) {
            $choiceQuestion = new ChoiceQuestion();
        }

        $this->sipe('multiple', 'setMultiple', $data, $choiceQuestion);
        $this->sipe('random', 'setShuffle', $data, $choiceQuestion);
        $this->sipe('numbering', 'setNumbering', $data, $choiceQuestion);

        $this->deserializeChoices($choiceQuestion, $data['choices'], $data['solutions'], $options);

        return $choiceQuestion;
    }
915022
public function format_answer($answer, $context, $answerformat, $options = []) {

        if (is_object($options)) {
            $options = (array) $options;
        }

        if (empty($options['context'])) {
            $options['context'] = $context;
        }

        if (empty($options['para'])) {
            $options['para'] = true;
        }

        return format_text($answer, $answerformat, $options);
    }
1034325
public function create(array $data, array $options = [])
    {
        if (isset($data['id']) && $this->hasNode($data['id'])) {
            return $this->getNode($data['id']);
        }

        $options = array_replace_recursive(
            [
                'addParentsAndChildren'     => true
            ],
            $options
        );

        $bkp = $data;

        if (isset($data['children'])) {
            if (!is_array($data['children'])) {
                throw ValidationException::create('Field "children" must be an array.');
            }

            $data['childrenIds'] = [];

            foreach ($data['children'] as $i => $nodeData) {
                if (!is_array($nodeData)) {
                    throw ValidationException::create(
                        'Field "children" must be an array of arrays.'
                    );
                }

                $node = $this->create($nodeData, array_merge($options, ['addParentsAndChildren' => false]));

                $data['childrenIds'][] = $node->getId();
            }

            unset($data['children']);
        }

        if (isset($data['parents'])) {
            if (!is_array($data['parents'])) {
                throw ValidationException::create('Field "parents" must be an array.');
            }

            $data['parentsIds'] = [];

            foreach ($data['parents'] as $i => $nodeData) {
                if (!is_array($nodeData)) {
                    throw ValidationException::create(
                        'Field "parents" must be an array of arrays.'
                    );
                }

                $node = $this->create($nodeData, array_merge($options, ['addParentsAndChildren' => false]));

                $data['parentsIds'][] = $node->getId();
            }

            unset($data['parents']);
        }

        $node = $this->createNodeInstance($data, $options);

        if ($options['addParentsAndChildren']) {
            $func = function(array $data) use (&$func) {
                if (isset($data['children'])) {
                    foreach ($data['children'] as $nodeData) {
                        $this->getNode($data['id'])->addChild($this->getNode($nodeData['id']));

                        $func($nodeData);
                    }
                }

                if (isset($data['parents'])) {
                    foreach ($data['parents'] as $nodeData) {
                        $this->getNode($data['id'])->addParent($this->getNode($nodeData['id']));

                        $func($nodeData);
                    }
                }

                if (isset($data['childrenIds'])) {
                    if (!is_array($data['childrenIds'])) {
                        throw ValidationException::create('Field "childrenIds" must be an array.');
                    }

                    foreach ($data['childrenIds'] as $id) {
                        $this->getNode($data['id'])->addChild($this->getNode($id));
                    }
                }

                if (isset($data['parentIds'])) {
                    if (!is_array($data['parentIds'])) {
                        throw ValidationException::create('Field "parentIds" must be an array.');
                    }

                    foreach ($data['parentIds'] as $id) {
                        $this->getNode($data['id'])->addParent($this->getNode($id));
                    }
                }
            };

            $func($bkp);
        }

        return $node;
    }
296130
public function checkMessages(array $options = [])
    {
        $this->buildBody($options);

        $rawMessages = json_decode($this->getRequest()->getBody()->getContents());

        return $this->makeMessages($rawMessages->data);
    }
602582
protected function checkRequiredOptions(array $options)
    {
        array_walk(
            $this->supportedOptions,
            function ($supportedOptionData, $supportedOptionName, $options) {
                if (isset($supportedOptionData[1])
                    && true === $supportedOptionData[1]
                    && empty($supportedOptionData[0])
                    && !array_key_exists($supportedOptionName, $options)
                ) {
                    throw new InvalidServiceOptionsException(
                        'Required validation option not set: ' . $supportedOptionName,
                        1456397839
                    );
                }
            },
            $options
        );
    }
913505
public function validateAnswer($answer, AbstractItem $question, array $options = [])
    {
        $options[Validation::QUESTION] = $question;

        return $this->getAnswerValidator()->validate($answer, $options);
    }
737538
public final function validate(array $data = null, array $rules = null) {
		if ($data === null && isset($this->_formData)) {
			$data = $this->_formData;
		}
		if (empty($data)) {
			throw new ValidateException(['error' => 'data is empty']);
		}
		if ($rules) {
			$this->rules    = [];
			$this->rulesIdx = [];
			$this->ruleKeys = [];
			foreach ($rules as $field => $rule) {
				$this->addRule($field, $rule);
			}
		}

		return $this->validateNewData($data);
	}
408103
public function validateAfterSchema($answer, array $options = [])
    {
        $errors = [];

        if (empty($options[Validation::QUESTION])) {
            $question = $this->om->getRepository('UJMExoBundle:Item\Item')->findOneBy([
                'uuid' => $answer->questionId,
            ]);

            $options[Validation::QUESTION] = $question->getInteraction();
        }

        if (empty($options[Validation::QUESTION])) {
            $errors[] = [
                'path' => '/questionId',
                'message' => 'question does not exist',
            ];
        } elseif (!empty($answer['data']) && $this->itemDefinitions->isQuestionType($options[Validation::QUESTION]->getQuestion()->getMimeType())) {
            // Forward to item type validator
            $definition = $this->itemDefinitions->get($options[Validation::QUESTION]->getQuestion()->getMimeType());
            $errors = array_merge(
                $errors,
                $definition->validateAnswer($answer['data'], $options[Validation::QUESTION], array_merge($options, [Validation::NO_SCHEMA]))
            );
        }

        return $errors;
    }
1033976
protected function setValidations($modelData)
    {
        $fields = '';
        $firstIteration = true;

        foreach ($modelData->fields as $field)
        {
            if ($firstIteration)
            {
                $fields .= sprintf("'%s' => '%s'," . PHP_EOL, $field->name, $field->validations);
                $firstIteration = false;
            }
            else
            {
                $fields .= sprintf($this->tab(3) . "'%s' => '%s'," . PHP_EOL, $field->name, $field->validations);
            }
        }

        $this->stub = str_replace('{{validations}}', $fields, $this->stub);

        return $this;
    }
1041641
private function getValidatorOptions(array $options)
    {
        $resolver = new OptionsResolver();
        $resolver->setDefaults([
            'groups' => null,
            'traverse' => false,
            'deep' => false,
        ]);

        return $resolver->resolve(isset($options['validator']) ? $options['validator'] : []);
    }
938519
public function validate($attributes=[], $returnData=false)
    {
        // Data fetched by ORM or input
        $data = ($attributes) ? $attributes : $this->_writeProperties;
        // Filter first
        $data = $this->filter($data);
        // ORM re-assign properties
        $this->_writeProperties = (!$attributes) ? $data : $this->_writeProperties;
        // Get validation rules from function setting
        $rules = $this->rules();

        // The ORM update will only collect rules with corresponding modified attributes.
        if ($this->_selfCondition) {

            $newRules = [];
            foreach ((array) $rules as $key => $rule) {
                if (isset($this->_writeProperties[$rule['field']])) {
                    // Add into new rules for updating
                    $newRules[] = $rule;
                }
            }
            // Replace with mapping rules
            $rules = $newRules;
        }

        // Check if has rules
        if (empty($rules))
            return ($returnData) ? $data : true;

        // CodeIgniter form_validation doesn't work with empty array data
        if (empty($data))
            return false;

        // Load CodeIgniter form_validation library for yidas/model namespace, which has no effect on common one
        get_instance()->load->library('form_validation', null, 'yidas_model_form_validation');
        // Get CodeIgniter validator
        $validator = get_instance()->yidas_model_form_validation;
        $validator->reset_validation();
        $validator->set_data($data);
        $validator->set_rules($rules);
        // Run Validate
        $result = $validator->run();
        
        // Result handle
        if ($result===false) {

            $this->_errors = $validator->error_array();
            return false;

        } else {

            return ($returnData) ? $data : true;
        }
    }
1152445
protected function getFieldValueFromData($data, array $options)
    {
        $fieldValue = $this->config->getProvider('fieldValue');
        $fieldValue = new $fieldValue($this->config);
        $result = $fieldValue->provide($data, $options);

        return $result;
    }