protected function getIdentifierCaster(EntityPropertyMeta $identifierMeta) { if ($identifierMeta->getType() instanceof IntegerType) { $idCaster = function ($idString, $debugKey) { $id = \Psc\Code\Code::castId($idString, FALSE); if ($id === FALSE) { throw new ValidatorRuleException(sprintf("Der Wert '%s' des Schlüssels %s kann nicht zu einer numerischen Id gecastet werden", $idString, $debugKey)); } return $id; }; } elseif ($identifierMeta->getType() instanceof StringType) { $idCaster = function ($idString, $debugKey) { if (!is_string($idString)) { throw new ValidatorRuleException(sprintf("Der Type '%s' des Schlüssels %s muss ein String sein", gettype($idString), $debugKey)); } if (mb_strlen($idString) == 0) { throw new ValidatorRuleException(sprintf("Der Wert '%s' des Schlüssels %s kann nicht zu einem Identifier gecastet werden", $idString, $debugKey)); } return $idString; }; } else { throw new \Psc\Code\NotImplementedException(sprintf('IdCaster für Type %s ist nicht implementiert', $identifierMeta->getType())); } return $idCaster; }
public function validate($data) { if ($data === NULL) { throw new EmptyDataException(); } if (is_string($data)) { $data = trim($data); } $id = Code::castId($data); if ($id > 0) { return $id; } throw new \Psc\Exception('Zahl größer 0 erwartet'); }
/** * @dataProvider provideCastId */ public function testCastId($expected, $id, $default = NULL) { $this->assertEquals($expected, Code::castId($id, $default)); }