1
0
Fork 0
mirror of synced 2024-05-16 10:42:34 +12:00
appwrite/src/Appwrite/Utopia/Request/Filters/V12.php

166 lines
5.2 KiB
PHP
Raw Normal View History

2021-12-30 22:25:43 +13:00
<?php
namespace Appwrite\Utopia\Request\Filters;
use Appwrite\Utopia\Request\Filter;
2021-12-31 21:40:35 +13:00
class V12 extends Filter
2021-12-30 22:25:43 +13:00
{
// Convert 0.11 params format to 0.12 format
public function parse(array $content, string $model): array
{
switch ($model) {
2022-01-01 01:00:07 +13:00
// No IDs -> Custom IDs
2021-12-30 22:25:43 +13:00
case "account.create":
2022-01-01 01:00:07 +13:00
case "account.createMagicURLSession":
case "users.create":
2022-01-01 02:30:50 +13:00
$content = $this->addId($content, 'userId');
2022-01-01 01:00:07 +13:00
break;
case "functions.create":
2022-01-01 02:30:50 +13:00
$content = $this->addId($content, 'functionId');
2022-01-01 01:00:07 +13:00
break;
case "teams.create":
2022-01-01 02:30:50 +13:00
$content = $this->addId($content, 'teamId');
2022-01-01 01:00:07 +13:00
break;
// Status integer -> boolean
case "users.updateStatus":
2022-01-01 02:30:50 +13:00
$content = $this->convertStatus($content);
break;
// Deprecating order type
case "functions.listExecutions":
$content = $this->removeOrderType($content);
2022-01-01 01:00:07 +13:00
break;
// The rest (more complex) formats
case "database.createDocument":
2022-01-01 02:30:50 +13:00
$content = $this->addId($content, 'documentId');
$content = $this->removeParentProperties($content);
break;
case "database.listDocuments":
$content = $this->removeOrderCast($content);
$content = $this->convertOrder($content);
$content = $this->convertQueries($content);
2022-01-01 01:00:07 +13:00
break;
case "database.createCollection":
2022-01-01 02:30:50 +13:00
$content = $this->addId($content, 'collectionId');
$content = $this->removeRules($content);
$content = $this->addCollectionPermissionLevel($content);
break;
case "database.updateCollection":
$content = $this->removeRules($content);
$content = $this->addCollectionPermissionLevel($content);
2021-12-30 22:25:43 +13:00
break;
}
2022-01-01 02:30:50 +13:00
return $content;
2021-12-30 22:25:43 +13:00
}
2022-01-01 02:30:50 +13:00
// New parameters
2022-01-01 04:17:57 +13:00
protected function addId(array $content, string $key): array
2022-01-01 01:00:07 +13:00
{
$content[$key] = 'unique()';
return $content;
}
2022-01-01 02:30:50 +13:00
protected function addCollectionPermissionLevel(array $content): array
{
$content['permission'] = 'document';
return $content;
}
// Deprecated parameters
protected function removeRules(array $content): array
{
unset($content['rules']);
return $content;
}
protected function removeOrderType(array $content): array
{
unset($content['orderType']);
return $content;
}
protected function removeOrderCast(array $content): array
{
unset($content['orderCast']);
return $content;
}
protected function removeParentProperties(array $content): array
{
2022-01-05 01:30:50 +13:00
if (isset($content['parentDocument'])) unset($content['parentDocument']);
if (isset($content['parentProperty'])) unset($content['parentProperty']);
if (isset($content['parentPropertyType'])) unset($content['parentPropertyType']);
2022-01-01 02:30:50 +13:00
return $content;
}
// Modified parameters
2022-01-01 01:00:07 +13:00
protected function convertStatus(array $content): array
2021-12-30 22:25:43 +13:00
{
2022-01-05 01:30:50 +13:00
if (isset($content['status'])) {
$content['status'] = $content['status'] === 2 ? false : true;
}
2022-01-01 02:30:50 +13:00
return $content;
}
protected function convertOrder(array $content): array
{
2022-01-05 01:30:50 +13:00
if (isset($content['orderField'])) {
$content['orderAttributes'] = [ $content['orderField'] ];
unset($content['orderField']);
}
2022-01-01 02:30:50 +13:00
2022-01-05 01:30:50 +13:00
if (isset($content['orderType'])) {
$content['orderTypes'] = [ $content['orderType'] ];
unset($content['orderType']);
}
2022-01-01 02:30:50 +13:00
return $content;
}
protected function convertQueries(array $content): array
{
2022-01-01 04:45:49 +13:00
$queries = [];
if(!empty($content['filters'])) {
foreach ($content['filters'] as $filter) {
$operators = ['=' => 'equal', '!=' => 'notEqual', '>' => 'greater', '<' => 'lesser', '<=' => 'lesserEqual', '>=' => 'greaterEqual'];
foreach ($operators as $operator => $operatorVerbose) {
if (\str_contains($filter, $operator)) {
$usedOperator = $operator;
break;
}
}
if(isset($usedOperator)) {
[ $attributeKey, $filterValue ] = \explode($usedOperator, $filter);
2022-01-04 22:05:21 +13:00
if($filterValue === 'true' || $filterValue === 'false') {
// Let's keep it at true and false string, but without "" around
// No action needed
} else {
$filterValue = \is_numeric($filterValue) ? $filterValue : '"' . $filterValue . '"';
}
2022-01-04 22:05:21 +13:00
$query = $attributeKey . '.' . $operators[$usedOperator] . '(' . $filterValue . ')';
2022-01-01 04:45:49 +13:00
\array_push($queries, $query);
}
}
}
2022-01-04 22:05:21 +13:00
// We cannot migrate search properly
unset($content['search']);
2022-01-01 04:45:49 +13:00
unset($content['filters']);
$content['queries'] = $queries;
2022-01-01 02:30:50 +13:00
2021-12-30 22:25:43 +13:00
return $content;
}
}