/**
  *
  * @param \Sds\DoctrineExtensions\Generator\GenerateEventArgs $eventArgs
  */
 public function generatorDojoModelValidator(GenerateEventArgs $eventArgs)
 {
     $metadata = $eventArgs->getDocumentManager()->getClassMetadata($eventArgs->getClassName());
     $options = $eventArgs->getOptions();
     $resource = $eventArgs->getResource();
     $defaultMixins = $this->getDefaultMixins();
     $templateArgs = [];
     $hasMultiFieldValidator = array_key_exists(MultiFieldValidator::getResourceName($metadata->name), $metadata->generator);
     $params = ['validators' => []];
     if (isset($options['mixins'])) {
         $templateArgs['dependencyMids'] = $options['mixins'];
     } else {
         $templateArgs['dependencyMids'] = $defaultMixins['validator']['model'];
     }
     $templateArgs['mixins'] = $this->namesFromMids($templateArgs['dependencyMids']);
     $templateArgs['dependencies'] = $this->namesFromMids($templateArgs['dependencyMids']);
     if ($hasMultiFieldValidator) {
         $templateArgs['dependencyMids'][] = MultiFieldValidator::getMid($metadata->name);
         $templateArgs['dependencies'][] = 'MultiFieldValidator';
         $params['validators'][] = new Expr('new MultiFieldValidator');
     }
     foreach ($this->getSerializer()->fieldListForUnserialize($metadata) as $field) {
         if (array_key_exists(Validator::getResourceName($metadata->name, $field), $metadata->generator)) {
             $templateArgs['dependencyMids'][] = Validator::getMid($metadata->name, $field);
             $templateArgs['dependencies'][] = ucfirst($field) . 'Validator';
             $params['validators'][] = new Expr('new ' . ucfirst($field) . 'Validator');
         }
     }
     $templateArgs['dependencyMids'] = ',' . $this->indent($this->implodeMids($templateArgs['dependencyMids']));
     $templateArgs['dependencies'] = ',' . $this->indent($this->implodeNames($templateArgs['dependencies']));
     $templateArgs['mixins'] = $this->indent($this->implodeNames($templateArgs['mixins']), 12) . $this->indent("\n", 8);
     $templateArgs['params'] = $this->implodeParams($params);
     $templateArgs['comment'] = $this->indent("// Will return a validator to validate a complete model for {$metadata->name}");
     $resource->content = $this->populateTemplate(file_get_contents(__DIR__ . '/Template/Module.js.template'), $templateArgs);
     $this->persistToFile($this->getFilePath($metadata->name), $resource->content);
 }
 public function postBuildMetadata(LoadClassMetadataEventArgs $eventArgs)
 {
     $metadata = $eventArgs->getClassMetadata();
     if (!isset($metadata->generator)) {
         return;
     }
     //Add implied generatable inputs
     if (array_key_exists(Generator\Form::getResourceName($metadata->name), $metadata->generator)) {
         foreach ($this->getSerializer()->fieldListForUnserialize($metadata) as $field) {
             if (!array_key_exists(Generator\Input::getResourceName($metadata->name, $field), $metadata->generator)) {
                 $this->annotationDojoInput(new AnnotationEventArgs($metadata, EventType::field, new Sds\Dojo\Input([]), $metadata->reflFields[$field], $eventArgs->getDocumentManager()->getEventManager()));
             }
         }
     }
     //Add implied generatable validators
     foreach ($this->getSerializer()->fieldListForUnserialize($metadata) as $field) {
         if (array_key_exists(Generator\Input::getResourceName($metadata->name, $field), $metadata->generator) && array_key_exists($field, $metadata->validator['fields']) && !array_key_exists(Generator\Validator::getResourceName($metadata->name, $field), $metadata->generator)) {
             $this->annotationDojoValidator(new AnnotationEventArgs($metadata, EventType::field, new Sds\Dojo\Validator([]), $metadata->reflFields[$field], $eventArgs->getDocumentManager()->getEventManager()));
         }
     }
     //Add implied multifieldvalidator
     if ((array_key_exists(Generator\Form::getResourceName($metadata->name), $metadata->generator) || array_key_exists(Generator\ModelValidator::getResourceName($metadata->name), $metadata->generator)) && array_key_exists('document', $metadata->validator) && !array_key_exists(Generator\MultiFieldValidator::getResourceName($metadata->name), $metadata->generator)) {
         $this->annotationDojoMultiFieldValidator(new AnnotationEventArgs($metadata, EventType::document, new Sds\Dojo\MultiFieldValidator([]), $metadata->reflClass, $eventArgs->getDocumentManager()->getEventManager()));
     }
 }
Пример #3
0
 /**
  *
  * @param \Sds\DoctrineExtensions\Generator\GenerateEventArgs $eventArgs
  */
 public function generatorDojoInput(GenerateEventArgs $eventArgs)
 {
     $metadata = $eventArgs->getDocumentManager()->getClassMetadata($eventArgs->getClassName());
     $options = $eventArgs->getOptions();
     $resource = $eventArgs->getResource();
     $field = $options['field'];
     $defaultMixins = $this->getDefaultMixins();
     $templateArgs = [];
     $hasValidator = array_key_exists(Validator::getResourceName($metadata->name, $field), $metadata->generator);
     $params = [];
     if ($hasValidator) {
         switch ($metadata->fieldMappings[$field]['type']) {
             case 'int':
                 $defaultMids = $defaultMixins['input']['intWithValidator'];
                 break;
             case 'float':
                 $defaultMids = $defaultMixins['input']['floatWithValidator'];
                 break;
             case 'custom_id':
                 $params['type'] = "hidden";
             case 'string':
             default:
                 $defaultMids = $defaultMixins['input']['stringWithValidator'];
                 break;
         }
         if (isset($options['mixins'])) {
             $templateArgs['dependencyMids'] = $options['mixins'];
         } else {
             $templateArgs['dependencyMids'] = $defaultMids;
         }
         $templateArgs['dependencies'] = $this->namesFromMids($templateArgs['dependencyMids']);
         $templateArgs['mixins'] = $this->namesFromMids($templateArgs['dependencyMids']);
         $templateArgs['dependencyMids'][] = Validator::getMid($metadata->name, $field);
         $templateArgs['dependencies'][] = ucfirst($field) . 'Validator';
         $params['validator'] = new Expr('new ' . ucfirst($field) . 'Validator');
     } else {
         switch ($metadata->fieldMappings[$field]['type']) {
             case 'int':
                 $defaultMids = $defaultMixins['input']['int'];
                 break;
             case 'float':
                 $defaultMids = $defaultMixins['input']['float'];
                 break;
             case 'custom_id':
                 $params['type'] = 'hidden';
             case 'string':
             default:
                 $defaultMids = $defaultMixins['input']['string'];
                 break;
         }
         if (isset($options['mixins'])) {
             $templateArgs['dependencyMids'] = $options['mixins'];
         } else {
             $templateArgs['dependencyMids'] = $defaultMids;
         }
         $templateArgs['dependencies'] = $this->namesFromMids($templateArgs['dependencyMids']);
         $templateArgs['mixins'] = $this->namesFromMids($templateArgs['dependencyMids']);
     }
     $params['name'] = $field;
     //Camel case splitting regex
     $regex = '/(?<=[a-z])(?=[A-Z])/x';
     $params['label'] = ucfirst(implode(' ', preg_split($regex, $field)));
     if (isset($options['params'])) {
         foreach ($options['params'] as $key => $value) {
             $params[$key] = $value;
         }
     }
     $templateArgs['dependencyMids'] = ',' . $this->indent($this->implodeMids($templateArgs['dependencyMids']));
     $templateArgs['dependencies'] = ',' . $this->indent($this->implodeNames($templateArgs['dependencies']));
     $templateArgs['mixins'] = $this->indent($this->implodeNames($templateArgs['mixins']), 12) . $this->indent("\n", 8);
     $templateArgs['params'] = $this->implodeParams($params);
     $templateArgs['comment'] = $this->indent("// Will return an input for the {$field} field");
     $resource->content = $this->populateTemplate(file_get_contents(__DIR__ . '/Template/Module.js.template'), $templateArgs);
     $this->persistToFile($this->getFilePath($metadata->name, $field), $resource->content);
 }