Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Apr 20, 2021
0 parents commit fa3a023
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# composer package patch
vendor/
composer.lock

# OSX
.DS_Store

# Windows
Thumbs.db
29 changes: 29 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
129 changes: 129 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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());
```
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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"
}
}
}
5 changes: 5 additions & 0 deletions src/Uuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace dynamikaweb\uuid;

class Uuid extends \Ramsey\Uuid\Uuid {}
8 changes: 8 additions & 0 deletions src/UuidMask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace dynamikaweb\uuid;

class UuidMask extends \yii\widgets\MaskedInput
{
public $mask = \Ramsey\Uuid\Validator\GenericValidator::VALID_PATTERN;
}
5 changes: 5 additions & 0 deletions src/UuidValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace dynamikaweb\uuid;

class UuidValidator extends \macgyer\yii2uuidvalidator\UuidValidator {}

0 comments on commit fa3a023

Please sign in to comment.