From fa3a02396398f64b1ced3bc38dea015e74dd22ef Mon Sep 17 00:00:00 2001 From: RodrigoDornelles Date: Tue, 20 Apr 2021 15:18:47 -0300 Subject: [PATCH] initial commit --- .gitignore | 9 +++ LICENSE.txt | 29 ++++++++++ README.md | 129 ++++++++++++++++++++++++++++++++++++++++++ composer.json | 31 ++++++++++ src/Uuid.php | 5 ++ src/UuidMask.php | 8 +++ src/UuidValidator.php | 5 ++ 7 files changed, 216 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Uuid.php create mode 100644 src/UuidMask.php create mode 100644 src/UuidValidator.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..77ee4aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +# composer package patch +vendor/ +composer.lock + +# OSX +.DS_Store + +# Windows +Thumbs.db \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..b4c8a88 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2019, dynamikaweb +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..071beba --- /dev/null +++ b/README.md @@ -0,0 +1,129 @@ +dynamikaweb/yii2-uuid +===================== +[![Latest Stable Version](https://img.shields.io/github/v/release/dynamikaweb/yii2-uuid)](https://github.com/dynamikaweb/yii2-uuid/releases ) +[![Total Downloads](https://poser.pugx.org/dynamikaweb/yii2-uuid/downloads)](https://packagist.org/packages/dynamikaweb/yii2-uuid) +[![License](https://poser.pugx.org/dynamikaweb/yii2-uuid/license)](https://github.com/dynamikaweb/yii2-uuid/blob/master/LICENSE.txt) +[![Codacy Badge](https://app.codacy.com/project/badge/Grade/7af0de78a0514ab5bedb020f3749198d)](https://www.codacy.com/gh/dynamikaweb/yii2-uuid/dashboard?utm_source=github.com&utm_medium=referral&utm_content=dynamikaweb/yii2-uuid&utm_campaign=Badge_Grade) +[![Build Test](https://scrutinizer-ci.com/g/dynamikaweb/yii2-uuid/badges/build.png?b=master)](https://scrutinizer-ci.com/g/dynamikaweb/yii2-uuid/) +[![Latest Unstable Version](https://poser.pugx.org/dynamikaweb/yii2-uuid/v/unstable)](https://github.com/dynamikaweb/yii2-uuid/find/master) + +Installation +------------ +The preferred way to install this extension is through [composer](http://getcomposer.org/download/). + +Either run + +```SHELL +$ composer require dynamikaweb/yii2-uuid "*" +``` + +or add + +```JSON +"dynamikaweb/yii2-uuid": "*" +``` + +to the `require` section of your `composer.json` file. + +## Database usage ## + +### Create migration ### + +```PHP +public function safeUp() +{ + $this->addColumn('sometable', 'uuid', 'uuid' => $this->binary(16)->unique()->notNull()); + ... +} +``` + +## Model usage ## + +### Validate ### + +```PHP +public function rules() +{ + return [ + [['uuid'], UuidValidator::classname(), 'on' => self::SCENARIO_SEARCH] + ... + ]; +} +``` + +### Generate and save ### + +```PHP +public function beforeSave($insert) +{ + if (!parent::beforeSave($insert)) { + return false; + } + + if ($this->isNewRecord) { + $this->setAttribute('uuid', Uuid::uuid4()->getBytes()); + } + ... +} +``` + +### Formatting to string ### + +```PHP +public function getUuidToString() +{ + if (is_resource($this->uuid)) { + $this->uuid = stream_get_contents($this->uuid); + } + + return Uuid::fromBytes($this->uuid)->toString(); +} +``` + +## Controller usage ## + +### View ### + +```PHP +public function actionView($uuid) +{ + return $this->render('view', [ + 'model' => $this->findModel($uuid) + ]); +} +``` + +### Finding ### + +```PHP +protected function findModel($uuid) +{ + try { + $uuid = '\x'.bin2hex(Uuid::fromString($uuid)->getBytes()); + } + catch (InvalidUuidStringException $e) { + throw new HttpException(400, 'UUID invalid!'); + } + if (($model = SomeModel::findOne(['uuid' => $uuid])) === null) { + throw new HttpException(404, 'UUID not found!'); + } + + return $model; +} +``` + +## View usage ## + +### Form ## + +```PHP +echo UuidMask::widget([ + 'name' => 'uuid' +]); +``` + +### Active form ### + +```PHP +echo $form->field($model, 'from_date')->widget(Uuid::className()); +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..e518b03 --- /dev/null +++ b/composer.json @@ -0,0 +1,31 @@ +{ + "name": "dynamikaweb/yii2-uuid", + "description": "Guide and package for using UUID in yii2 projects", + "type": "yii2-extension", + "keywords": ["yii2", "uuid"], + "license": "BSD-3-Clause", + "authors": [ + { + "name": "RodrigoDornelles", + "email": "rodrigo@dornelles.me", + "role": "Developer" + } + ], + "support": { + "issues": "http://github.com/dynamikaweb/yii2-uuid/issues", + "source": "http://github.com/dynamikaweb/yii2-uuid" + }, + "require": { + "php": ">=7.4.0", + "macgyer/yii2-uuid-validator": "*", + "yiisoft/yii2": "*", + "ramsey/uuid": "*" + }, + "minimum-stability": "dev", + "prefer-stable": false, + "autoload": { + "psr-4": { + "dynamikaweb\\uuid\\": "src" + } + } +} \ No newline at end of file diff --git a/src/Uuid.php b/src/Uuid.php new file mode 100644 index 0000000..838bdfc --- /dev/null +++ b/src/Uuid.php @@ -0,0 +1,5 @@ +