/** * @param \Nella\Validator\ClassMetadata */ public function parse(\Nella\Validator\ClassMetadata $metadata) { $reflection = $metadata->getClassReflection(); foreach ($reflection->getProperties() as $property) { if ($property->hasAnnotation('validate')) { $rules = (array) $property->getAnnotation('validate'); foreach ($rules as $key => $value) { if (is_int($key)) { $key = $value; $value = NULL; } $metadata->addRule($property->getName(), $key, $value); } } } }
/** * @expectedException Nette\InvalidStateException */ public function testRuleExistingRule() { $this->metadata->addRule('test', "foo"); $this->metadata->addRule('test', "foo"); }
/** * @param \Nella\Validator\ClassMetadata */ public function parse(\Nella\Validator\ClassMetadata $metadata) { $ref = $metadata->getClassReflection(); if ($ref->implementsInterface('Nella\Models\IEntity') && ($ref->hasAnnotation('entity') || $ref->hasAnnotation('mappedSuperClass'))) { $emeta = $this->container->getEntityManager()->getClassMetadata($ref->getName()); foreach ($emeta->fieldMappings as $field) { barDump($field, $ref->getName() . "::\$$" . $field['fieldName']); if (isset($field['declared']) && $field['declared'] != $ref->getName()) { continue; } switch ($field['type']) { case 'integer': case 'smallint': case 'bigint': $metadata->addRule($field['fieldName'], Validator::TYPE, 'int'); break; case 'decimal': case 'float': $metadata->addRule($field['fieldName'], Validator::TYPE, 'double'); break; case 'boolean': case 'object': case 'array': $metadata->addRule($field['fieldName'], Validator::TYPE, $field['type']); break; case 'string': $metadata->addRule($field['fieldName'], Validator::TYPE, $field['type']); $metadata->addRule($field['fieldName'], Validator::MAX_LENGTH, $field['length'] ?: 255); break; case 'date': case 'time': case 'datetime': case 'datetimetz': $metadata->addRule($field['fieldName'], Validator::INSTANCE, 'DateTime'); break; case 'text': $metadata->addRule($field['fieldName'], Validator::TYPE, 'string'); break; } if (!isset($field['id']) || !$field['id']) { $metadata->addRule($field['fieldName'], $field['nullable'] ? Validator::NULLABLE : Validator::NOTNULL); } else { $metadata->addRule($field['fieldName'], Validator::NULLABLE); } } foreach ($emeta->associationMappings as $field) { if ($field['type'] == ClassMetadata::ONE_TO_ONE || $field['type'] == ClassMetadata::MANY_TO_ONE) { if (isset($field['declared']) && $field['declared'] != $ref->getName()) { continue; } $metadata->addRule($field['fieldName'], Validator::INSTANCE, $field['targetEntity']); $metadata->addRule($field['fieldName'], $field['joinColumns'][0]['nullable'] ? Validator::NULLABLE : Validator::NOTNULL ); } } } }