本文整理汇总了PHP中Symfony\Component\Validator\Constraint::getDefaultOption方法的典型用法代码示例。如果您正苦于以下问题:PHP Constraint::getDefaultOption方法的具体用法?PHP Constraint::getDefaultOption怎么用?PHP Constraint::getDefaultOption使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Validator\Constraint
的用法示例。
在下文中一共展示了Constraint::getDefaultOption方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: validate
public function validate($value, Constraint $constraint)
{
$usernameErrors = array();
$roleNameErrors = array();
$workspace = $constraint->getDefaultOption();
$wsRoleNames = array();
$workspaceRoles = $this->roleManager->getRolesByWorkspace($workspace);
foreach ($workspaceRoles as $workspaceRole) {
$wsRoleNames[] = $workspaceRole->getTranslationKey();
}
$data = $this->ut->formatCsvOutput(file_get_contents($value));
$lines = str_getcsv($data, PHP_EOL);
foreach ($lines as $line) {
$linesTab = explode(';', $line);
$nbElements = count($linesTab);
if (trim($line) !== '' && $nbElements !== 2) {
$this->context->addViolation($constraint->message);
return;
}
}
foreach ($lines as $i => $line) {
if (trim($line) !== '') {
$datas = explode(';', $line);
$username = $datas[0];
$roleName = $datas[1];
$user = $this->userManager->getOneUserByUsername($username);
if (is_null($user)) {
$msg = $this->translator->trans('workspace_user_invalid', array('%username%' => $username, '%line%' => $i + 1), 'platform') . ' ';
$usernameErrors[] = $msg;
}
if (!in_array($roleName, $wsRoleNames)) {
$msg = $this->translator->trans('line_number', array('%line%' => $i + 1), 'platform') . ' ';
$msg .= $this->translator->trans('unavailable_role', array('%translationKey%' => $roleName), 'platform');
$roleNameErrors[] = $msg;
}
}
}
foreach ($usernameErrors as $error) {
$this->context->addViolation($error);
}
foreach ($roleNameErrors as $error) {
$this->context->addViolation($error);
}
}
示例2: validate
public function validate($value, Constraint $constraint)
{
$mode = $constraint->getDefaultOption();
$lines = str_getcsv(file_get_contents($value), PHP_EOL);
$authDrivers = $this->authenticationManager->getDrivers();
foreach ($lines as $line) {
$linesTab = explode(';', $line);
$nbElements = count($linesTab);
if (trim($line) != '') {
if ($nbElements < 5) {
$this->context->addViolation($constraint->message);
return;
}
}
}
$usernames = array();
$mails = array();
if ($mode === 1) {
$currentDate = new \DateTime();
$timestamp = $currentDate->getTimestamp();
$fakeUsername = '@@@fake_username_' . $timestamp . '@@@';
$fakeMail = 'fake_email_' . $timestamp . '@fake-' . $timestamp . '-claroline-connect.com';
}
foreach ($lines as $i => $line) {
if (trim($line) != '') {
$user = explode(';', $line);
$firstName = $user[0];
$lastName = $user[1];
$username = $user[2];
$pwd = $user[3];
$email = $user[4];
if (isset($user[5])) {
$code = trim($user[5]) === '' ? null : $user[5];
} else {
$code = null;
}
if (isset($user[6])) {
$phone = trim($user[6]) === '' ? null : $user[6];
} else {
$phone = null;
}
if (isset($user[7])) {
$authentication = trim($user[7]) === '' ? null : $user[7];
} else {
$authentication = null;
}
if (isset($user[8])) {
$modelName = trim($user[7]) === '' ? null : $user[7];
} else {
$modelName = null;
}
!array_key_exists($email, $mails) ? $mails[$email] = array($i + 1) : ($mails[$email][] = $i + 1);
!array_key_exists($username, $usernames) ? $usernames[$username] = array($i + 1) : ($usernames[$username][] = $i + 1);
$existingUser = null;
if ($mode === 1) {
try {
$existingUser = $this->userManager->getUserByUsernameOrMail($username, $email);
} catch (NonUniqueResultException $e) {
$msg = $this->translator->trans('line_number', array('%line%' => $i + 1), 'platform');
$msg .= ' ' . $this->translator->trans('username_and_email_from_two_different_users', array('%username%' => $username, '%email%' => $email), 'platform');
$this->context->addViolation($msg);
continue;
}
}
if (!is_null($existingUser)) {
// For an update, we will validate user with a fake username and email
$upperExistingUsername = strtoupper(trim($existingUser->getUsername()));
$upperExistingMail = strtoupper(trim($existingUser->getMail()));
$upperUsername = strtoupper(trim($username));
$upperMail = strtoupper(trim($email));
if ($upperExistingUsername === $upperUsername && $upperExistingMail === $upperMail) {
$existingUser->setUsername($fakeUsername);
$existingUser->setMail($fakeMail);
} elseif ($upperExistingUsername === $upperUsername) {
$existingUser->setUsername($fakeUsername);
$existingUser->setMail($email);
} else {
$existingUser->setUsername($username);
$existingUser->setMail($fakeMail);
}
$existingUser->setFirstName($firstName);
$existingUser->setLastName($lastName);
$existingUser->setPlainPassword($pwd);
$existingUser->setAdministrativeCode($code);
$existingUser->setPhone($phone);
$errors = $this->validator->validate($existingUser, array('registration', 'Default'));
$existingUser->setUsername($username);
$existingUser->setMail($email);
} else {
$newUser = new User();
$newUser->setFirstName($firstName);
$newUser->setLastName($lastName);
$newUser->setUsername($username);
$newUser->setPlainPassword($pwd);
$newUser->setMail($email);
$newUser->setAdministrativeCode($code);
$newUser->setPhone($phone);
$errors = $this->validator->validate($newUser, array('registration', 'Default'));
}
if ($authentication) {
//.........这里部分代码省略.........