Skip to content
Merged
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
14 changes: 12 additions & 2 deletions src/ActiveRecordAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PDOStatement;
use RuntimeException;
use Sanovskiy\SimpleObject\Collections\QueryResult;
use Sanovskiy\SimpleObject\DataTransformers\BooleanTransformer;
use Sanovskiy\SimpleObject\Query\Filter;
use Sanovskiy\SimpleObject\Traits\ActiveRecordIteratorTrait;
use Sanovskiy\Utility\NamingStyle;
Expand Down Expand Up @@ -563,12 +564,19 @@ public function getDataForSave(bool $applyTransforms = true): ?array
$value = $this->values[$property];
}

//Boolean transformer workaround
BooleanTransformer::setDatabaseDriver(ConnectionManager::getConfig(static::getSimpleObjectConfigNameWrite())->getDriver());
$rule = $this->getTransformRuleForField($tableFieldName);

if ($applyTransforms && $value !== null && !empty($rule)) {
$value = $rule->toDatabaseValue($value);
}

$data[$tableFieldName] = $value;
// Make sure null values are not added to the data array
if ($value !== null) {
$data[$tableFieldName] = $value;
}

}

return $data;
Expand Down Expand Up @@ -626,6 +634,8 @@ public function store(string $propertyOrField, mixed $value): void
if ($rule->isValidDatabaseData($value)) {
$propertyValue = $rule->toProperty($value);
} elseif ($rule->isValidPropertyData($value)) {
//Boolean transformer workaround
BooleanTransformer::setDatabaseDriver(ConnectionManager::getConfig(static::getSimpleObjectConfigNameWrite())->getDriver());
$DBValue = $rule->toDatabaseValue($value);
}
}
Expand Down Expand Up @@ -721,7 +731,7 @@ public function save(bool $force = false): bool
$this->loadedValues = $data;
return true;
} catch (Exception $e) {
throw new RuntimeException('SimpleObject error: ' . $e->getMessage(), $e->getCode(), $e);
throw new RuntimeException('SimpleObject error: ' . $e->getMessage(), (int) $e->getCode(), $e);
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/DataTransformers/BooleanTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public static function toProperty($value, $params = null): ?bool
if (in_array($value, ['0',0,'false','N'],true)){
return false;
}
if (is_bool($value)){
return $value;
}
throw new InvalidArgumentException('Unsupported value for '.__METHOD__);
}

Expand All @@ -26,16 +29,16 @@ public static function toDatabaseValue($value, $params = null): ?string
return match (static::$databaseDriver) {
'mysql' => $value ? '1' : '0',
'pgsql', 'mssql' => $value ? 't' : 'f',
default => throw new RuntimeException('Unsupported driver'),
default => throw new RuntimeException('Unsupported drive '.static::$databaseDriver),
};
}

public static function isValidDatabaseData($value): bool
public static function isValidDatabaseData($value, ?array $params = null): bool
{
return in_array($value,['1',1,'true','Y','0',0,'false','N'],true);
}

public static function isValidPropertyData($value): bool
public static function isValidPropertyData($value, ?array $params = null): bool
{
return is_bool($value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/DataTransformers/DateTimeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public static function toDatabaseValue($value, $params=null): ?string
return $value->format($date_format);
}

public static function isValidDatabaseData($value): bool
public static function isValidDatabaseData($value, ?array $params = null): bool
{
return (is_null($value) || (is_string($value) && strtotime($value) !== false));
}

public static function isValidPropertyData($value): bool
public static function isValidPropertyData($value, ?array $params = null): bool
{
return is_null($value) || $value instanceof DateTime;
}
Expand Down
8 changes: 4 additions & 4 deletions src/DataTransformers/EnumTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function setValidValues(array $validValues): void
self::$validValues = $validValues;
}

public static function toProperty($value, $params = null)
public static function toProperty($value, ?array $params = null)
{
if (!static::isValidDatabaseData($value,$params)) {
throw new InvalidArgumentException('Invalid data for ' . __METHOD__);
Expand All @@ -25,7 +25,7 @@ public static function toProperty($value, $params = null)
return $value;
}

public static function toDatabaseValue($value, $params = null)
public static function toDatabaseValue($value, ?array $params = null)
{
if (!static::isValidPropertyData($value,$params)) {
throw new InvalidArgumentException('Invalid data for ' . __METHOD__);
Expand All @@ -34,12 +34,12 @@ public static function toDatabaseValue($value, $params = null)
return $value;
}

public static function isValidDatabaseData($value, $params = null): bool
public static function isValidDatabaseData($value, ?array $params = null): bool
{
return in_array($value, $params['allowed_values']);
}

public static function isValidPropertyData($value, $params = null): bool
public static function isValidPropertyData($value, ?array $params = null): bool
{
return in_array($value, $params['allowed_values'], true);
}
Expand Down
4 changes: 2 additions & 2 deletions src/DataTransformers/FloatTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public static function toDatabaseValue($value, $params = null): ?string
return (string) $value;
}

public static function isValidDatabaseData($value): bool
public static function isValidDatabaseData($value, ?array $params = null): bool
{
return is_null($value) || is_numeric($value);
}

public static function isValidPropertyData($value): bool
public static function isValidPropertyData($value, ?array $params = null): bool
{
return is_null($value) || is_float($value);
}
Expand Down
8 changes: 4 additions & 4 deletions src/DataTransformers/IntegerTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class IntegerTransformer extends DataTransformerAbstract
{
public static function toProperty($value, $params = null): ?int
public static function toProperty($value, ?array $params = null): ?int
{
if (is_null($value)){return null;}

Expand All @@ -17,7 +17,7 @@ public static function toProperty($value, $params = null): ?int
return (int) $value;
}

public static function toDatabaseValue($value, $params = null): ?string
public static function toDatabaseValue($value, ?array $params = null): ?string
{
if (is_null($value)){return null;}

Expand All @@ -27,12 +27,12 @@ public static function toDatabaseValue($value, $params = null): ?string
return (string) $value;
}

public static function isValidDatabaseData($value): bool
public static function isValidDatabaseData($value, ?array $params = null): bool
{
return is_null($value) || is_numeric($value);
}

public static function isValidPropertyData($value): bool
public static function isValidPropertyData($value, ?array $params = null): bool
{
return is_null($value) || is_int($value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/DataTransformers/JsonTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ public static function toDatabaseValue($value, $params = null): ?string
return (string)json_encode($value);
}

public static function isValidDatabaseData($value): bool
public static function isValidDatabaseData($value, ?array $params = null): bool
{
return is_null($value) || (json_decode($value) !== null && json_last_error() === JSON_ERROR_NONE);
}

public static function isValidPropertyData($value): bool
public static function isValidPropertyData($value, ?array $params = null): bool
{
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/DataTransformers/UUIDTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class UUIDTransformer extends DataTransformerAbstract
{
protected static string $pattern = '/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/i';

public static function toProperty($value, $params = null): ?string
public static function toProperty($value, ?array $params = null): ?string
{
if (is_null($value)){return null;}

Expand All @@ -18,7 +18,7 @@ public static function toProperty($value, $params = null): ?string
return $value;
}

public static function toDatabaseValue($value, $params = null): ?string
public static function toDatabaseValue($value, ?array $params = null): ?string
{
if (is_null($value)){return null;}

Expand All @@ -28,12 +28,12 @@ public static function toDatabaseValue($value, $params = null): ?string
return (string) $value;
}

public static function isValidDatabaseData($value): bool
public static function isValidDatabaseData($value, ?array $params = null): bool
{
return is_string($value) && preg_match(static::$pattern, $value);
}

public static function isValidPropertyData($value): bool
public static function isValidPropertyData($value, ?array $params = null): bool
{
return self::isValidDatabaseData($value);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Interfaces/DataTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

interface DataTransformerInterface
{
public static function toProperty($value, $params = null);
public static function toProperty($value, ?array $params = null);

public static function toDatabaseValue($value, $params = null);
public static function toDatabaseValue($value, ?array $params = null);

public static function isValidDatabaseData($value);
public static function isValidDatabaseData($value, ?array $params = null);

public static function isValidPropertyData($value);
public static function isValidPropertyData($value, ?array $params = null);

}
30 changes: 20 additions & 10 deletions src/Query/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,28 @@ private function buildQuery(): void
$this->sqlInstructions .= ' ORDER BY ' . $config[self::PH_DELIMITERS][self::PH_D_LEFT] . $this->filters[$instruction][0] . $config[self::PH_DELIMITERS][self::PH_D_RIGHT] . ' ' . strtoupper($this->filters[$instruction][1] ?? 'asc');
break;
case self::LIMIT:
if (!is_array($this->filters[$instruction])){
$this->filters[$instruction] = [$this->filters[$instruction],0];
if (!is_array($this->filters[$instruction])) {
$this->filters[$instruction] = [$this->filters[$instruction], 0];
}
$bind = [$this->filters[$instruction][0]];
$limitSQL = $config[self::PH_LIMIT];
$offsetSQL = '';
if (count($this->filters[$instruction]) > 1) {
$offsetSQL .= ' ' . $config[self::PH_OFFSET];
$bind[] = $this->filters[$instruction][1];

$limit = (int) $this->filters[$instruction][0];
$offset = (int) ($this->filters[$instruction][1] ?? 0);

if (ConnectionManager::getConfig($this->modelClass::getSimpleObjectConfigNameRead())?->getDriver() === 'mysql') {
$this->sqlInstructions .= " LIMIT $limit OFFSET $offset";
} else {
$bind = [$limit];
$limitSQL = $config[self::PH_LIMIT];
$offsetSQL = '';

if (count($this->filters[$instruction]) > 1) {
$offsetSQL .= ' ' . $config[self::PH_OFFSET];
$bind[] = $offset;
}

$this->sqlInstructions .= ' ' . ($config[self::PH_LIMIT_INVERT] ? ($offsetSQL . ' ' . $limitSQL) : ($limitSQL . ' ' . $offsetSQL));
$this->bindInstructions = array_merge($this->bindInstructions, $config[self::PH_LIMIT_INVERT] ? array_reverse($bind) : $bind);
}
$this->sqlInstructions .= ' ' . ($config[self::PH_LIMIT_INVERT] ? ($offsetSQL . ' ' . $limitSQL) : ($limitSQL . ' ' . $offsetSQL));
$this->bindInstructions = array_merge($this->bindInstructions, $config[self::PH_LIMIT_INVERT] ? array_reverse($bind) : $bind);
break;
case self::GROUP:
$this->sqlInstructions .= ' ' . $config[self::PH_GROUP] . ' ' . $config[self::PH_DELIMITERS][self::PH_D_LEFT] . $this->filters[$instruction][0] . $config[self::PH_DELIMITERS][self::PH_D_RIGHT];
Expand Down
5 changes: 2 additions & 3 deletions src/TransformRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ public function toDatabaseValue(mixed $value): mixed

public function isValidPropertyData(mixed $value): bool
{
return $this->transformerClass::isValidPropertyData($value);
return $this->transformerClass::isValidPropertyData($value, $this->transformerParams);
}

public function isValidDatabaseData(mixed $value): bool
{
return $this->transformerClass::isValidDatabaseData($value);
return $this->transformerClass::isValidDatabaseData($value, $this->transformerParams);
}

/**
Expand All @@ -59,5 +59,4 @@ public function getPropertyType(): string
return $this->propertyType;
}


}