From 0e3fb9a6e834075a7654497fe2c8c22c6a70cdd4 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 11 Jun 2018 08:45:05 +0200 Subject: [PATCH] app/library/Mvc/Model/Behavior/RandomId.php; minor stuff. --- app/library/Mvc/Model/Behavior/RandomId.php | 33 ++++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/app/library/Mvc/Model/Behavior/RandomId.php b/app/library/Mvc/Model/Behavior/RandomId.php index d84ce2c..e5c644a 100644 --- a/app/library/Mvc/Model/Behavior/RandomId.php +++ b/app/library/Mvc/Model/Behavior/RandomId.php @@ -2,9 +2,9 @@ namespace Httpcb\Mvc\Model\Behavior; -use \Phalcon\Mvc\Model\Behavior; -use \Phalcon\Mvc\Model\BehaviorInterface; -use \Phalcon\Exception; +use Phalcon\Mvc\Model\Behavior, + Phalcon\Mvc\Model\BehaviorInterface, + Phalcon\Mvc\Model\Exception; /** * Generates a unique base64 url-safe id for the field specified. @@ -14,11 +14,16 @@ use \Phalcon\Exception; */ class RandomId extends Behavior implements BehaviorInterface { + /** + * Constructor. + * + * @param array|null $options + * @throws Exception + */ public function __construct($options = null) { - $field = null; - if (isset($options['field'])) { - $field = $options['field']; + if (!isset($options['field']) || strlen($options['field']) < 1) { + throw new Exception("'field' must be set in the option array."); } if (isset($options['length'])) { @@ -29,12 +34,14 @@ class RandomId extends Behavior implements BehaviorInterface $options['length'] = 32; } - if (strlen($field) < 1) { - throw new Exception("'field' must be set in the option array."); - } parent::__construct($options); } + /** + * @param string $type + * @param \Phalcon\Mvc\ModelInterface $model + * @throws \Phalcon\Security\Exception + */ public function notify($type, \Phalcon\Mvc\ModelInterface $model) { 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) { $field = $this->_options['field']; + $len = $this->_options['length']; if ($model->$field === null) { $random = new \Phalcon\Security\Random(); for($i = 0; $i < 3; $i++) { - $id = $random->base64Safe(); - $id = substr($id, 0, $this->_options['length']); + $id = substr($random->base64Safe(), 0, $len); $count = $model->count(array( "$field = ?0",