Created
February 2, 2019 22:51
-
-
Save orobogenius/cf6f5a0eb571670492be95e027112dee to your computer and use it in GitHub Desktop.
Extending Laravel's Validator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Verifiers; | |
use Illuminate\Contracts\Redis\Factory; | |
use Illuminate\Validation\PresenceVerifierInterface; | |
class RedisPresenceVerifier implements PresenceVerifierInterface | |
{ | |
/** | |
* The Redis factory implementation. | |
* | |
* @var \Illuminate\Contracts\Redis\Factory | |
*/ | |
protected $redis; | |
/** | |
* Create a new redis presence verifier. | |
* | |
* @param \Illuminate\Contracts\Redis\Factory $redis | |
* @return void | |
*/ | |
public function __construct(Factory $redis) | |
{ | |
$this->redis = $redis; | |
} | |
/** | |
* Count the number of objects in a collection having the given value. | |
* | |
* @param string $collection | |
* @param string $column | |
* @param string $value | |
* @param int|null $excludeId | |
* @param string|null $idColumn | |
* @param array $extra | |
* @return int | |
*/ | |
public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = []) | |
{ | |
if (! $this->collectionExists($collection)) { | |
return -1; | |
} | |
// Assuming we are working with a list | |
$values = $this->redis->lrange($collection, 0, -1); | |
return (int) in_array($value, $values); | |
} | |
/** | |
* Count the number of objects in a collection with the given values. | |
* | |
* @param string $collection | |
* @param string $column | |
* @param array $values | |
* @param array $extra | |
* @return int | |
*/ | |
public function getMultiCount($collection, $column, array $values, array $extra = []) | |
{ | |
if (! $this->collectionExists($collection)) { | |
return 0; | |
} | |
// Assuming we are working with a list | |
return count(array_intersect( | |
$values, | |
$this->redis->lrange($collection, 0, -1) | |
)); | |
} | |
/** | |
* Determine if the given key exists. | |
* | |
* @param string $collection | |
* @return bool | |
*/ | |
protected function collectionExists($collection) | |
{ | |
return $this->redis->exists($collection); | |
} | |
/** | |
* Set the connection to be used. | |
* | |
* @param string $connection | |
* @return void | |
*/ | |
public function setConnection($connection) | |
{ | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment