Skip to content
This repository was archived by the owner on Feb 16, 2022. It is now read-only.

Add Float to Entity Types #119

Closed
wants to merge 11 commits into from
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](CONTRIBUTING.md).

## [v4.1.0] - 2020-04-01
- Add Float to Entity Types

## [v4.0.1] - 2020-03-20
- Convert into bigInteger database fields
Expand Down
1 change: 1 addition & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
'attribute_boolean_values' => 'attribute_boolean_values',
'attribute_datetime_values' => 'attribute_datetime_values',
'attribute_integer_values' => 'attribute_integer_values',
'attribute_float_values' => 'attribute_float_values',
'attribute_text_values' => 'attribute_text_values',
'attribute_varchar_values' => 'attribute_varchar_values',

Expand Down
2 changes: 1 addition & 1 deletion database/factories/AttributeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
$factory->define(Rinvex\Attributes\Models\Attribute::class, function (Faker $faker) {
return [
'slug' => $faker->slug,
'type' => $faker->randomElement(['boolean', 'datetime', 'integer', 'text', 'varchar']),
'type' => $faker->randomElement(['boolean', 'datetime', 'integer', 'float', 'text', 'varchar']),
'name' => $faker->name,
'entities' => $faker->randomElement(['App\Models\Company', 'App\Models\Product', 'App\Models\User']),
];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAttributeFloadValuesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up(): void
{
Schema::create(config('rinvex.attributes.tables.attribute_integer_values'), function (Blueprint $table) {
// Columns
$table->bigIncrements('id');
$table->float('content', 8, 2);
$table->bigInteger('attribute_id')->unsigned();
$table->bigInteger('entity_id')->unsigned();
$table->string('entity_type');
$table->timestamps();

// Indexes
$table->foreign('attribute_id')->references('id')->on(config('rinvex.attributes.tables.attributes'))
->onDelete('cascade')->onUpdate('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down(): void
{
Schema::dropIfExists(config('rinvex.attributes.tables.attribute_integer_values'));
}
}
60 changes: 60 additions & 0 deletions src/Models/Type/Float.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Rinvex\Attributes\Models\Type;

use Rinvex\Attributes\Models\Value;

/**
* Rinvex\Attributes\Models\Type\Float.
*
* @property int $id
* @property int $content
* @property int $attribute_id
* @property int $entity_id
* @property string $entity_type
* @property \Carbon\Carbon|null $created_at
* @property \Carbon\Carbon|null $updated_at
* @property-read \Rinvex\Attributes\Models\Attribute $attribute
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $entity
*
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Type\Float whereAttributeId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Type\Float whereContent($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Type\Float whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Type\Float whereEntityId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Type\Float whereEntityType($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Type\Float whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Attributes\Models\Type\Float whereUpdatedAt($value)
* @mixin \Eloquent
*/
class Float extends Value
{
/**
* {@inheritdoc}
*/
protected $casts = [
'content' => 'float',
'attribute_id' => 'float',
'entity_id' => 'float',
'entity_type' => 'string',
];

/**
* Create a new Eloquent model instance.
*
* @param array $attributes
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);

$this->setTable(config('rinvex.attributes.tables.attribute_float_values'));
$this->setRules([
'content' => 'required|regex:/^\d*(\.\d{2})?$/',
'attribute_id' => 'required|integer|exists:'.config('rinvex.attributes.tables.attributes').',id',
'entity_id' => 'required|integer',
'entity_type' => 'required|string',
]);
}
}
1 change: 1 addition & 0 deletions tests/Feature/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected function setUp(): void
'text' => \Rinvex\Attributes\Models\Type\Text::class,
'bool' => \Rinvex\Attributes\Models\Type\Boolean::class,
'integer' => \Rinvex\Attributes\Models\Type\Integer::class,
'float' => \Rinvex\Attributes\Models\Type\Float::class,
'varchar' => \Rinvex\Attributes\Models\Type\Varchar::class,
'datetime' => \Rinvex\Attributes\Models\Type\Datetime::class,
]);
Expand Down