ContainsYamlValidator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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("supports","initial_place","places","transitions");
  17. $object = $this->context->getObject();
  18. $type = $object->getType();
  19. $markingType = $object->getMarkingType();
  20. $name = $this->context->getObject();;
  21. //Validate Format YAML
  22. try {
  23. $yaml = Yaml::parse($value,100,2);
  24. } catch (DumpException $e) {
  25. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  26. return;
  27. } catch (ParseException $e) {
  28. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  29. return;
  30. }
  31. if($yaml) {
  32. $no_valid_key = false;
  33. foreach($yaml as $key => $d) {
  34. if(!in_array($key,$valids_keys)) {
  35. $no_valid_key = true;
  36. $this->context->buildViolation("errors.workflow_template_key_invalid")->setParameter('%key%', $key)->addViolation();
  37. }
  38. }
  39. if($no_valid_key) return;
  40. } else {
  41. $this->context->buildViolation("errors.workflow_template_format_error")->setParameter('%string%', "")->addViolation();
  42. return;
  43. }
  44. //Validate Template SUPPORTS
  45. if(isset($yaml['supports'])) {
  46. if(!is_array($yaml['supports'])) {
  47. $this->context->buildViolation("errors.workflow_template_supports_error")->setParameter('%string%', "")->addViolation();
  48. } elseif(count($yaml['supports']) == 0) {
  49. $this->context->buildViolation("errors.workflow_template_supports_empty")->setParameter('%string%', "")->addViolation();
  50. } else {
  51. foreach($yaml['supports'] as $k => $class) {
  52. if(!class_exists($class)) {
  53. $this->context->buildViolation("errors.workflow_template_supports_class_error")->setParameter('%class%', $class)->addViolation();
  54. }
  55. }
  56. }
  57. } else {
  58. $this->context->buildViolation("errors.workflow_template_supports_undefined")->setParameter('%string%', "")->addViolation();
  59. }
  60. //Validate Template PLACES
  61. $places = array();
  62. if(isset($yaml['places'])) {
  63. $places = $yaml['places'];
  64. } else {
  65. $this->context->buildViolation("errors.workflow_template_places_undefined")->setParameter('%string%', "")->addViolation();
  66. }
  67. $transitions = array();
  68. if(isset($yaml['transitions'])) {
  69. foreach($yaml['transitions'] as $k => $data) {
  70. $transitions[] = new Transition($k,$data['from'],$data['to']);
  71. }
  72. } else {
  73. $this->context->buildViolation("errors.workflow_template_transitions_undefined")->setParameter('%string%', "")->addViolation();
  74. }
  75. //Validate Definition
  76. if($type == "workflow") {
  77. $mt = ($markingType == "single_state");
  78. $validator = new WorkflowValidator($mt);
  79. } else {
  80. $validator = new StateMachineValidator();
  81. }
  82. $initialPlace = null;
  83. if(isset($yaml['initial_place'])) {
  84. $initialPlace = $yaml['initial_place'];
  85. }
  86. try {
  87. $definition = new Definition($places,$transitions,$initialPlace);
  88. $validator->validate($definition, $name);
  89. } catch (\Exception $e) {
  90. $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
  91. return;
  92. } catch (\LogicException $e) {
  93. $this->context->buildViolation("Exception: ".$e->getMessage())->setParameter('%string%', "")->addViolation();
  94. return;
  95. }
  96. }
  97. }