Skip to content

Commit fbcb333

Browse files
committed
Pint
1 parent 212703a commit fbcb333

21 files changed

+256
-389
lines changed

src/Concerns/HasLayoutInstances.php

+17-32
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Whitecube\LaravelFlexibleContent\Concerns;
44

5-
use Whitecube\LaravelFlexibleContent\LayoutsCollection;
6-
use Whitecube\LaravelFlexibleContent\Contracts\Layout;
75
use Whitecube\LaravelFlexibleContent\Contracts\Flexible;
8-
use Whitecube\LaravelFlexibleContent\Exceptions\LayoutNotFoundException;
6+
use Whitecube\LaravelFlexibleContent\Contracts\Layout;
97
use Whitecube\LaravelFlexibleContent\Exceptions\InstanceNotInsertableException;
8+
use Whitecube\LaravelFlexibleContent\Exceptions\LayoutNotFoundException;
9+
use Whitecube\LaravelFlexibleContent\LayoutsCollection;
1010

1111
trait HasLayoutInstances
1212
{
@@ -28,45 +28,38 @@ trait HasLayoutInstances
2828
* Prevent the Flexible container to instanciate more layouts
2929
* than the indicated amount.
3030
*
31-
* @param null|int $instances
3231
* @return $this
3332
*/
34-
public function limit(?int $instances = 1) : Flexible
33+
public function limit(?int $instances = 1): Flexible
3534
{
3635
$this->limit = (($instances ?? -1) < 0) ? null : $instances;
3736

3837
return $this;
3938
}
4039

4140
/**
42-
* Retrieve the amount of layouts that can be instanciated in
41+
* Retrieve the amount of layouts that can be instanciated in
4342
* this Flexible container.
44-
*
45-
* @return null|int
4643
*/
47-
public function getLimit() : ?int
44+
public function getLimit(): ?int
4845
{
4946
return $this->limit;
5047
}
5148

5249
/**
5350
* Add a layout instance to the Flexible container.
5451
*
55-
* @param string $key
56-
* @param array $attributes
57-
* @param null|int $index
58-
* @param null|string $id
5952
* @return $this
6053
*/
61-
public function insert(string $key, array $attributes = [], ?int $index = null, ?string $id = null) : Flexible
54+
public function insert(string $key, array $attributes = [], ?int $index = null, ?string $id = null): Flexible
6255
{
63-
if(! ($layout = $this->getLayout($key))) {
56+
if (! ($layout = $this->getLayout($key))) {
6457
throw LayoutNotFoundException::make($key);
6558
}
6659

6760
$instance = $layout->make($id, $attributes);
6861

69-
if(($reason = $this->canInsert($instance)) !== true) {
62+
if (($reason = $this->canInsert($instance)) !== true) {
7063
throw InstanceNotInsertableException::make($instance, $reason);
7164
}
7265

@@ -81,18 +74,17 @@ public function insert(string $key, array $attributes = [], ?int $index = null,
8174
* Check if the given instance can be inserted or return the
8275
* reason for refusal.
8376
*
84-
* @param \Whitecube\LaravelFlexibleContent\Contracts\Layout $instance
8577
* @return bool|int
8678
*/
8779
protected function canInsert(Layout $instance)
8880
{
89-
if(! is_null($limit = $this->getLimit()) && ($limit <= $this->count())) {
81+
if (! is_null($limit = $this->getLimit()) && ($limit <= $this->count())) {
9082
return InstanceNotInsertableException::REASON_LIMIT;
9183
}
9284

9385
$code = $instance->isInsertable($this);
9486

95-
if(is_int($code) || $code === false) {
87+
if (is_int($code) || $code === false) {
9688
return $code ?: 0;
9789
}
9890

@@ -101,40 +93,33 @@ protected function canInsert(Layout $instance)
10193

10294
/**
10395
* Get all the inserted layout instances as a collection.
104-
*
105-
* @return \Whitecube\LaravelFlexibleContent\LayoutsCollection
10696
*/
107-
public function instances() : LayoutsCollection
97+
public function instances(): LayoutsCollection
10898
{
109-
if(! $this->instances) {
110-
$this->instances = new LayoutsCollection();
99+
if (! $this->instances) {
100+
$this->instances = new LayoutsCollection;
111101
}
112102

113103
return $this->instances;
114104
}
115105

116106
/**
117107
* Get all the inserted layout instances serialized for display in a user interface.
118-
*
119-
* @return array
120108
*/
121-
public function instancesValues() : array
109+
public function instancesValues(): array
122110
{
123111
return $this->instances()
124-
->map(fn(Layout $instance) => $instance->toDisplayableArray())
112+
->map(fn (Layout $instance) => $instance->toDisplayableArray())
125113
->values()
126114
->all();
127115
}
128116

129117
/**
130118
* Get the amount of inserted layout instances, total or per layout key.
131-
*
132-
* @param null|string $key
133-
* @return int
134119
*/
135120
public function count(?string $key = null): int
136121
{
137-
if(! $this->instances) {
122+
if (! $this->instances) {
138123
return 0;
139124
}
140125

src/Concerns/HasLayouts.php

+16-27
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace Whitecube\LaravelFlexibleContent\Concerns;
44

5-
use Whitecube\LaravelFlexibleContent\Layout as BaseLayout;
6-
use Whitecube\LaravelFlexibleContent\LayoutsCollection;
75
use Whitecube\LaravelFlexibleContent\Contracts\Flexible;
86
use Whitecube\LaravelFlexibleContent\Contracts\Layout;
97
use Whitecube\LaravelFlexibleContent\Exceptions\InvalidLayoutException;
108
use Whitecube\LaravelFlexibleContent\Exceptions\InvalidLayoutKeyException;
9+
use Whitecube\LaravelFlexibleContent\Layout as BaseLayout;
10+
use Whitecube\LaravelFlexibleContent\LayoutsCollection;
1111

1212
trait HasLayouts
1313
{
@@ -21,31 +21,30 @@ trait HasLayouts
2121
/**
2222
* Add an instanciable layout to the Flexible container.
2323
*
24-
* @param mixed $layout
25-
* @param null|int $limit
24+
* @param mixed $layout
2625
* @return $this
2726
*/
28-
public function register(string|Layout|callable $layout, ?int $limit = null) : Flexible
27+
public function register(string|Layout|callable $layout, ?int $limit = null): Flexible
2928
{
30-
if(is_string($layout) && is_a($layout, Layout::class, true)) {
29+
if (is_string($layout) && is_a($layout, Layout::class, true)) {
3130
$layout = new $layout;
3231
} elseif (is_callable($layout)) {
3332
$layout = call_user_func($layout, ($instance = new BaseLayout)) ?? $instance;
3433
}
3534

36-
if(! is_a($layout, Layout::class)) {
35+
if (! is_a($layout, Layout::class)) {
3736
throw InvalidLayoutException::make($layout);
3837
}
3938

40-
if(! ($key = $layout->getKey())) {
39+
if (! ($key = $layout->getKey())) {
4140
throw InvalidLayoutKeyException::make($layout);
4241
}
4342

44-
if($this->hasLayout($key)) {
43+
if ($this->hasLayout($key)) {
4544
throw InvalidLayoutKeyException::make($layout, $key, true);
4645
}
4746

48-
if(! is_null($limit)) {
47+
if (! is_null($limit)) {
4948
$layout->limit($limit);
5049
}
5150

@@ -56,49 +55,39 @@ public function register(string|Layout|callable $layout, ?int $limit = null) : F
5655

5756
/**
5857
* Check if the Flexible container has a defined layout for given key.
59-
*
60-
* @param string $key
61-
* @return bool
6258
*/
63-
public function hasLayout(string $key) : bool
59+
public function hasLayout(string $key): bool
6460
{
6561
return $this->layouts ? $this->layouts->has($key) : false;
6662
}
6763

6864
/**
6965
* Get the defined layout for given key in the Flexible container.
70-
*
71-
* @param string $key
72-
* @return null|\Whitecube\LaravelFlexibleContent\Contracts\Layout
7366
*/
74-
public function getLayout(string $key) : ?Layout
67+
public function getLayout(string $key): ?Layout
7568
{
7669
return $this->layouts ? $this->layouts->get($key) : null;
7770
}
7871

7972
/**
8073
* Get all the defined layouts as a collection.
81-
*
82-
* @return \Whitecube\LaravelFlexibleContent\LayoutsCollection
8374
*/
84-
public function layouts() : LayoutsCollection
75+
public function layouts(): LayoutsCollection
8576
{
86-
if(! $this->layouts) {
87-
$this->layouts = new LayoutsCollection();
77+
if (! $this->layouts) {
78+
$this->layouts = new LayoutsCollection;
8879
}
8980

9081
return $this->layouts;
9182
}
9283

9384
/**
9485
* Get all the defined layouts serialized for display in a menu.
95-
*
96-
* @return array
9786
*/
98-
public function layoutsMenu() : array
87+
public function layoutsMenu(): array
9988
{
10089
return $this->layouts()
101-
->map(fn(Layout $layout) => $layout->toButtonArray())
90+
->map(fn (Layout $layout) => $layout->toButtonArray())
10291
->values()
10392
->all();
10493
}

src/Concerns/HasResolver.php

+14-16
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ trait HasResolver
3333
/**
3434
* Define the callback that should be used to setup the Flexible container's value.
3535
*
36-
* @param callable $buildCallback
3736
* @return $this
3837
*/
3938
public function buildUsing(callable $buildCallback)
@@ -46,7 +45,7 @@ public function buildUsing(callable $buildCallback)
4645
/**
4746
* Define the callback that should be used when saving the Flexible container's value.
4847
*
49-
* @param callable $saveCallback
48+
* @param callable $saveCallback
5049
* @return $this
5150
*/
5251
public function serializeUsing(callable $serializeCallback)
@@ -59,7 +58,6 @@ public function serializeUsing(callable $serializeCallback)
5958
/**
6059
* Define a default serialization format method that should be used when saving the Flexible container's value.
6160
*
62-
* @param string $serializeFormat
6361
* @return $this
6462
*/
6563
public function serializeUsingFormat(string $serializeFormat)
@@ -72,13 +70,14 @@ public function serializeUsingFormat(string $serializeFormat)
7270
/**
7371
* Create the Flexible container's base layout instances.
7472
*
75-
* @param mixed $data
73+
* @param mixed $data
7674
* @return $this
7775
*/
7876
public function build($data = null)
7977
{
80-
if(! is_null($this->buildCallback)) {
78+
if (! is_null($this->buildCallback)) {
8179
call_user_func($this->buildCallback, $this, $data);
80+
8281
return $this;
8382
}
8483

@@ -88,18 +87,18 @@ public function build($data = null)
8887
/**
8988
* Create the Flexible container's base layout instances from given value.
9089
*
91-
* @param mixed $data
90+
* @param mixed $data
9291
* @return $this
9392
*/
9493
public function buildFromData($data = null)
9594
{
96-
if($data instanceof Collection) {
95+
if ($data instanceof Collection) {
9796
$data = $data->toArray();
98-
} else if (is_string($data)) {
97+
} elseif (is_string($data)) {
9998
$data = json_decode($data, true) ?? [];
10099
}
101100

102-
if(! $data || ! is_array($data)) {
101+
if (! $data || ! is_array($data)) {
103102
return $this;
104103
}
105104

@@ -113,12 +112,12 @@ public function buildFromData($data = null)
113112
/**
114113
* Attempt to insert a single instance from serialized value.
115114
*
116-
* @param mixed $item
115+
* @param mixed $item
117116
* @return $this
118117
*/
119118
public function buildItem($item)
120119
{
121-
if(! is_array($item) && ! is_object($item)) {
120+
if (! is_array($item) && ! is_object($item)) {
122121
return $this;
123122
}
124123

@@ -138,7 +137,7 @@ public function buildItem($item)
138137
*/
139138
public function serialize()
140139
{
141-
if(! is_null($this->serializeCallback)) {
140+
if (! is_null($this->serializeCallback)) {
142141
return call_user_func($this->serializeCallback, $this);
143142
}
144143

@@ -148,14 +147,13 @@ public function serialize()
148147
/**
149148
* Transform the current value in the desired format.
150149
*
151-
* @param string $format
152150
* @return mixed
153151
*/
154152
public function serializeAs(string $format)
155153
{
156-
$serializationMethod = 'serializeAs' . ucfirst($format);
154+
$serializationMethod = 'serializeAs'.ucfirst($format);
157155

158-
if(! method_exists($this, $serializationMethod)) {
156+
if (! method_exists($this, $serializationMethod)) {
159157
throw InvalidSerializationFormatException::make($format);
160158
}
161159

@@ -170,7 +168,7 @@ public function serializeAs(string $format)
170168
public function serializeAsArray()
171169
{
172170
return $this->instances()
173-
->map(fn(Layout $instance) => $instance->toSerializableArray())
171+
->map(fn (Layout $instance) => $instance->toSerializableArray())
174172
->all();
175173
}
176174

0 commit comments

Comments
 (0)