Skip to content

Commit

Permalink
Remove deprecated methods
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorejb committed Oct 7, 2024
1 parent e2b2a76 commit aed10ab
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 218 deletions.
91 changes: 8 additions & 83 deletions src/Entities.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

use PeachySQL\PeachySql;
use PeachySQL\QueryBuilder\SqlParams;
use PeachySQL\SqlException;
use Teapot\{HttpException, StatusCode};

/**
* @psalm-type PropArray = array{
* col?: string, nullGroup?: bool, notDefault?: bool, alias?: string, type?: string,
* timeZone?: \DateTimeZone|null, getValue?: callable|null, dependsOn?: list<string>, output?: bool
* }
*/
abstract class Entities
{
/**
Expand All @@ -33,11 +26,7 @@ abstract protected function getMap(): array;
public function __construct(PeachySql $db)
{
$this->db = $db;
/** @psalm-suppress DeprecatedMethod */
$legacyPropMap = $this->getPropMap();
/** @var array<string, PropArray> $rawPropMap */
$rawPropMap = array_replace_recursive(Helpers::selectMapToPropMap($this->getSelectMap()), $legacyPropMap);
$bcProps = Helpers::rawPropMapToProps($rawPropMap);
$bcProps = Helpers::selectMapToPropMap($this->getSelectMap());
$selectProps = $this->getSelectProps();

foreach ($selectProps as $prop) {
Expand Down Expand Up @@ -107,24 +96,6 @@ protected function getDefaultSort(): array
return [$this->idField => 'asc'];
}

/**
* Specify a friendly error message for constraint violations (when inserting/updating rows)
* @deprecated
*/
protected function getDuplicateError(): string
{
return '';
}

/**
* Specify a friendly error message for constraint violations (when attempting to delete rows)
* @deprecated
*/
protected function getConstraintError(): string
{
return '';
}

/**
* Can be used to return a separate property map for filtering/sorting (but not inserting/updating)
*/
Expand All @@ -133,17 +104,6 @@ protected function getSelectMap(): array
return $this->getMap();
}

/**
* Merge additional property information with getSelectMap().
* Look at the Prop class constructor to see supported options.
* @deprecated Use getSelectProps() instead.
* @return array<string, PropArray>
*/
protected function getPropMap(): array
{
return [];
}

/**
* Merge additional property information with getSelectMap().
* Look at the Prop class constructor to see supported options.
Expand Down Expand Up @@ -201,30 +161,15 @@ public function deleteByIds(array $ids): int
return 0;
}

try {
return $this->db->deleteFrom($this->getTableName(), [$this->idColumn => $ids]);
} catch (SqlException $e) {
/** @psalm-suppress DeprecatedMethod */
$constraintError = $this->getConstraintError();

if ($constraintError !== '' && $e->getSqlState() === '23000') {
throw new HttpException($constraintError, StatusCode::CONFLICT, $e);
} else {
throw $e;
}
}
return $this->db->deleteFrom($this->getTableName(), [$this->idColumn => $ids]);
}

public function updateById(int|string $id, array $data): int
{
$row = Helpers::allPropertiesToColumns($this->map, $this->processValues($data, [$id]));
$row = $this->processRow($row, [$id]);

try {
return $this->db->updateRows($this->getTableName(), $row, [$this->idColumn => $id]);
} catch (SqlException $e) {
throw $this->properException($e);
}
return $this->db->updateRows($this->getTableName(), $row, [$this->idColumn => $id]);
}

/**
Expand All @@ -240,11 +185,7 @@ public function patchByIds(array $ids, array $mergePatch): int
$colVals = self::propertiesToColumns($this->map, $this->processValues($mergePatch, $ids));
$colVals = $this->processRow($colVals, $ids);

try {
return $this->db->updateRows($this->getTableName(), $colVals, [$this->idColumn => $ids]);
} catch (SqlException $e) {
throw $this->properException($e);
}
return $this->db->updateRows($this->getTableName(), $colVals, [$this->idColumn => $ids]);
}

/**
Expand Down Expand Up @@ -280,27 +221,11 @@ public function addEntities(array $entities): array
}
}

try {
$ids = $this->db->insertRows($this->getTableName(), $rows, $this->getIdentityIncrement())->ids;
foreach ($existingIds as $offset => $id) {
array_splice($ids, $offset, 0, [$id]);
}
return $ids;
} catch (SqlException $e) {
throw $this->properException($e);
}
}

private function properException(SqlException $e): \Exception
{
/** @psalm-suppress DeprecatedMethod */
$duplicateError = $this->getDuplicateError();

if ($duplicateError !== '' && $e->getSqlState() === '23000') {
return new HttpException($duplicateError, StatusCode::CONFLICT, $e);
} else {
return $e;
$ids = $this->db->insertRows($this->getTableName(), $rows, $this->getIdentityIncrement())->ids;
foreach ($existingIds as $offset => $id) {
array_splice($ids, $offset, 0, [$id]);
}
return $ids;
}

/**
Expand Down
31 changes: 2 additions & 29 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

/**
* @internal
* @psalm-import-type PropArray from Entities
*/
class Helpers
{
/**
* @return array<string, PropArray>
* @return array<string, Prop>
*/
public static function selectMapToPropMap(array $map, string $context = ''): array
{
Expand All @@ -32,7 +31,7 @@ public static function selectMapToPropMap(array $map, string $context = ''): arr
if (is_array($val)) {
$propMap = array_merge($propMap, self::selectMapToPropMap($val, $newKey));
} else {
$propMap[$newKey] = ['col' => $val];
$propMap[$newKey] = new Prop($newKey, $val);
}
}

Expand Down Expand Up @@ -236,32 +235,6 @@ public static function getFieldPropMap(array $fields, array $propMap): array
return $fieldProps;
}

/**
* @param array<string, PropArray> $map
* @return array<string, Prop>
*/
public static function rawPropMapToProps(array $map): array
{
$props = [];

foreach ($map as $name => $options) {
$props[$name] = new Prop(
$name,
$options['col'] ?? '',
$options['nullGroup'] ?? false,
!($options['notDefault'] ?? false),
$options['alias'] ?? '',
$options['type'] ?? null,
$options['timeZone'] ?? false,
$options['getValue'] ?? null,
$options['dependsOn'] ?? [],
$options['output'] ?? true
);
}

return $props;
}

/**
* @param array<Prop> $props
* @return array<string, Prop>
Expand Down
33 changes: 0 additions & 33 deletions src/QueryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,6 @@ public function __construct(array $filter, array $originalFilter, array $sort, a
$this->fieldProps = $fieldProps;
}

/**
* @deprecated Use readonly property instead
*/
public function getFilter(): array
{
return $this->filter;
}

/**
* @deprecated Use readonly property instead
*/
public function getOriginalFilter(): array
{
return $this->originalFilter;
}

/**
* @deprecated Use readonly property instead
*/
public function getSort(): array
{
return $this->sort;
}

/**
* @deprecated Use readonly property instead
* @return Prop[]
*/
public function getFieldProps(): array
{
return $this->fieldProps;
}

public function getColumns(): string
{
$columns = '';
Expand Down
8 changes: 4 additions & 4 deletions test/EntitiesDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public function testEmptyQueries(Entities $entities): void
/**
* @dataProvider entitiesProvider
*/
public function testGetDuplicateError(Entities $entities): void
public function testDuplicateError(Entities $entities): void
{
$users = [
[
Expand All @@ -228,14 +228,14 @@ public function testGetDuplicateError(Entities $entities): void
$entities->addEntities($users);
throw new \Exception('Failed to throw duplicate name exception');
} catch (\Exception $e) {
$this->assertSame('A user with this name already exists', $e->getMessage());
$this->assertStringContainsStringIgnoringCase(' duplicate ', $e->getMessage());
}
}

/**
* @dataProvider dbProvider
*/
public function testGetConstraintError(PeachySql $db): void
public function testConstraintError(PeachySql $db): void
{
$user = [
'name' => 'Some Name',
Expand All @@ -251,7 +251,7 @@ public function testGetConstraintError(PeachySql $db): void
$entities->deleteByIds([$id]);
throw new \Exception('Failed to throw constraint violation exception');
} catch (\Exception $e) {
$this->assertSame('Failed to delete user: it still has things referencing it', $e->getMessage());
$this->assertStringContainsStringIgnoringCase(' constraint ', $e->getMessage());
}
}

Expand Down
Loading

0 comments on commit aed10ab

Please sign in to comment.