/** * {@inheritDoc} * * @return ValidatorInterface */ public function createService(ServiceLocatorInterface $serviceLocator) { $services = $serviceLocator->getServiceLocator(); /* @var $options InputFilterOptionsInterface */ $options = $services->get(ModuleOptions::class); /* @var $userMapper \CmsUser\Persistence\UserMapperInterface */ $userMapper = $services->get('MapperManager')->get($options->getUserEntityClass()); $identity = null; if ($services->has($options->getAuthenticationService())) { /* @var $authService \Zend\Authentication\AuthenticationServiceInterface */ $authService = $services->get($options->getAuthenticationService()); if ($authService->hasIdentity()) { /* @var $identity \CmsUser\Mapping\UserInterface */ $identity = $authService->getIdentity(); } } return $serviceLocator->get('Callback', ['messages' => [Callback::INVALID_VALUE => 'This username is already taken'], 'callback' => function ($value) use($userMapper, $identity) { if ($identity) { $filter = new StringToLower(['encoding' => 'UTF-8']); if ($filter->filter($identity->getUsername()) === $filter->filter($value)) { return true; } } return !$userMapper->findOneByUsername($value); }, 'break_chain_on_failure' => true]); }
/** * Defined by Zend\Filter\Filter * * Does a lowercase on the content of the given file * * @param string|array $value Full path of file to change or $_FILES data array * @return string|array The given $value * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ public function filter($value) { // An uploaded file? Retrieve the 'tmp_name' $isFileUpload = is_array($value) && isset($value['tmp_name']); if ($isFileUpload) { $uploadData = $value; $value = $value['tmp_name']; } if (!file_exists($value)) { throw new Exception\InvalidArgumentException("File '{$value}' not found"); } if (!is_writable($value)) { throw new Exception\RuntimeException("File '{$value}' is not writable"); } $content = file_get_contents($value); if (!$content) { throw new Exception\RuntimeException("Problem while reading file '{$value}'"); } $content = parent::filter($content); $result = file_put_contents($value, $content); if (!$result) { throw new Exception\RuntimeException("Problem while writing file '{$value}'"); } if ($isFileUpload) { return $uploadData; } return $value; }
public function filter($value) { if (is_array($value) && isset($value['name'])) { $value['name'] = parent::filter($value['name']); } else { if (is_string($value)) { $value = parent::filter($value); } else { throw new \InvalidArgumentException('Error argument type "' . gettype($value) . '"'); } } return $value; }
/** * Defined by Zend\Filter\Filter * * Does a lowercase on the content of the given file * * @param string $value Full path of file to change * @return string The given $value * @throws Exception\InvalidArgumentException * @throws Exception\RuntimeException */ public function filter($value) { if (!file_exists($value)) { throw new Exception\InvalidArgumentException("File '{$value}' not found"); } if (!is_writable($value)) { throw new Exception\RuntimeException("File '{$value}' is not writable"); } $content = file_get_contents($value); if (!$content) { throw new Exception\RuntimeException("Problem while reading file '{$value}'"); } $content = parent::filter($content); $result = file_put_contents($value, $content); if (!$result) { throw new Exception\RuntimeException("Problem while writing file '{$value}'"); } return $value; }
/** * Defined by Zend\Filter\Filter * * Does a lowercase on the content of the given file * * @param string $value Full path of file to change * @return string The given $value * @throws \Zend\Filter\Exception */ public function __invoke($value) { if (!file_exists($value)) { throw new Filter\Exception("File '{$value}' not found"); } if (!is_writable($value)) { throw new Filter\Exception("File '{$value}' is not writable"); } $content = file_get_contents($value); if (!$content) { throw new Filter\Exception("Problem while reading file '{$value}'"); } $content = parent::__invoke($content); $result = file_put_contents($value, $content); if (!$result) { throw new Filter\Exception("Problem while writing file '{$value}'"); } return $value; }
/** * to lower chars * * @param string $s * @return string */ private function _toLowerChars($s) { $lowerFilter = new \Zend\Filter\StringToLower(); $lowerFilter->setEncoding('utf-8'); return $lowerFilter->filter($s); }
/** * @dataProvider returnUnfilteredDataProvider * @return void */ public function testReturnUnfiltered($input) { $this->assertEquals($input, $this->_filter->filter($input)); }