ContainsYamlValidator.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace WorkflowBundle\Validator\Constraints;
  3. use Symfony\Component\Validator\Constraint;
  4. use Symfony\Component\Validator\ConstraintValidator;
  5. use Symfony\Component\Yaml\Yaml;
  6. use Symfony\Component\Yaml\Exception\DumpException;
  7. use Symfony\Component\Yaml\Exception\ParseException;
  8. use Symfony\Component\Workflow\Definition;
  9. use Symfony\Component\Workflow\Validator\StateMachineValidator;
  10. use Symfony\Component\Workflow\Validator\WorkflowValidator;
  11. use Symfony\Component\Workflow\Transition;
  12. class ContainsYamlValidator extends ConstraintValidator
  13. {
  14. public function validate($value, Constraint $constraint)
  15. {
  16. $valids_keys = array("initial_place","places","transitions");
  17. $object = $this->context->getObject();
  18. $type = $object->getType();
  19. $markingType = $object->getMarkingType();
  20. $name = $this->context->getObject();
  21. $supports = $object->getSupport();
  22. //Validate Format YAML
  23. try {
  24. $yaml = Yaml::parse($value,100,2);
  25. } catch (DumpException $e) {
  26. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  27. return;
  28. } catch (ParseException $e) {
  29. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  30. return;
  31. }
  32. if(!is_array($yaml)) {
  33. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  34. return;
  35. }
  36. if($yaml) {
  37. $no_valid_key = false;
  38. foreach($yaml as $key => $d) {
  39. if(!in_array($key,$valids_keys)) {
  40. $no_valid_key = true;
  41. $this->context->buildViolation("errors.workflow_template_key_invalid")->setParameter('%key%', $key)->addViolation();
  42. }
  43. }
  44. if($no_valid_key) return;
  45. } else {
  46. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  47. return;
  48. }
  49. //Validate Template SUPPORTS
  50. if(!is_null($supports)) {
  51. if(count($supports) == 0) {
  52. $this->context->buildViolation("errors.workflow_template_supports_empty")->setParameter('%string%', "")->addViolation();
  53. } else {
  54. foreach($supports as $k => $class) {
  55. if(!class_exists($class)) {
  56. $this->context->buildViolation("errors.workflow_template_supports_class_error")->setParameter('%class%', $class)->addViolation();
  57. }
  58. }
  59. }
  60. } else {
  61. $this->context->buildViolation("errors.workflow_template_supports_undefined")->setParameter('%string%', "")->addViolation();
  62. }
  63. //Validate Template PLACES
  64. $places = array();
  65. if(isset($yaml['places'])) {
  66. $places = $yaml['places'];
  67. } else {
  68. $this->context->buildViolation("errors.workflow_template_places_undefined")->setParameter('%string%', "")->addViolation();
  69. }
  70. $transitions = array();
  71. if(isset($yaml['transitions'])) {
  72. foreach($yaml['transitions'] as $k => $data) {
  73. if(isset($data['from']) && isset($data['to'])) {
  74. $transitions[] = new Transition($k,$data['from'],$data['to']);
  75. } else {
  76. $this->context->buildViolation("errors.workflow_template_transitions_format")->setParameter('%transition%', $k)->addViolation();
  77. }
  78. }
  79. } else {
  80. $this->context->buildViolation("errors.workflow_template_transitions_undefined")->setParameter('%string%', "")->addViolation();
  81. }
  82. //Validate Definition
  83. if($type == "workflow") {
  84. $mt = ($markingType == "single_state");
  85. $validator = new WorkflowValidator($mt);
  86. } else {
  87. $validator = new StateMachineValidator();
  88. }
  89. $initialPlace = null;
  90. if(isset($yaml['initial_place'])) {
  91. $initialPlace = $yaml['initial_place'];
  92. }
  93. try {
  94. $definition = new Definition($places,$transitions,$initialPlace);
  95. $validator->validate($definition, $name);
  96. } catch (\Exception $e) {
  97. $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
  98. return;
  99. } catch (\LogicException $e) {
  100. $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
  101. return;
  102. }
  103. }
  104. }