Skip to content

Commit

Permalink
Merge pull request #2 from AuroraWebSoftware/all-shapes
Browse files Browse the repository at this point in the history
- getAllFlexyShapes method added
  • Loading branch information
emreakay authored Sep 30, 2024
2 parents 527cca1 + 1088146 commit 149b2f5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Contracts/FlexyModelContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use AuroraWebSoftware\FlexyField\Enums\FlexyFieldType;
use AuroraWebSoftware\FlexyField\Models\Shape;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Collection;

interface FlexyModelContract
{
Expand All @@ -22,6 +23,11 @@ public static function setFlexyShape(string $fieldName, FlexyFieldType $fieldTyp

public static function getFlexyShape(string $fieldName): ?Shape;

/**
* @return Collection<int,Shape>|null
*/
public static function getAllFlexyShapes(): ?Collection;

public static function deleteFlexyShape(string $fieldName): bool;

/**
Expand Down
21 changes: 15 additions & 6 deletions src/Traits/Flexy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use DateTime;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Validator;

trait Flexy
Expand Down Expand Up @@ -137,6 +138,13 @@ public static function getFlexyShape(string $fieldName): ?Shape
return Shape::where('model_type', $modelType)->where('field_name', $fieldName)->first();
}

public static function getAllFlexyShapes(): ?Collection
{
$modelType = static::getModelType();

return Shape::where('model_type', $modelType)->orderBy('sort')->get();
}

public static function deleteFlexyShape(string $fieldName): bool
{
$modelType = static::getModelType();
Expand All @@ -154,12 +162,13 @@ public function flexy(): Attribute
$this->fields->_model_type = static::getModelType();
$this->fields->_model_id = $this->id;

$values = Value::where(
[
'model_type' => static::getModelType(),
'model_id' => $this->id,
]
)->get();
$values = Value::leftJoin('ff_shapes', 'ff_values.model_type', '=', 'ff_shapes.model_type')
->where(
[
'ff_values.model_type' => static::getModelType(),
'ff_values.model_id' => $this->id,
]
)->orderBy('ff_shapes.sort')->get();

$values->each(function ($value) {
$this->fields[$value->field_name] =
Expand Down
18 changes: 18 additions & 0 deletions tests/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,21 @@
expect(json_decode(ExampleShapelyFlexyModel::whereName('ExampleFlexyModel 1')->first()->flexy->a))->toBe(['a', 'b']);

});

it('can get all shapes', function () {
$flexyModel1 = ExampleShapelyFlexyModel::create(['name' => 'ExampleFlexyModel 1']);
ExampleShapelyFlexyModel::$hasShape = true;

ExampleShapelyFlexyModel::setFlexyShape('a', FlexyFieldType::INTEGER, 2, 'numeric|max:7');
ExampleShapelyFlexyModel::setFlexyShape('b', FlexyFieldType::INTEGER, 1, 'numeric|max:7');

// $flexyModel1->flexy->a = 5;
// $flexyModel1->save();
//
// dd(ExampleShapelyFlexyModel::getAllFlexyShapes());
expect(ExampleShapelyFlexyModel::getAllFlexyShapes())->toHaveCount(2);

ExampleShapelyFlexyModel::setFlexyShape('c', FlexyFieldType::STRING, 5, 'string|max:7');

expect(ExampleShapelyFlexyModel::getAllFlexyShapes())->toHaveCount(3);
});

0 comments on commit 149b2f5

Please sign in to comment.