123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <?php
- namespace WorkflowBundle\Validator\Constraints;
- use Symfony\Component\Validator\Constraint;
- use Symfony\Component\Validator\ConstraintValidator;
- use Symfony\Component\Yaml\Yaml;
- use Symfony\Component\Yaml\Exception\DumpException;
- use Symfony\Component\Yaml\Exception\ParseException;
- use Symfony\Component\Workflow\Definition;
- use Symfony\Component\Workflow\Validator\StateMachineValidator;
- use Symfony\Component\Workflow\Validator\WorkflowValidator;
- use Symfony\Component\Workflow\Transition;
- class ContainsYamlValidator extends ConstraintValidator
- {
- public function validate($value, Constraint $constraint)
- {
- $valids_keys = array("initial_place","places","transitions");
- $object = $this->context->getObject();
- $type = $object->getType();
- $markingType = $object->getMarkingType();
- $name = $this->context->getObject();
- $supports = $object->getSupport();
- //Validate Format YAML
- try {
- $yaml = Yaml::parse($value,100,2);
- } catch (DumpException $e) {
- $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
- return;
- } catch (ParseException $e) {
- $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
- return;
- }
- if(!is_array($yaml)) {
- $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
- return;
- }
- if($yaml) {
- $no_valid_key = false;
- foreach($yaml as $key => $d) {
- if(!in_array($key,$valids_keys)) {
- $no_valid_key = true;
- $this->context->buildViolation("errors.workflow_template_key_invalid")->setParameter('%key%', $key)->addViolation();
- }
- }
- if($no_valid_key) return;
- } else {
- $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
- return;
- }
- //Validate Template SUPPORTS
- if(!is_null($supports)) {
- if(count($supports) == 0) {
- $this->context->buildViolation("errors.workflow_template_supports_empty")->setParameter('%string%', "")->addViolation();
- } else {
- foreach($supports as $k => $class) {
- if(!class_exists($class)) {
- $this->context->buildViolation("errors.workflow_template_supports_class_error")->setParameter('%class%', $class)->addViolation();
- }
- }
- }
- } else {
- $this->context->buildViolation("errors.workflow_template_supports_undefined")->setParameter('%string%', "")->addViolation();
- }
- //Validate Template PLACES
- $places = array();
- if(isset($yaml['places'])) {
- $places = $yaml['places'];
- } else {
- $this->context->buildViolation("errors.workflow_template_places_undefined")->setParameter('%string%', "")->addViolation();
- }
- $transitions = array();
- if(isset($yaml['transitions'])) {
- foreach($yaml['transitions'] as $k => $data) {
- if(isset($data['from']) && isset($data['to'])) {
- $transitions[] = new Transition($k,$data['from'],$data['to']);
- } else {
- $this->context->buildViolation("errors.workflow_template_transitions_format")->setParameter('%transition%', $k)->addViolation();
- }
- }
- } else {
- $this->context->buildViolation("errors.workflow_template_transitions_undefined")->setParameter('%string%', "")->addViolation();
- }
- //Validate Definition
- if($type == "workflow") {
- $mt = ($markingType == "single_state");
- $validator = new WorkflowValidator($mt);
- } else {
- $validator = new StateMachineValidator();
- }
- $initialPlace = null;
- if(isset($yaml['initial_place'])) {
- $initialPlace = $yaml['initial_place'];
- }
- try {
- $definition = new Definition($places,$transitions,$initialPlace);
- $validator->validate($definition, $name);
- } catch (\Exception $e) {
- $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
- return;
- } catch (\LogicException $e) {
- $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
- return;
- }
- }
- }
|