public function _validate() { ValidationManager::validate(function () use($vm) { $vm->register('barcode_type', new FunctionValidator(function () { return in_array($this->type, self::$BARCODE_TYPES); }), 'Barcode type must be one of those: ', implode(', ', self::$BARCODE_TYPES)); $vm->register('barcode', new FunctionValidator(function () { return empty(Barcode::findDuplicate($this->type, $this->value, $this->product)); }), sprintf('There is a duplicated barcode of type %s with the value %s - Product: %s', $this->type, $this->value, $this->product->name)); $required_length = 0; switch ($this->type) { case Barcode::ISBN_10: $required_length = 10; break; case Barcode::ISBN_13: $required_length = 13; break; case Barcode::ISSN: $required_length = 8; break; } $vm->register('barcode', new FunctionValidator(function () use($required_length) { return $required_length > 0 && strlen($this->value) == $required_length; }), 'ISSN must have length of 8'); }); }
protected function _validate() { ValidationManager::validate(function ($vm) { $vm->register('amount', new FunctionValidator(function () { return $this->amount > 0; })); }); }
protected function _validate() { ValidationManager::validate(function ($vm) { $vm->register('name', new RequireValidator($this->name), 'Category name is required'); }); }
/** * @param $params * @param $product * @throw Markzero\Validation\Exception\ValidationException */ private static function validateParams(ParameterBag $params, Product $product) { ValidationManager::validate(function ($vm) use($params, $product) { $catid = $params->get('product[category_id]', null, true); $name = $params->get('product[name]', '', true); $isbn10 = $params->get('product[isbn_10]', null, true); $isbn13 = $params->get('product[isbn_13]', null, true); $issn = $params->get('product[issn]', null, true); $vm->register('product[name]', new RequireValidator($name), 'Product Name is required'); $vm->register('product[category_id]', new RequireValidator($catid), 'Category is required'); if ($isbn10) { $vm->register('product[isbn_10]', new FunctionValidator(function () use($isbn10) { return strlen($isbn10) == 10; }), 'ISBN10 must have length of 10'); $vm->register('product[isbn_10]', new FunctionValidator(function () use($isbn10, $product) { return empty(Barcode::findDuplicate(Barcode::ISBN_10, $isbn10, $product)); }), 'Duplicated ISBN10'); } if ($isbn13) { $vm->register('product[isbn_13]', new FunctionValidator(function () use($isbn13) { return strlen($isbn13) == 13; }), 'ISBN13 must have length of 13'); $vm->register('product[isbn_13]', new FunctionValidator(function () use($isbn13, $product) { return empty(Barcode::findDuplicate(Barcode::ISBN_13, $isbn13, $product)); }), 'Duplicated ISBN13'); } if ($issn) { $vm->register('product[issn]', new FunctionValidator(function () use($issn) { return strlen($issn) == 8; }), 'ISSN must have length of 8'); $vm->register('product[issn]', new FunctionValidator(function () use($issn, $product) { return empty(Barcode::findDuplicate(Barcode::ISSN, $issn, $product)); }), 'Duplicated ISSN'); } }); }
/** * Try to sign user in * * @param string $email * @param string $password * * @throw Markzero\Http\Exception\ResourceNotFoundException * Markzero\Auth\Exception\AuthenticationFailedException * Markzero\Validation\Exception\ValidationException */ public static function signIn($email, $password) { ValidationManager::validate(function ($vm) use($email, $password) { $vm->register('user.email', new Validator\EmailValidator($email)); $vm->register('user.password', new Validator\RequireValidator($password), 'Password is required'); }); $user = User::findOneBy(['email' => $email]); if ($user === null) { throw new ResourceNotFoundException(); } if (password_verify($password, $user->password_hash)) { self::signInWithUser($user); } else { throw new AuthenticationFailedException(); } }