Example #1
0
 public function validate($data)
 {
     if (empty($data)) {
         $e = new EmptyDataException();
         $e->setDefaultValue(array());
         throw $e;
     }
     $data = json_decode($data, true);
     if (count($data) == 0) {
         $e = new EmptyDataException();
         $e->setDefaultValue(array());
         throw $e;
     }
     $rep = $this->em->getRepository($this->entityName);
     $collection = $rep->findAllByIds($data, $this->idName);
     // überspringt dies fehlende Ids?
     // sortieren nach der Data List (da findAllByIds das nicht tut, und das ist auch gut so)
     $getter = \Psc\Code\Code::castGetter($this->idName);
     $keys = array_flip($data);
     usort($collection, function ($entityA, $entityB) use($keys, $getter) {
         $keyA = $keys[$getter($entityA)];
         $keyB = $keys[$getter($entityB)];
         if ($keyA === $keyB) {
             return 0;
         }
         return $keyA > $keyB ? 1 : -1;
     });
     if (count($collection) == count($data)) {
         return $collection;
     } else {
         throw new \Psc\Exception('Unexpected: Es wurden: ' . count($collection) . ' IDs hydriert, aber ' . count($data) . ' IDs angegeben.');
     }
 }
Example #2
0
 public function validate($data)
 {
     $e = new EmptyDataException();
     $e->setDefaultValue(array());
     if ($data === NULL) {
         throw $e;
     }
     if (trim($data) == ',') {
         throw $e;
     }
     $cb = new Callback($this->tagsClass, 'toCollection');
     return $cb->call(array($data));
 }
 /**
  * 
  * Achtung! nicht bool zurückgeben, stattdessen irgendeine Exception schmeissen
  * 
  * Wird EmptyDataException bei validate() geworfen und ist der Validator mit optional aufgerufen worden, wird keine ValidatorException geworfen
  * 
  * @return $data
  */
 public function validate($data)
 {
     if ($data === NULL) {
         throw EmptyDataException::factory(NULL);
     }
     $repository = $this->dc->getRepository($this->entityMeta->getClass());
     try {
         return $repository->hydrate($data);
     } catch (\Psc\Doctrine\EntityNotFoundException $e) {
         throw EmptyDataException::factory(NULL, $e);
     }
 }
Example #4
0
 public function validate($data)
 {
     if ($data === NULL) {
         throw EmptyDataException::factory(array());
     }
     $data = Code::castArray($data);
     if (count($data) == 0) {
         throw EmptyDataException::factory(array());
     }
     if (count($data) > 0) {
         return $data;
     }
     throw new \Psc\Exception('Konnte nicht validiert werden');
 }
 /**
  * 
  * Wird EmptyDataException bei validate() geworfen und ist der Validator mit optional aufgerufen worden, wird keine ValidatorException geworfen
  * @return $data
  */
 public function validate($data = NULL)
 {
     if ($data === NULL || $data === array()) {
         throw EmptyDataException::factory(new \Psc\Data\ArrayCollection());
     }
     if (!is_array($data)) {
         throw new \InvalidArgumentException('Parameter ist als Array erwartet. ' . Code::varInfo($data) . ' wurde übergeben');
     }
     $identifierCaster = $this->getIdentifierCaster($this->entityMeta->getIdentifier());
     $repository = $this->dc->getRepository($this->entityMeta->getClass());
     $entities = new ArrayCollection();
     foreach ($data as $key => $identifierString) {
         $identifier = $identifierCaster($identifierString, $key);
         $entities->add($repository->hydrate($identifier));
     }
     return $entities;
 }
 /**
  *
  * Empty Data ist NULL
  * 
  * @return DateTime
  */
 public function validate($data)
 {
     if (is_integer($data) && $data > 0) {
         return new DateTime($data);
     }
     if ($data === NULL || !is_array($data) || !array_key_exists('date', $data) || $data['date'] == NULL || !$this->timeIsOptional && (!array_key_exists('time', $data) || $data['time'] == NULL)) {
         throw EmptyDataException::factory(NULL);
     }
     $data['time'] = !isset($data['time']) ? NULL : trim($data['time']);
     $dateRule = new DateValidatorRule('d.m.Y');
     $date = $dateRule->validate($data['date']);
     // cooler: time validator rule
     if ($this->timeIsOptional && $data['time'] == NULL) {
         return new DateTime($date);
         // sets time to 00:00
     } else {
         return DateTime::parse('d.m.Y H:i', $date->format('d.m.Y') . ' ' . $data['time']);
     }
 }
Example #7
0
 public function validate($data)
 {
     if ($data === NULL) {
         throw EmptyDataException::factory(0.0);
     }
     if ($data === '') {
         throw EmptyDataException::factory(0.0);
     }
     if (is_string($data)) {
         $float = \Psc\Code\Numbers::parseFloat($data, '.', ',');
     } elseif (is_numeric($data)) {
         $float = (double) $data;
     } else {
         throw new Exception('Parsing von ' . $data . ' war nicht möglich. String|Integer erwartet');
     }
     if (!is_float($float)) {
         throw new Exception('Parsing von ' . $data . ' war nicht möglich');
     }
     if (!$this->zero && $float == 0) {
         throw new Exception('0 ist nicht erlaubt');
     }
     return $float;
 }