first version, no prod ready

This commit is contained in:
Jonathan Martz
2025-06-24 21:43:34 +02:00
parent 81e8486870
commit a21e474749
22 changed files with 566 additions and 693 deletions

View File

@@ -1,213 +1,146 @@
<?php
namespace Pb;
namespace PocketBase;
use PocketBase\ViewModel\CustomRecordViewModel;
use PocketBase\ViewModel\RecordListViewModel;
use PocketBase\ViewModel\RecordViewModel;
/**
*
*/
class Collection
{
/**
* @var string
*/
private string $collection;
public HttpClient $http;
private Client $client;
private string $name;
public string $path;
/**
* @var string
*/
private string $url;
/**
* @var string
*/
private static string $token = '';
/**
* @param string $url
* @param string $collection
* @param string $token
*/
public function __construct(string $url, string $collection, string $token = '')
public function getPath(): string
{
$this->url = $url;
$this->collection = $collection;
if (!empty($token)) {
self::$token = $token;
}
return $this->path;
}
/**
* @param int $start
* @param int $end
* @param array $queryParams
* @return array
*/
public function getList(int $start = 1, int $end = 50, array $queryParams = []): array
public function setPath(string $path): Collection
{
$queryParams['perPage'] = $end;
$this->path = $path;
return $this;
}
public function __construct(string $url = null, string $name = 'users')
{
$this->name = $name;
$this->http = new HttpClient();
$this->client = new Client();
}
public function getFullList(int $batch, $bodyParams = [], array $queryParams = []): RecordListViewModel
{
$queryParams['limit'] = $batch;
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
return json_decode($response, JSON_FORCE_OBJECT);
return new RecordListViewModel(
json_decode(
$this->http->doRequest(
$this->client->getBaseUrl() . $this->path . '?' . $getParams,
'GET',
$bodyParams,
$this->client->token
),
true)
);
}
/**
* @param string $recordId
* @param string $field
* @param string $filepath
* @return void
*/
public function upload(string $recordId, string $field, string $filepath): void
public function getList(int $page, int $limit, $bodyParams = [], array $queryParams = []): RecordListViewModel
{
$ch = curl_init($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId);
curl_setopt_array($ch, array(
CURLOPT_CUSTOMREQUEST => 'PATCH',
CURLOPT_POSTFIELDS => array(
$field => new \CURLFile($filepath)
)
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array('Content-Type: multipart/form-data');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
}
/**
* @param string $email
* @param string $password
* @return void
*/
public function authAsUser(string $email, string $password): string
{
$result = $this->doRequest($this->url . "/api/collections/users/auth-with-password", 'POST', ['identity' => $email, 'password' => $password]);
if (!empty($result['token'])) {
self::$token = $result['token'];
}
return $result;
}
/**
* @param int $batch
* @param array $queryParams
* @return array
*/
public function getFullList(array $queryParams, int $batch = 200): array
{
$queryParams = [... $queryParams, 'perPage' => $batch];
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
return json_decode($response, JSON_FORCE_OBJECT);
return new RecordListViewModel(json_decode(
$this->http->doRequest(
$this->client->getBaseUrl() . $this->path . '?' . $getParams,
'GET',
$bodyParams,
$this->client->token
), true));
}
/**
* @param string $filter
* @param array $queryParams
* @return array
*/
public function getFirstListItem(string $filter, array $queryParams = []): array
public function authAsUser(string $email, string $password): ?string
{
$queryParams['perPage'] = 1;
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
$response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET');
return json_decode($response, JSON_FORCE_OBJECT)['items'][0];
$data = json_decode(
$this->http->doRequest(
$this->client->getBaseUrl() . '/api/collections/users/auth-with-password' . '?' . $getParams,
'POST',
[
'identity' => $email,
'password' => $password,
]
), true);
return $data['token'];
}
/**
* @param array $bodyParams
* @param array $queryParams
* @return void
*/
public function create(array $bodyParams = [], array $queryParams = []): string
public function authAsAdmin(string $email, string $password): ?string
{
return $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records", 'POST', $bodyParams);
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
$data = json_decode($this->http->doRequest(
$this->client->baseurl . "/api/collections/_superusers/auth-with-password" . '?' . $getParams,
'POST',
[
'identity' => $email,
'password' => $password,
]), true);
return $data['token'];
}
/**
* @param string $recordId
* @param array $bodyParams
* @param array $queryParams
* @return void
*/
public function update(string $recordId, array $bodyParams = [], array $queryParams = []): void
public function create(array $bodyParams, array $queryParams = []): CustomRecordViewModel
{
// Todo bodyParams equals json, currently workaround
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'PATCH', $bodyParams);
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
return new CustomRecordViewModel(json_decode(
$this->http->doRequest(
$this->client->getBaseUrl() . $this->getPath() . '?' . $getParams,
'POST',
$bodyParams,
$this->client->token
), true) ?? []);
}
/**
* @param string $recordId
* @param array $queryParams
* @return void
*/
public function delete(string $recordId, array $queryParams = []): void
public function update($id, $bodyParams, $token = '', array $queryParams = []): RecordViewModel
{
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE');
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
return new RecordViewModel(json_decode($this->http->doRequest(
$this->client->getBaseUrl() . '/api/collections/test/records/' . $id . '?' . $getParams
, 'PATCH', json_encode($bodyParams), $token), true));
}
/**
* @param string $recordId
* @param string $url
* @param string $method
* @return bool|string
*/
public function doRequest(string $url, string $method, $bodyParams = []): string
public function getOne($id, $token = '', array $queryParams = []): CustomRecordViewModel
{
// TODO move doRequestIntoService ?
// TODO replace curl with HttpClient
$ch = curl_init();
if (self::$token != '') {
$headers = array(
'Content-Type:application/json',
'Authorization: ' . self::$token
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
if ($bodyParams) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$output = curl_exec($ch);
curl_close($ch);
return $output;
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
return new CustomRecordViewModel(json_decode($this->http->doRequest(
$this->client->getBaseUrl() . '/api/collections/test/records/' . $id . '?' . $getParams#
, 'GET', [], $token), true));
}
/**
* @param string $recordId
* @param array $queryParams
* @return mixed
*/
public function getOne(string $recordId, array $queryParams = []): array
public function delete($id, $token = '', array $queryParams = []): CustomRecordViewModel
{
$output = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'GET');
return json_decode($output, JSON_FORCE_OBJECT);
$getParams = !empty($queryParams) ? http_build_query($queryParams) : "";
return new CustomRecordViewModel(json_decode($this->http->doRequest(
$this->client->getBaseUrl() . '/api/collections/test/records/' . $id . '?' . $getParams
, 'DELETE', [], $token), true));
}
/**
* @param string $email
* @param string $password
* @return void
*/
public function authAsAdmin(string $email, string $password): string
public function setClient(Client $client): void
{
$bodyParams['identity'] = $email;
$bodyParams['password'] = $password;
$output = $this->doRequest($this->url . "/api/collections/_superusers/auth-with-password", 'POST', $bodyParams);
$token = json_decode($output, true)['token'];
if ($token) {
self::$token = $token;
}
return $output;
$this->client = $client;
}
}
public function getName(): string
{
return $this->name;
}
public function getAuthToken(): ?string
{
return $this->client->token;
}
public function setHttp(HttpClient $httpClient): void
{
$this->http = $httpClient;
}
}