Skip to content

Commit

Permalink
Merge pull request #9 from verbanent/feature/custom-column-name
Browse files Browse the repository at this point in the history
Allowed custom column name for primary key
  • Loading branch information
radek-ziemniewicz authored Jan 4, 2022
2 parents bf8e62a + 255dc41 commit 093db43
Show file tree
Hide file tree
Showing 40 changed files with 495 additions and 133 deletions.
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
FROM php:fpm-buster
RUN pecl install xdebug && docker-php-ext-enable xdebug
FROM php:8-cli-alpine
RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" &&\
php composer-setup.php &&\
php -r "unlink('composer-setup.php');" &&\
mv composer.phar /usr/local/bin/composer
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"description": "Ordered binary UUID in Laravel / Eloquent based on UUID version 1",
"require": {
"php": "^7.3|^8.0",
"ramsey/uuid": "^3.8|^4.0"
"ramsey/uuid": "^3.8|^4"
},
"require-dev": {
"phpunit/phpunit": "^9",
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: "3.3"
services:
php:
ebu_php:
build: .
container_name: php
container_name: ebu_php
restart: unless-stopped
tty: true
volumes:
- .:/var/www/html
- ./docker/xdebug-local.ini:/usr/local/etc/php/conf.d/xdebug-local.ini
14 changes: 0 additions & 14 deletions src/AbstractModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,4 @@ class AbstractModel extends Model
* @var string
*/
protected $keyType = 'uuid';

/**
* Column name for primary key.
*
* @var string
*/
protected $primaryKey = 'uuid';

/**
* Allow to fill UUID column.
*
* @var array
*/
protected $fillable = ['uuid'];
}
1 change: 1 addition & 0 deletions src/Exceptions/AccessedUnsetUuidPropertyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

class AccessedUnsetUuidPropertyException extends \RuntimeException
{
//
}
8 changes: 2 additions & 6 deletions src/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@
use Illuminate\Support\Fluent;

/**
* Class change UUID type from default char(36) to binary(16).
* Class changes the UUID type from default char(36) to binary(16).
*/
class MySqlGrammar extends IlluminateMySqlGrammar
{
/**
* Create the column definition for a uuid type.
*
* @param \Illuminate\Support\Fluent
*
* @return string
* Creates the column definition for the UUID type.
*/
protected function typeUuid(Fluent $column): string
{
Expand Down
24 changes: 15 additions & 9 deletions src/Traits/BinaryUuidSupportableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ public static function bootBinaryUuidSupportableTrait(): void
{
static::creating(
function (Model $model) {
$uuid = $model->getKeyName();

/** @var Model|BinaryUuidSupportableTrait $model */
if (!isset($model->attributes['uuid'])) {
$model->uuid = $model->generateUuid();
} elseif (Uuid::isValid($model->attributes['uuid'])) {
$model->uuid = Uuid::fromString($model->attributes['uuid'])->getBytes();
} elseif (is_string($model->attributes['uuid']) && strlen($model->attributes['uuid']) === 16) {
$model->uuid = $model->attributes['uuid'];
if (!isset($model->attributes[$uuid])) {
$model->$uuid = $model->generateUuid();
} elseif (Uuid::isValid($model->attributes[$uuid])) {
$model->$uuid = Uuid::fromString($model->attributes[$uuid])->getBytes();
} elseif (is_string($model->attributes[$uuid]) && strlen($model->attributes[$uuid]) === 16) {
$model->$uuid = $model->attributes[$uuid];
}

if (isset($model->readable) && $model->readable) {
Expand Down Expand Up @@ -101,13 +103,15 @@ public function generateUuid(): string
*/
public function uuid(): string
{
if (!isset($this->uuid)) {
$uuid = $this->getKeyName();

if (!isset($this->$uuid)) {
throw new AccessedUnsetUuidPropertyException(
'Cannot get UUID property for not saved model'
);
}

return Uuid::fromBytes($this->uuid)->toString();
return Uuid::fromBytes($this->$uuid)->toString();
}

/**
Expand All @@ -119,6 +123,8 @@ public function uuid(): string
*/
public static function find(string $uuid): Model
{
return static::where('uuid', '=', Uuid::fromString($uuid)->getBytes())->firstOrFail();
$uuidKey = app(static::class)->getKeyName();

return static::where($uuidKey, '=', Uuid::fromString($uuid)->getBytes())->firstOrFail();
}
}
2 changes: 1 addition & 1 deletion src/Traits/ForeignBinaryUuidSupportableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static function isUuidValid(Model $model, string $uuidable): bool
* @param string $columnName
* @param string $uuid
*
* @return \Illuminate\Database\Eloquent\Collection
* @return Collection
*/
public static function findByUuid(string $columnName, string $uuid): Collection
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
/**
* Example class for traits tests.
*/
class AbstractExampleModel extends AbstractModel
class AbstractExampleIdModel extends AbstractModel
{
/**
* Table name.
*
* @var string
*/
protected $table = 'model_test';
protected $table = 'model_test_id';

/**
* Create rows in table without timestamps.
Expand Down
41 changes: 41 additions & 0 deletions tests/Example/AbstractExampleUuidModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Verbanent\Uuid\Test\Example;

use Verbanent\Uuid\AbstractModel;

/**
* Example class for traits tests.
*/
class AbstractExampleUuidModel extends AbstractModel
{
/**
* Table name.
*
* @var string
*/
protected $table = 'model_test_uuid';

/**
* Create rows in table without timestamps.
*
* @var bool
*/
public $timestamps = false;

/**
* Column name for primary key.
*
* @var string
*/
protected $primaryKey = 'uuid';

/**
* Allow to fill UUID column.
*
* @var array
*/
protected $fillable = ['uuid'];
}
14 changes: 0 additions & 14 deletions tests/Example/Binary/CatModel.php

This file was deleted.

15 changes: 15 additions & 0 deletions tests/Example/Binary/CatUuidModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Verbanent\Uuid\Test\Example\Binary;

use Verbanent\Uuid\Test\Example\AbstractExampleUuidModel;

/**
* Cat model.
*/
class CatUuidModel extends AbstractExampleUuidModel
{
//
}
14 changes: 0 additions & 14 deletions tests/Example/Binary/CowModel.php

This file was deleted.

15 changes: 15 additions & 0 deletions tests/Example/Binary/CowUuidModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Verbanent\Uuid\Test\Example\Binary;

use Verbanent\Uuid\Test\Example\AbstractExampleUuidModel;

/**
* Cow model.
*/
class CowUuidModel extends AbstractExampleUuidModel
{
//
}
14 changes: 0 additions & 14 deletions tests/Example/Binary/DogModel.php

This file was deleted.

15 changes: 15 additions & 0 deletions tests/Example/Binary/DogUuidModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Verbanent\Uuid\Test\Example\Binary;

use Verbanent\Uuid\Test\Example\AbstractExampleUuidModel;

/**
* Dog model.
*/
class DogUuidModel extends AbstractExampleUuidModel
{
//
}
14 changes: 0 additions & 14 deletions tests/Example/Binary/HorseModel.php

This file was deleted.

15 changes: 15 additions & 0 deletions tests/Example/Binary/HorseUuidModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Verbanent\Uuid\Test\Example\Binary;

use Verbanent\Uuid\Test\Example\AbstractExampleUuidModel;

/**
* Horse model.
*/
class HorseUuidModel extends AbstractExampleUuidModel
{
//
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Verbanent\Uuid\Test\Example\Binary;

use Verbanent\Uuid\Test\Example\AbstractExampleModel;
use Verbanent\Uuid\Test\Example\AbstractExampleUuidModel;

/**
* Pig model.
*/
class PigModel extends AbstractExampleModel
class PigUuidModel extends AbstractExampleUuidModel
{
protected $readable = true;
}
15 changes: 15 additions & 0 deletions tests/Example/BinaryId/CatIdModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Verbanent\Uuid\Test\Example\BinaryId;

use Verbanent\Uuid\Test\Example\AbstractExampleIdModel;

/**
* Cat model.
*/
class CatIdModel extends AbstractExampleIdModel
{
//
}
15 changes: 15 additions & 0 deletions tests/Example/BinaryId/CowIdModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Verbanent\Uuid\Test\Example\BinaryId;

use Verbanent\Uuid\Test\Example\AbstractExampleIdModel;

/**
* Cow model.
*/
class CowIdModel extends AbstractExampleIdModel
{
protected $fillable = ['id'];
}
15 changes: 15 additions & 0 deletions tests/Example/BinaryId/DogIdModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Verbanent\Uuid\Test\Example\BinaryId;

use Verbanent\Uuid\Test\Example\AbstractExampleIdModel;

/**
* Dog model.
*/
class DogIdModel extends AbstractExampleIdModel
{
//
}
15 changes: 15 additions & 0 deletions tests/Example/BinaryId/HorseIdModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Verbanent\Uuid\Test\Example\BinaryId;

use Verbanent\Uuid\Test\Example\AbstractExampleIdModel;

/**
* Horse model.
*/
class HorseIdModel extends AbstractExampleIdModel
{
//
}
Loading

0 comments on commit 093db43

Please sign in to comment.