Skip to content

Commit 546eb4f

Browse files
committed
Updated canInsert method & added isInsertable method on Layout
1 parent e388ee9 commit 546eb4f

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

src/Concerns/HasLayoutInstances.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,10 @@ protected function canInsert(Layout $instance)
9090
return InstanceNotInsertableException::REASON_LIMIT;
9191
}
9292

93-
if(! is_null($limit = $instance->getLimit()) && ($limit <= $this->count($instance->getKey()))) {
94-
return InstanceNotInsertableException::REASON_LAYOUT_LIMIT;
93+
$code = $instance->isInsertable($this);
94+
95+
if(is_int($code) || $code === false) {
96+
return $code ?: 0;
9597
}
9698

9799
return true;

src/Contracts/Layout.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Whitecube\LaravelFlexibleContent\Contracts;
44

5+
use Whitecube\LaravelFlexibleContent\Contracts\Flexible;
6+
57
interface Layout
68
{
79
/**
@@ -67,6 +69,15 @@ public function attributes(array $attributes, $syncOriginal = false) : Layout;
6769
*/
6870
public function getAttributes();
6971

72+
/**
73+
* Check if the current layout can be inserted in the provided flexible container.
74+
* If not insertable, it is recommended to return an error code (int).
75+
*
76+
* @param \Whitecube\LaravelFlexibleContent\Contracts\Flexible $container
77+
* @return bool|int
78+
*/
79+
public function isInsertable(Flexible $container);
80+
7081
/**
7182
* Create a layout instance from this layout.
7283
*

src/Layout.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
1010
use Illuminate\Database\Eloquent\Concerns\HidesAttributes;
1111
use Whitecube\LaravelFlexibleContent\Contracts\Layout as LayoutInterface;
12+
use Whitecube\LaravelFlexibleContent\Contracts\Flexible as FlexibleInterface;
13+
use Whitecube\LaravelFlexibleContent\Exceptions\InstanceNotInsertableException;
1214

1315
class Layout implements LayoutInterface, ArrayAccess, JsonSerializable, Arrayable
1416
{
@@ -125,6 +127,22 @@ public function attributes(array $attributes, $syncOriginal = false): LayoutInte
125127
return $this;
126128
}
127129

130+
/**
131+
* Check if the current layout can be inserted in the provided flexible container.
132+
* If not insertable, it is recommended to return an error code (int).
133+
*
134+
* @param \Whitecube\LaravelFlexibleContent\Contracts\Flexible $container
135+
* @return bool|int
136+
*/
137+
public function isInsertable(FlexibleInterface $container)
138+
{
139+
if(! is_null($limit = $this->getLimit()) && ($limit <= $container->count($this->getKey()))) {
140+
return InstanceNotInsertableException::REASON_LAYOUT_LIMIT;
141+
}
142+
143+
return true;
144+
}
145+
128146
/**
129147
* Create a layout instance from this layout.
130148
*

0 commit comments

Comments
 (0)