Skip to content

Commit 122de08

Browse files
committed
Improve lodash structure and code style
1 parent 0015f79 commit 122de08

File tree

7 files changed

+1119
-850
lines changed

7 files changed

+1119
-850
lines changed

composer.lock

Lines changed: 1078 additions & 804 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Lodash/Http/Requests/RestrictsExtraAttributes.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ private function checkForNotAllowedProperties(): void
6060
// Ignore marked properties
6161
if (! empty($this->ignoreExtraProperties)) {
6262
foreach ($this->ignoreExtraProperties as $deleleValue) {
63-
if (($key = array_search($deleleValue, $validationData)) !== false) {
63+
$key = array_search($deleleValue, $validationData);
64+
if ($key !== false) {
6465
unset($validationData[$key]);
6566
}
6667
}

src/Lodash/Http/Resources/JsonResource.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,39 @@ abstract class JsonResource extends BaseResource
3636
public function getTransformed(): array
3737
{
3838
if ($this->resource instanceof TransformableContract) {
39-
return $this->transformToApi($this->resource);
39+
return static::transformToApi($this->resource);
4040
}
4141

4242
return [];
4343
}
4444

45+
public function setWith(array $data): self
46+
{
47+
$this->with = $data;
48+
49+
return $this;
50+
}
51+
52+
public function getWith(): array
53+
{
54+
return $this->with;
55+
}
56+
57+
public function appendWith(array $data): self
58+
{
59+
$this->with = array_merge_recursive($this->with, $data);
60+
61+
return $this;
62+
}
63+
4564
public function toArray($request): array
4665
{
4766
$data = $this->getResourceData();
4867

68+
if (! empty($this->with)) {
69+
$data += Arr::except($this->with, ['id', 'type', 'attributes']);
70+
}
71+
4972
$relationsData = $this->getRelationsData();
5073

5174
if (! empty($relationsData)) {

src/Lodash/ServiceProvider.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@
1818
use Longman\LaravelLodash\Validation\StrictTypeValidator;
1919
use Longman\LaravelLodash\Validation\Validator;
2020

21-
use function app;
2221
use function array_keys;
23-
use function array_pad;
2422
use function config;
2523
use function config_path;
26-
use function preg_split;
2724
use function resource_path;
2825
use function str_replace;
29-
use function trim;
3026

3127
class ServiceProvider extends LaravelServiceProvider
3228
{
@@ -75,8 +71,6 @@ function (Translator $translator, array $data, array $rules, array $messages): V
7571
},
7672
);
7773

78-
$this->registerBladeDirectives();
79-
8074
$this->loadTranslations();
8175
//$this->loadValidations();
8276
}
@@ -103,28 +97,6 @@ protected function registerCommands(): void
10397
$this->commands(array_keys($this->commands));
10498
}
10599

106-
protected function registerBladeDirectives(): void
107-
{
108-
if (! config('lodash.register.blade_directives')) {
109-
return;
110-
}
111-
112-
// Display relative time
113-
app('blade.compiler')->directive('datetime', static function ($expression) {
114-
return "<?php echo '<time datetime=\'' . with($expression)->toIso8601String()
115-
. '\' title=\'' . $expression . '\'>'
116-
. with($expression)->diffForHumans() . '</time>' ?>";
117-
});
118-
119-
// Pluralization helper
120-
app('blade.compiler')->directive('plural', static function ($expression) {
121-
$expression = trim($expression, '()');
122-
[$count, $str, $spacer] = array_pad(preg_split('/,\s*/', $expression), 3, "' '");
123-
124-
return "<?php echo $count . $spacer . str_plural($str, $count) ?>";
125-
});
126-
}
127-
128100
protected function registerRequestMacros(): void
129101
{
130102
if (! config('lodash.register.request_macros')) {

src/Lodash/Testing/Attributes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use function array_key_exists;
1212
use function array_replace_recursive;
13+
use function count;
1314
use function explode;
1415
use function str_starts_with;
1516

src/Lodash/Testing/DataStructuresProvider.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use function array_shift;
1111
use function call_user_func_array;
1212
use function explode;
13-
use function get_called_class;
1413
use function is_callable;
1514
use function lcfirst;
1615
use function property_exists;
@@ -20,20 +19,6 @@
2019

2120
abstract class DataStructuresProvider
2221
{
23-
public static function __callStatic(string $name, array $arguments): mixed
24-
{
25-
$property = lcfirst(Str::substr($name, 3));
26-
if (! property_exists(get_called_class(), $property)) {
27-
throw new InvalidArgumentException('Property "' . $property . '" does not exists');
28-
}
29-
$structure = static::$$property;
30-
31-
$parameters = [&$structure, $arguments[0] ?? []];
32-
call_user_func_array([get_called_class(), 'includeNestedRelations'], $parameters);
33-
34-
return $structure;
35-
}
36-
3722
protected static function includeNestedRelations(array &$item, array $relations): void
3823
{
3924
if (empty($relations)) {
@@ -90,4 +75,18 @@ protected static function getItemStructure(string $relationItem): array
9075

9176
return static::$method();
9277
}
78+
79+
public static function __callStatic(string $name, array $arguments): mixed
80+
{
81+
$property = lcfirst(Str::substr($name, 3));
82+
if (! property_exists(static::class, $property)) {
83+
throw new InvalidArgumentException('Property "' . $property . '" does not exists');
84+
}
85+
$structure = static::$$property;
86+
87+
$parameters = [&$structure, $arguments[0] ?? []];
88+
call_user_func_array([static::class, 'includeNestedRelations'], $parameters);
89+
90+
return $structure;
91+
}
9392
}

src/config/config.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
],
3535

3636
'register' => [
37-
'blade_directives' => false,
3837
'request_macros' => false,
3938
'translations' => true,
4039
'validation_rules' => true,

0 commit comments

Comments
 (0)