|
@@ -4,21 +4,108 @@ 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)
|
|
|
{
|
|
|
- $yaml = Yaml::dump($value,100,2);
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- // if (!preg_match('/^[a-zA-Z0-9]+$/', $value, $matches)) {
|
|
|
- // $this->context->buildViolation($constraint->message)
|
|
|
- // ->setParameter('%string%', $value)
|
|
|
- // ->addViolation();
|
|
|
- // }
|
|
|
+ $valids_keys = array("supports","initial_place","places","transitions");
|
|
|
+
|
|
|
+ $object = $this->context->getObject();
|
|
|
+ $type = $object->getType();
|
|
|
+ $markingType = $object->getMarkingType();
|
|
|
+ $name = $this->context->getObject();;
|
|
|
+
|
|
|
+ //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($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(isset($yaml['supports'])) {
|
|
|
+ if(!is_array($yaml['supports'])) {
|
|
|
+ $this->context->buildViolation("errors.workflow_template_supports_error")->setParameter('%string%', "")->addViolation();
|
|
|
+ } elseif(count($yaml['supports']) == 0) {
|
|
|
+ $this->context->buildViolation("errors.workflow_template_supports_empty")->setParameter('%string%', "")->addViolation();
|
|
|
+ } else {
|
|
|
+ foreach($yaml['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) {
|
|
|
+
|
|
|
+ $transitions[] = new Transition($k,$data['from'],$data['to']);
|
|
|
+ }
|
|
|
+ } 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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
}
|