app/library/Mvc/Model/Behavior/RandomId.php; minor stuff.
This commit is contained in:
parent
df261d968e
commit
0e3fb9a6e8
1 changed files with 22 additions and 11 deletions
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
namespace Httpcb\Mvc\Model\Behavior;
|
namespace Httpcb\Mvc\Model\Behavior;
|
||||||
|
|
||||||
use \Phalcon\Mvc\Model\Behavior;
|
use Phalcon\Mvc\Model\Behavior,
|
||||||
use \Phalcon\Mvc\Model\BehaviorInterface;
|
Phalcon\Mvc\Model\BehaviorInterface,
|
||||||
use \Phalcon\Exception;
|
Phalcon\Mvc\Model\Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generates a unique base64 url-safe id for the field specified.
|
* Generates a unique base64 url-safe id for the field specified.
|
||||||
|
|
@ -14,11 +14,16 @@ use \Phalcon\Exception;
|
||||||
*/
|
*/
|
||||||
class RandomId extends Behavior implements BehaviorInterface
|
class RandomId extends Behavior implements BehaviorInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
*
|
||||||
|
* @param array|null $options
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
public function __construct($options = null)
|
public function __construct($options = null)
|
||||||
{
|
{
|
||||||
$field = null;
|
if (!isset($options['field']) || strlen($options['field']) < 1) {
|
||||||
if (isset($options['field'])) {
|
throw new Exception("'field' must be set in the option array.");
|
||||||
$field = $options['field'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($options['length'])) {
|
if (isset($options['length'])) {
|
||||||
|
|
@ -29,12 +34,14 @@ class RandomId extends Behavior implements BehaviorInterface
|
||||||
$options['length'] = 32;
|
$options['length'] = 32;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen($field) < 1) {
|
|
||||||
throw new Exception("'field' must be set in the option array.");
|
|
||||||
}
|
|
||||||
parent::__construct($options);
|
parent::__construct($options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $type
|
||||||
|
* @param \Phalcon\Mvc\ModelInterface $model
|
||||||
|
* @throws \Phalcon\Security\Exception
|
||||||
|
*/
|
||||||
public function notify($type, \Phalcon\Mvc\ModelInterface $model)
|
public function notify($type, \Phalcon\Mvc\ModelInterface $model)
|
||||||
{
|
{
|
||||||
switch($type) {
|
switch($type) {
|
||||||
|
|
@ -44,16 +51,20 @@ class RandomId extends Behavior implements BehaviorInterface
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \Phalcon\Mvc\ModelInterface $model
|
||||||
|
* @throws \Phalcon\Security\Exception
|
||||||
|
*/
|
||||||
public function generateId(\Phalcon\Mvc\ModelInterface $model)
|
public function generateId(\Phalcon\Mvc\ModelInterface $model)
|
||||||
{
|
{
|
||||||
$field = $this->_options['field'];
|
$field = $this->_options['field'];
|
||||||
|
$len = $this->_options['length'];
|
||||||
|
|
||||||
if ($model->$field === null) {
|
if ($model->$field === null) {
|
||||||
|
|
||||||
$random = new \Phalcon\Security\Random();
|
$random = new \Phalcon\Security\Random();
|
||||||
for($i = 0; $i < 3; $i++) {
|
for($i = 0; $i < 3; $i++) {
|
||||||
$id = $random->base64Safe();
|
$id = substr($random->base64Safe(), 0, $len);
|
||||||
$id = substr($id, 0, $this->_options['length']);
|
|
||||||
|
|
||||||
$count = $model->count(array(
|
$count = $model->count(array(
|
||||||
"$field = ?0",
|
"$field = ?0",
|
||||||
|
|
|
||||||
Reference in a new issue