2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
namespace openvk\Web\Models\Repositories;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
use openvk\Web\Models\Entities\{Gift, GiftCategory};
|
|
|
|
|
|
|
|
class Gifts
|
|
|
|
{
|
|
|
|
private $context;
|
|
|
|
private $gifts;
|
|
|
|
private $cats;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function __construct()
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
|
|
$this->gifts = $this->context->table("gifts");
|
|
|
|
$this->cats = $this->context->table("gift_categories");
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function get(int $id): ?Gift
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$gift = $this->gifts->get($id);
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$gift) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
return new Gift($gift);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getCat(int $id): ?GiftCategory
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$cat = $this->cats->get($id);
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$cat) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
return new GiftCategory($cat);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getCategories(int $page, ?int $perPage = null, &$count = nullptr): \Traversable
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$cats = $this->cats->where("deleted", false);
|
|
|
|
$count = $cats->count();
|
|
|
|
$cats = $cats->page($page, $perPage ?? OPENVK_DEFAULT_PER_PAGE);
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($cats as $cat) {
|
2021-10-07 11:48:55 +03:00
|
|
|
yield new GiftCategory($cat);
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2021-10-07 11:48:55 +03:00
|
|
|
}
|
2023-09-18 18:09:25 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getCategoriesCount(): int
|
2023-09-18 18:09:25 +03:00
|
|
|
{
|
|
|
|
$cats = $this->cats->where("deleted", false);
|
|
|
|
return $cats->count();
|
|
|
|
}
|
2021-10-07 11:48:55 +03:00
|
|
|
}
|