diff --git a/src/Resolvers/DataCollectableFromSomethingResolver.php b/src/Resolvers/DataCollectableFromSomethingResolver.php index 6b1e2067..d30b1446 100644 --- a/src/Resolvers/DataCollectableFromSomethingResolver.php +++ b/src/Resolvers/DataCollectableFromSomethingResolver.php @@ -57,10 +57,10 @@ public function execute( DataTypeKind::DataArray, DataTypeKind::Array => $this->normalizeToArray($normalizedItems), DataTypeKind::DataEnumerable, DataTypeKind::Enumerable => new $intoType->name($this->normalizeToArray($normalizedItems)), DataTypeKind::DataCollection => new $intoType->name($dataClass, $this->normalizeToArray($normalizedItems)), - DataTypeKind::DataPaginatedCollection => new $intoType->name($dataClass, $this->normalizeToPaginator($normalizedItems, $collectableMetaData)), - DataTypeKind::DataCursorPaginatedCollection => new $intoType->name($dataClass, $this->normalizeToCursorPaginator($normalizedItems, $collectableMetaData)), - DataTypeKind::DataPaginator, DataTypeKind::Paginator => $this->normalizeToPaginator($normalizedItems, $collectableMetaData), - DataTypeKind::DataCursorPaginator, DataTypeKind::CursorPaginator => $this->normalizeToCursorPaginator($normalizedItems, $collectableMetaData), + DataTypeKind::DataPaginatedCollection => new $intoType->name($dataClass, $this->normalizeToPaginator($normalizedItems, $collectableMetaData, null)), + DataTypeKind::DataCursorPaginatedCollection => new $intoType->name($dataClass, $this->normalizeToCursorPaginator($normalizedItems, $collectableMetaData, null)), + DataTypeKind::DataPaginator, DataTypeKind::Paginator => $this->normalizeToPaginator($normalizedItems, $collectableMetaData, $intoType), + DataTypeKind::DataCursorPaginator, DataTypeKind::CursorPaginator => $this->normalizeToCursorPaginator($normalizedItems, $collectableMetaData, $intoType), default => throw CannotCreateDataCollectable::create(get_debug_type($items), $intoType->name) }; } @@ -173,6 +173,7 @@ protected function normalizeToArray( protected function normalizeToPaginator( array|Paginator|AbstractPaginator|CursorPaginator|AbstractCursorPaginator $items, CollectableMetaData $collectableMetaData, + ?NamedType $intoType, ): Paginator|AbstractPaginator { if ($items instanceof Paginator || $items instanceof AbstractPaginator) { return $items; @@ -180,7 +181,9 @@ protected function normalizeToPaginator( $normalizedItems = $this->normalizeToArray($items); - return new LengthAwarePaginator( + $type = $intoType->name ?? LengthAwarePaginator::class; + + return new ($type)( $normalizedItems, $collectableMetaData->paginator_total ?? count($items), $collectableMetaData->paginator_per_page ?? 15, @@ -190,6 +193,7 @@ protected function normalizeToPaginator( protected function normalizeToCursorPaginator( array|Paginator|AbstractPaginator|CursorPaginator|AbstractCursorPaginator $items, CollectableMetaData $collectableMetaData, + ?NamedType $intoType, ): CursorPaginator|AbstractCursorPaginator { if ($items instanceof CursorPaginator || $items instanceof AbstractCursorPaginator) { return $items; @@ -197,7 +201,9 @@ protected function normalizeToCursorPaginator( $normalizedItems = $this->normalizeToArray($items); - return new \Illuminate\Pagination\CursorPaginator( + $type = $intoType->name ?? \Illuminate\Pagination\CursorPaginator::class; + + return new ($type)( $normalizedItems, $collectableMetaData->paginator_per_page ?? 15, $collectableMetaData->paginator_cursor diff --git a/src/Support/Iterables/Concerns/IterableData.php b/src/Support/Iterables/Concerns/IterableData.php new file mode 100644 index 00000000..253fea98 --- /dev/null +++ b/src/Support/Iterables/Concerns/IterableData.php @@ -0,0 +1,94 @@ +include($include); + } + + if ($exclude) { + $factory->exclude($exclude); + } + + if ($only) { + $factory->only($only); + } + + if ($except) { + $factory->except($except); + } + + if ($includePermanently) { + $factory->includePermanently($includePermanently); + } + + if ($excludePermanently) { + $factory->excludePermanently($excludePermanently); + } + + if ($onlyPermanently) { + $factory->onlyPermanently($onlyPermanently); + } + + if ($exceptPermanently) { + $factory->exceptPermanently($exceptPermanently); + } + + if ($wrap === true) { + $factory->withWrapping(); + } + + if ($wrap === false) { + $factory->withoutWrapping(); + } + + if ($transformValues !== null) { + $factory->withValueTransformation($transformValues); + } + + if ($mapPropertyNames !== null) { + $factory->withPropertyNameMapping($mapPropertyNames); + } + + $transformationContext = null; + + foreach ($this->items as $key => $value) { + $this->items[$key] = $value instanceof $dataClass + ? $value + : $dataClass::from($value); + + $transformationContext ??= $factory->create($this->items[$key]); + + + } + + return $this; + } + + public function toArray(): array + { + return DataContainer::get() + } +} diff --git a/src/Support/Iterables/LengthAwareDataPaginator.php b/src/Support/Iterables/LengthAwareDataPaginator.php new file mode 100644 index 00000000..f0427409 --- /dev/null +++ b/src/Support/Iterables/LengthAwareDataPaginator.php @@ -0,0 +1,11 @@ +getPartialsContainer()->includePartials ??= new PartialsCollection(); - - foreach ($includes as $include) { - $partialsCollection->attach(Partial::create($include)); - } + $this->expandPartials( + $this->getPartialsContainer()->includePartials ??= new PartialsCollection(), + $includes, + permanent: false, + ); return $this; } - public function includePermanently(string ...$includes): static + public function includePermanently(string|array ...$includes): static { - $partialsCollection = $this->getPartialsContainer()->includePartials ??= new PartialsCollection(); - - foreach ($includes as $include) { - $partialsCollection->attach(Partial::create($include, permanent: true)); - } + $this->expandPartials( + $this->getPartialsContainer()->includePartials ??= new PartialsCollection(), + $includes, + permanent: true, + ); return $this; } - public function exclude(string ...$excludes): static + public function exclude(string|array ...$excludes): static { - $partialsCollection = $this->getPartialsContainer()->excludePartials ??= new PartialsCollection(); - - foreach ($excludes as $exclude) { - $partialsCollection->attach(Partial::create($exclude)); - } + $this->expandPartials( + $this->getPartialsContainer()->excludePartials ??= new PartialsCollection(), + $excludes, + permanent: false, + ); return $this; } - public function excludePermanently(string ...$excludes): static + public function excludePermanently(string|array ...$excludes): static { - $partialsCollection = $this->getPartialsContainer()->excludePartials ??= new PartialsCollection(); - - foreach ($excludes as $exclude) { - $partialsCollection->attach(Partial::create($exclude, permanent: true)); - } + $this->expandPartials( + $this->getPartialsContainer()->excludePartials ??= new PartialsCollection(), + $excludes, + permanent: true, + ); return $this; } - public function only(string ...$only): static + public function only(string|array ...$only): static { - $partialsCollection = $this->getPartialsContainer()->onlyPartials ??= new PartialsCollection(); - - foreach ($only as $onlyDefinition) { - $partialsCollection->attach(Partial::create($onlyDefinition)); - } + $this->expandPartials( + $this->getPartialsContainer()->onlyPartials ??= new PartialsCollection(), + $only, + permanent: false, + ); return $this; } - public function onlyPermanently(string ...$only): static + public function onlyPermanently(string|array ...$only): static { - $partialsCollection = $this->getPartialsContainer()->onlyPartials ??= new PartialsCollection(); - - foreach ($only as $onlyDefinition) { - $partialsCollection->attach(Partial::create($onlyDefinition, permanent: true)); - } + $this->expandPartials( + $this->getPartialsContainer()->onlyPartials ??= new PartialsCollection(), + $only, + permanent: true, + ); return $this; } - public function except(string ...$except): static + public function except(string|array ...$except): static { - $partialsCollection = $this->getPartialsContainer()->exceptPartials ??= new PartialsCollection(); - - foreach ($except as $exceptDefinition) { - $partialsCollection->attach(Partial::create($exceptDefinition)); - } + $this->expandPartials( + $this->getPartialsContainer()->exceptPartials ??= new PartialsCollection(), + $except, + permanent: false, + ); return $this; } - public function exceptPermanently(string ...$except): static + public function exceptPermanently(string|array ...$except): static { - $partialsCollection = $this->getPartialsContainer()->exceptPartials ??= new PartialsCollection(); - - foreach ($except as $exceptDefinition) { - $partialsCollection->attach(Partial::create($exceptDefinition, permanent: true)); - } + $this->expandPartials( + $this->getPartialsContainer()->exceptPartials ??= new PartialsCollection(), + $except, + permanent: true, + ); return $this; } + /** + * @param array|array> $partials + */ + protected function expandPartials( + PartialsCollection $partialsCollection, + array $partials, + bool $permanent, + ): void { + foreach ($partials as $partial) { + if (is_string($partial)) { + $partialsCollection->attach(Partial::create($partial, permanent: $permanent)); + + continue; + } + + if (! is_array($partial)) { + continue; + } + + foreach ($partial as $key => $subPartial) { + if (is_string($key) && is_callable($subPartial)) { + $partialsCollection->attach(Partial::createConditional($key, condition: $subPartial, permanent: $permanent)); + + continue; + } + + if (is_string($key) && $subPartial === true) { + $partialsCollection->attach(Partial::create($key, permanent: $permanent)); + + continue; + } + + if (is_string($subPartial)) { + $partialsCollection->attach(Partial::create($subPartial, permanent: $permanent)); + + continue; + } + } + } + } + public function includeWhen(string $include, bool|Closure $condition, bool $permanent = false): static { $partialsCollection = $this->getPartialsContainer()->includePartials ??= new PartialsCollection(); diff --git a/tests/Support/Iterables/LengthtAwareDataPaginatorTest.php b/tests/Support/Iterables/LengthtAwareDataPaginatorTest.php new file mode 100644 index 00000000..749dcb35 --- /dev/null +++ b/tests/Support/Iterables/LengthtAwareDataPaginatorTest.php @@ -0,0 +1,35 @@ +data(SimpleData::class); + // + // + // return SimpleData::collect(["Hello", "World", "Weclome"], LengthAwareDataPaginator::class)->data( + // include: 'single', + // exclude: ['multiple', 'items'], + // only: [ + // 'callable' => fn(Data $data) => $data->property === 'something', + // ], + // includePermanently: 'permanently', + // wrap: null, // default + // // wrap: false // disabled + // // wrap: 'key' // defined + // transformValues: true, // + // mapPropertyNames: true, + // ); + }); + + get('/')->dump(); +});