Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
akbarali1 committed Sep 12, 2024
1 parent 6ea88e4 commit 58b7f4b
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 163 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ return $viewModel->toView('organization.store');
```php
$viewModel = OrganizationViewModel::fromDataObject($orgData);
```

# 1.9 version add `toCsv` method

```php
Expand Down Expand Up @@ -209,4 +210,8 @@ final class PotentialPartnerController extends Controller
return $model;
}
}
```
```

# 2.0 version

`CollectionViewModel` and `PaginationViewModel` add function `setSnakeCase`
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name":"akbarali/view-model",
"version":"1.9.0",
"version":"2.0",
"description":"View Model PHP Laravel",
"license":"MIT",
"minimum-stability": "stable",
Expand Down
154 changes: 82 additions & 72 deletions src/CollectionViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,77 +11,87 @@

class CollectionViewModel implements ViewModelContract
{
protected Collection $dataCollection;
protected string $viewModel;

/**
* @param iterable $dataCollection
* @param string $viewModel
*/
public function __construct(iterable $dataCollection, string $viewModel)
{
$this->dataCollection = collect($dataCollection);
$this->viewModel = $viewModel;

$this->dataCollection->transform(function ($value) use ($viewModel) {
return new $viewModel($value);
});
}

public function toView(string $viewName, array $additionalParams = []): Factory|View|Application
{
return view($viewName, array_merge(['items' => $this->dataCollection, 'item' => $additionalParams]));
}

public function toJsonApi($toSnakeCase = true): ApiResponse
{
if ($toSnakeCase) {
return ApiResponse::getSuccessResponse($this->toSnakeCase($this->dataCollection));
}

return ApiResponse::getSuccessResponse($this->dataCollection);
}

/**
* @param iterable $dataCollection
* @param string $viewModel
* @return CollectionViewModel
*/
public static function createFromArray(iterable $dataCollection, string $viewModel): CollectionViewModel
{
return new static($dataCollection, $viewModel);
}

protected function toSnakeCase(iterable $items): array
{
$res = [];
try {
foreach ($items as $item) {
$row = [];
if (is_scalar($item)) {
$row = $item;
} elseif (is_array($item)) {
foreach ($item as $itemKey => $itemVal) {
$row[Str::snake($itemKey)] = is_iterable($itemVal) ? $this->toSnakeCase($itemVal) : $itemVal;
}
} else {
$class = new \ReflectionClass($item);
$properties = $class->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($properties as $reflectionProperty) {
if ($reflectionProperty->isStatic()) {
continue;
}
$value = $reflectionProperty->getValue($item);
$row[Str::snake($reflectionProperty->getName())] = is_iterable($value) ? $this->toSnakeCase($value) : $value;
}
}
$res[] = $row;
}

} catch (\Exception $exception) {
}

return $res;
}
protected Collection $dataCollection;
protected string $viewModel;
protected bool $toSnakeCase = false;

/**
* @param iterable $dataCollection
* @param string $viewModel
*/
public function __construct(iterable $dataCollection, string $viewModel)
{
$this->dataCollection = collect($dataCollection);
$this->viewModel = $viewModel;

$this->dataCollection->transform(function ($value) use ($viewModel) {
return new $viewModel($value);
});
}

public function toView(string $viewName, array $additionalParams = []): Factory|View|Application
{
return view($viewName, array_merge(['items' => $this->dataCollection, 'item' => $additionalParams]));
}

public function setSnakeCase(): static
{
$this->toSnakeCase = true;

return $this;
}

public function toJsonApi($toSnakeCase = true): ApiResponse
{
$this->toSnakeCase = $toSnakeCase;

if ($toSnakeCase) {
return ApiResponse::getSuccessResponse($this->toSnakeCase($this->dataCollection));
}

return ApiResponse::getSuccessResponse($this->dataCollection);
}

/**
* @param iterable $dataCollection
* @param string $viewModel
* @return CollectionViewModel
*/
public static function createFromArray(iterable $dataCollection, string $viewModel): CollectionViewModel
{
return new static($dataCollection, $viewModel);
}

protected function toSnakeCase(iterable $items): array
{
$res = [];
try {
foreach ($items as $item) {
$row = [];
if (is_scalar($item)) {
$row = $item;
} elseif (is_array($item)) {
foreach ($item as $itemKey => $itemVal) {
$row[Str::snake($itemKey)] = is_iterable($itemVal) ? $this->toSnakeCase($itemVal) : $itemVal;
}
} else {
$class = new \ReflectionClass($item);
$properties = $class->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($properties as $reflectionProperty) {
if ($reflectionProperty->isStatic()) {
continue;
}
$value = $reflectionProperty->getValue($item);
$row[Str::snake($reflectionProperty->getName())] = is_iterable($value) ? $this->toSnakeCase($value) : $value;
}
}
$res[] = $row;
}

} catch (\Exception $exception) {
}

return $res;
}
}

188 changes: 99 additions & 89 deletions src/PaginationViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,94 +12,104 @@

class PaginationViewModel implements ViewModelContract
{
protected DataObjectCollection $dataCollection;
protected string $viewModel;

public LengthAwarePaginator $pagination;

/**
* @param DataObjectCollection $dataCollection
* @param string $viewModel
*/
public function __construct(DataObjectCollection $dataCollection, string $viewModel)
{
$this->dataCollection = $dataCollection;
$this->viewModel = $viewModel;

$this->dataCollection->items->transform(fn($value) => new $viewModel($value));

$parameters = request()->getQueryString();
$parameters = preg_replace('/&page(=[^&]*)?|^page(=[^&]*)?&?/', '', $parameters);
$path = url(request()->path()).(empty($parameters) ? '' : '?'.$parameters);

$this->pagination = new LengthAwarePaginator($this->dataCollection->items, $this->dataCollection->totalCount, $this->dataCollection->limit, $this->dataCollection->page);
$this->pagination->withPath($path);
}

public function toView(string $viewName, ...$args): Factory|View|Application
{
return view($viewName, array_merge(['pagination' => $this->pagination], $args[0] ?? []));
}

public function toJsonApi($toSnakeCase = true, array $additionalParams = []): ApiResponse
{
if ($toSnakeCase) {
$data = [
'items' => $this->toSnakeCase($this->dataCollection->items),
'page' => $this->dataCollection->page,
'limit' => $this->dataCollection->limit,
'totalCount' => $this->dataCollection->totalCount,
];
if (count($additionalParams) > 0) {
$data = array_merge($data, $additionalParams);
}

return ApiResponse::getSuccessResponse($data);
}

$data = [
'items' => $this->dataCollection->items,
'page' => $this->dataCollection->page,
'limit' => $this->dataCollection->limit,
'totalCount' => $this->dataCollection->totalCount,
];
if (count($additionalParams) > 0) {
$data = array_merge($data, $additionalParams);
}

return ApiResponse::getSuccessResponse($data);
}

protected function toSnakeCase(iterable $items): array
{
$res = [];
try {
foreach ($items as $item) {
$row = [];
if (is_scalar($item)) {
$row = $item;
} elseif (is_array($item)) {
foreach ($item as $itemKey => $itemVal) {
$row[Str::snake($itemKey)] = is_iterable($itemVal) ? $this->toSnakeCase($itemVal) : $itemVal;
}
} else {
$class = new \ReflectionClass($item);
$properties = $class->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($properties as $reflectionProperty) {
if ($reflectionProperty->isStatic()) {
continue;
}
$value = $reflectionProperty->getValue($item);
$row[Str::snake($reflectionProperty->getName())] = is_iterable($value) ? $this->toSnakeCase($value) : $value;
}
}
$res[] = $row;
}

} catch (\Exception $exception) {
}

return $res;
}
protected DataObjectCollection $dataCollection;
protected string $viewModel;
protected bool $toSnakeCase = false;

public LengthAwarePaginator $pagination;

/**
* @param DataObjectCollection $dataCollection
* @param string $viewModel
*/
public function __construct(DataObjectCollection $dataCollection, string $viewModel)
{
$this->dataCollection = $dataCollection;
$this->viewModel = $viewModel;

$this->dataCollection->items->transform(fn($value) => new $viewModel($value));

$parameters = request()->getQueryString();
$parameters = preg_replace('/&page(=[^&]*)?|^page(=[^&]*)?&?/', '', $parameters);
$path = url(request()->path()).(empty($parameters) ? '' : '?'.$parameters);

$this->pagination = new LengthAwarePaginator($this->dataCollection->items, $this->dataCollection->totalCount, $this->dataCollection->limit, $this->dataCollection->page);
$this->pagination->withPath($path);
}

public function toView(string $viewName, ...$args): Factory|View|Application
{
return view($viewName, array_merge(['pagination' => $this->pagination], $args[0] ?? []));
}

public function setSnakeCase(): static
{
$this->toSnakeCase = true;

return $this;
}

public function toJsonApi($toSnakeCase = true, array $additionalParams = []): ApiResponse
{
$this->toSnakeCase = $toSnakeCase;

if ($toSnakeCase) {
$data = [
'items' => $this->toSnakeCase($this->dataCollection->items),
'page' => $this->dataCollection->page,
'limit' => $this->dataCollection->limit,
'totalCount' => $this->dataCollection->totalCount,
];
if (count($additionalParams) > 0) {
$data = array_merge($data, $additionalParams);
}

return ApiResponse::getSuccessResponse($data);
}

$data = [
'items' => $this->dataCollection->items,
'page' => $this->dataCollection->page,
'limit' => $this->dataCollection->limit,
'totalCount' => $this->dataCollection->totalCount,
];
if (count($additionalParams) > 0) {
$data = array_merge($data, $additionalParams);
}

return ApiResponse::getSuccessResponse($data);
}

protected function toSnakeCase(iterable $items): array
{
$res = [];
try {
foreach ($items as $item) {
$row = [];
if (is_scalar($item)) {
$row = $item;
} elseif (is_array($item)) {
foreach ($item as $itemKey => $itemVal) {
$row[Str::snake($itemKey)] = is_iterable($itemVal) ? $this->toSnakeCase($itemVal) : $itemVal;
}
} else {
$class = new \ReflectionClass($item);
$properties = $class->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($properties as $reflectionProperty) {
if ($reflectionProperty->isStatic()) {
continue;
}
$value = $reflectionProperty->getValue($item);
$row[Str::snake($reflectionProperty->getName())] = is_iterable($value) ? $this->toSnakeCase($value) : $value;
}
}
$res[] = $row;
}

} catch (\Exception $exception) {
}

return $res;
}
}

0 comments on commit 58b7f4b

Please sign in to comment.