create() публичный статический Метод

Create instance validator.
public static create ( ) : Validator
Результат Validator
 /**
  * @RPC\Route("/api/auth/login")
  * @RPC\Method("POST")
  */
 public function login()
 {
     $credentials = json_decode($this->request->getContent(), true);
     try {
         v::create()->key('email', v::notEmpty())->key('password', v::notEmpty())->assert($credentials);
     } catch (ValidationException $e) {
         $errors = $e->findMessages(['email', 'password']);
         throw new \pmill\Doctrine\Rest\Exception\ValidationException($errors);
     }
     $password = $credentials['password'];
     unset($credentials['password']);
     /** @var User $user */
     $user = $this->authenticationService->authenticateWithCredentials(User::class, $credentials, $password);
     $token = $this->authenticationService->generateTokenFromObject($user);
     return ['token' => $token];
 }
Пример #2
0
 public function testStaticCreateShouldReturnNewValidator()
 {
     $this->assertInstanceOf(Validator::class, Validator::create());
 }
Пример #3
0
 public function testStaticCreateShouldReturnNewValidator()
 {
     $this->assertInstanceOf('Respect\\Validation\\Validator', Validator::create());
 }
 protected function validator()
 {
     return Validator::create();
 }
 /**
  * @param $data array
  *
  */
 public function updateUser(array $data)
 {
     $stringValidator = v::stringType()->length(1, 128);
     $idValidator = v::numeric();
     $userValidator = v::create();
     $userValidator->key('id', $idValidator);
     $userValidator->key('username', $stringValidator);
     $userValidator->key('fullname', $stringValidator);
     $userValidator->key('password', $stringValidator, false);
     //optional
     try {
         $userValidator->assert($data);
     } catch (NestedValidationException $exception) {
         $this->writeFail($exception->getFullMessage());
         return;
     }
     //see if username is changing if so, check it is available
     $currentUserData = UserModel::getUser($data['id']);
     if ($data['username'] !== $currentUserData['username'] && UserModel::isUsernameAvailable($data['username'])) {
         $this->writeFail(["Username is not available"]);
         return;
     }
     //then check the current user is either admin or editing his own
     if ($this->isUserAuthenticated() && ($this->currentUser->isAdmin() || $this->currentUser->id === $data["id"])) {
         UserModel::updateUser($data);
     } else {
         $this->writeUnauthorized();
     }
 }
 private function updateTrip($data)
 {
     // Create validators
     $stringValidator = v::create();
     $stringValidator->stringType();
     $idValidator = v::numeric();
     $dateValidator = v::date('Y-m-d');
     $tripValidator = v::create();
     $tripValidator->key('user_id', $idValidator);
     $tripValidator->key('destination', $stringValidator);
     $tripValidator->key('start_date', $dateValidator);
     $tripValidator->key('end_date', $dateValidator);
     $tripValidator->key('comment', $stringValidator);
     // run validation
     try {
         $tripValidator->assert($data);
     } catch (NestedValidationException $exception) {
         $this->writeFail($exception->getFullMessage());
         return;
     }
     //then check the current user is either admin or editing his own
     if ($this->isUserAuthenticated() && ($this->currentUser->isAdmin() || $this->currentUser->id === $data["user_id"])) {
         TripModel::updateTrip($data);
     } else {
         $this->writeUnauthorized();
     }
 }