Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions lib/Helper/ColumnsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use OCA\Tables\Constants\UsergroupType;
use OCA\Tables\Db\Column;
use OCA\Tables\Service\ColumnTypes\IColumnTypeBusiness;
use OCP\Server;

class ColumnsHelper {

Expand All @@ -20,6 +22,11 @@ class ColumnsHelper {
Column::TYPE_USERGROUP,
];

/**
* @var array<string, IColumnTypeBusiness>
*/
private array $columnBusinesses = [];

public function __construct(
private UserHelper $userHelper,
private CircleHelper $circleHelper,
Expand Down Expand Up @@ -79,4 +86,18 @@ public function resolveSearchValue(string $placeholder, string $userId, ?Column
default: return $placeholder;
}
}

public function getColumnBusiness(Column $column): IColumnTypeBusiness {
$cacheKey = implode(':', [$column->getType(), $column->getSubtype()]);
if (isset($this->columnBusinesses[$cacheKey])) {
return $this->columnBusinesses[$cacheKey];
}

$businessClassName = 'OCA\Tables\Service\ColumnTypes\\';
$businessClassName .= ucfirst($column->getType()) . ucfirst($column->getSubtype()) . 'Business';
/** @var IColumnTypeBusiness $columnBusiness */
$columnBusiness = Server::get($businessClassName);

return $this->columnBusinesses[$cacheKey] = $columnBusiness;
}
}
12 changes: 6 additions & 6 deletions lib/Service/ColumnTypes/DatetimeBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
use Exception;
use OCA\Tables\Db\Column;

class DatetimeBusiness extends SuperBusiness implements IColumnTypeBusiness {
class DatetimeBusiness extends SuperBusiness {

/**
* @param mixed $value (string|null)
* @param Column|null $column
* @param Column $column
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
public function parseValue($value, Column $column): string {
if ($value === '' || $value === null) {
return '';
}
Expand All @@ -38,15 +38,15 @@ public function parseValue($value, ?Column $column = null): string {
$this->logger->debug('Could not parse format for datetime value', ['exception' => $e]);
}

return json_encode($newDateTime !== '' ? $newDateTime : '');
return json_encode($newDateTime);
}

/**
* @param mixed $value (string|null)
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsed($value, ?Column $column = null): bool {
public function canBeParsed($value, Column $column): bool {
if ($value === '' || $value === null) {
return true;
}
Expand Down
11 changes: 5 additions & 6 deletions lib/Service/ColumnTypes/DatetimeDateBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@

use OCA\Tables\Db\Column;

class DatetimeDateBusiness extends SuperBusiness implements IColumnTypeBusiness {
class DatetimeDateBusiness extends SuperBusiness {

/**
* @param mixed $value (string|null)
* @param Column|null $column
* @param Column $column
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
public function parseValue($value, Column $column): string {
return json_encode($this->isValidDate((string)$value, 'Y-m-d') ? (string)$value : '');
}

/**
* @param mixed $value (string|null)
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsed($value, ?Column $column = null): bool {
public function canBeParsed($value, Column $column): bool {
return $this->isValidDate((string)$value, 'Y-m-d');
}

}
10 changes: 5 additions & 5 deletions lib/Service/ColumnTypes/DatetimeTimeBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

use OCA\Tables\Db\Column;

class DatetimeTimeBusiness extends SuperBusiness implements IColumnTypeBusiness {
class DatetimeTimeBusiness extends SuperBusiness {

/**
* @param mixed $value (string|null)
* @param Column|null $column
* @param Column $column
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
public function parseValue($value, Column $column): string {
return json_encode($this->isValidDate((string)$value, 'H:i') ? $value : '');
}

/**
* @param mixed $value (string|null)
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsed($value, ?Column $column = null): bool {
public function canBeParsed($value, Column $column): bool {
return $this->isValidDate((string)$value, 'H:i');
}

Expand Down
22 changes: 11 additions & 11 deletions lib/Service/ColumnTypes/IColumnTypeBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ interface IColumnTypeBusiness {
* FIXME: Why is this not using Mapper methods which should do the same thing
*
* @param mixed $value
* @param Column|null $column
* @param Column $column
* @return string value stringify
*/
public function parseValue($value, ?Column $column): string;
public function parseValue($value, Column $column): string;

/**
* tests if the given value can be parsed to a value of the column type
* Tests if the given value can be parsed to a value of the column type
*
* @param mixed $value
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsed($value, ?Column $column): bool;
public function canBeParsed($value, Column $column): bool;

/**
* @throws BadRequestError In case the value is not valid
Expand All @@ -44,20 +44,20 @@ public function canBeParsed($value, ?Column $column): bool;
public function validateValue(mixed $value, Column $column, string $userId, int $tableId, ?int $rowId): void;

/**
* tests if the given string can be parsed to a value/id of the column type
* Tests if the given string can be parsed to a value/id of the column type
*
* @param mixed $value
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsedDisplayValue($value, ?Column $column): bool;
public function canBeParsedDisplayValue($value, Column $column): bool;

/**
* parses the given string to a value/id of the column type
* Parses the given string to a value/id of the column type
*
* @param mixed $value
* @param Column|null $column
* @param Column $column
* @return string
*/
public function parseDisplayValue($value, ?Column $column): string;
public function parseDisplayValue($value, Column $column): string;
}
10 changes: 5 additions & 5 deletions lib/Service/ColumnTypes/NumberBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

use OCA\Tables\Db\Column;

class NumberBusiness extends SuperBusiness implements IColumnTypeBusiness {
class NumberBusiness extends SuperBusiness {

/**
* @param mixed $value (int|float|string|null)
* @param Column|null $column
* @param Column $column
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
public function parseValue($value, Column $column): string {
if ($value === null) {
return '';
}
Expand All @@ -26,10 +26,10 @@ public function parseValue($value, ?Column $column = null): string {

/**
* @param mixed $value (int|float|string|null)
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsed($value, ?Column $column = null): bool {
public function canBeParsed($value, Column $column): bool {
return !$value || floatval($value);
}

Expand Down
10 changes: 5 additions & 5 deletions lib/Service/ColumnTypes/NumberProgressBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

use OCA\Tables\Db\Column;

class NumberProgressBusiness extends SuperBusiness implements IColumnTypeBusiness {
class NumberProgressBusiness extends SuperBusiness {

/**
* @param mixed $value (int|string|null)
* @param Column|null $column
* @param Column $column
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
public function parseValue($value, Column $column): string {
return json_encode((int)$value);
}

/**
* @param mixed $value (int|string|null)
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsed($value, ?Column $column = null): bool {
public function canBeParsed($value, Column $column): bool {
return !$value || ((int)$value >= 0 && (int)$value <= 100);
}

Expand Down
10 changes: 5 additions & 5 deletions lib/Service/ColumnTypes/NumberStarsBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@

use OCA\Tables\Db\Column;

class NumberStarsBusiness extends SuperBusiness implements IColumnTypeBusiness {
class NumberStarsBusiness extends SuperBusiness {

/**
* @param mixed $value (null|int|string)
* @param Column|null $column
* @param Column $column
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
public function parseValue($value, Column $column): string {
return json_encode((int)$value);
}

/**
* @param mixed $value (null|int|string)
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsed($value, ?Column $column = null): bool {
public function canBeParsed($value, Column $column): bool {
return !$value || in_array((int)$value, [0,1,2,3,4,5]);
}

Expand Down
32 changes: 7 additions & 25 deletions lib/Service/ColumnTypes/SelectionBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@

use OCA\Tables\Db\Column;

class SelectionBusiness extends SuperBusiness implements IColumnTypeBusiness {
class SelectionBusiness extends SuperBusiness {

/**
* @param mixed $value (array|string|null)
* @param Column|null $column
* @param Column $column
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
if (!$column) {
$this->logger->warning('No column given, but expected on ' . __FUNCTION__ . ' within ' . __CLASS__, ['exception' => new \Exception()]);
return '';
}

public function parseValue($value, Column $column): string {
$intValue = (int)$value;
if (!is_numeric($value) || $intValue != $value) {
return '';
Expand All @@ -36,12 +31,7 @@ public function parseValue($value, ?Column $column = null): string {
return '';
}

public function parseDisplayValue($value, ?Column $column = null): string {
if (!$column) {
$this->logger->warning('No column given, but expected on ' . __FUNCTION__ . ' within ' . __CLASS__, ['exception' => new \Exception()]);
return '';
}

public function parseDisplayValue($value, Column $column): string {
foreach ($column->getSelectionOptionsArray() as $option) {
if ($option['label'] === $value) {
return json_encode($option['id']);
Expand All @@ -53,14 +43,10 @@ public function parseDisplayValue($value, ?Column $column = null): string {

/**
* @param mixed $value (array|string|null)
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsed($value, ?Column $column = null): bool {
if (!$column) {
$this->logger->warning('No column given, but expected on ' . __FUNCTION__ . ' within ' . __CLASS__, ['exception' => new \Exception()]);
return false;
}
public function canBeParsed($value, Column $column): bool {
if ($value === null) {
return true;
}
Expand All @@ -79,11 +65,7 @@ public function canBeParsed($value, ?Column $column = null): bool {
return false;
}

public function canBeParsedDisplayValue($value, ?Column $column = null): bool {
if (!$column) {
$this->logger->warning('No column given, but expected on ' . __FUNCTION__ . ' within ' . __CLASS__, ['exception' => new \Exception()]);
return false;
}
public function canBeParsedDisplayValue($value, Column $column): bool {
if ($value === null) {
return true;
}
Expand Down
11 changes: 5 additions & 6 deletions lib/Service/ColumnTypes/SelectionCheckBusiness.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@

use OCA\Tables\Db\Column;

class SelectionCheckBusiness extends SuperBusiness implements IColumnTypeBusiness {
class SelectionCheckBusiness extends SuperBusiness {
public const PATTERN_POSITIVE = ['yes', '1', true, 1, 'true', 'TRUE'];
public const PATTERN_NEGATIVE = ['no', '0', false, 0, 'false', 'FALSE'];

/**
* @param mixed $value
* @param Column|null $column
* @param Column $column
* @return string
*/
public function parseValue($value, ?Column $column = null): string {
public function parseValue($value, Column $column): string {
$found = in_array($value, self::PATTERN_POSITIVE, true);
return json_encode($found ? 'true' : 'false');
}

/**
* @param mixed $value
* @param Column|null $column
* @param Column $column
* @return bool
*/
public function canBeParsed($value, ?Column $column = null): bool {
public function canBeParsed($value, Column $column): bool {
if ($value === null) {
return true;
}

return in_array($value, self::PATTERN_POSITIVE) || in_array($value, self::PATTERN_NEGATIVE) ;
}

}
Loading
Loading