Skip to content

Commit

Permalink
RecordInterface refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
swayok committed Aug 18, 2022
1 parent 796a529 commit 618c1eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 55 deletions.
6 changes: 1 addition & 5 deletions src/ORM/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,7 @@ public static function getRelation(string $name): Relation
return $relations[$name];
}

/**
* @param string $name
* @return bool
*/
public static function hasRelation($name): bool
public static function hasRelation(string $name): bool
{
return isset(static::getRelations()[$name]);
}
Expand Down
79 changes: 29 additions & 50 deletions src/ORM/RecordInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,16 @@

namespace PeskyORM\ORM;

use PeskyORM\Exception\OrmException;
use PeskyORM\Exception\RecordNotFoundException;

interface RecordInterface
{

/**
* Create new empty record
* @return static
*/
public static function newEmptyRecord(): RecordInterface;

/**
* @return TableInterface
*/
public static function getTable(): TableInterface;

/**
* @param string $name
* @return bool
*/
public static function hasColumn(string $name): bool;

/**
Expand All @@ -34,9 +23,21 @@ public static function hasColumn(string $name): bool;
*/
public static function getColumn(string $name, string &$format = null): Column;

public static function getPrimaryKeyColumnName(): string;

public static function hasPrimaryKeyColumn(): bool;

public static function getPrimaryKeyColumn(): Column;

public static function getRelations(): array;

public static function hasRelation(string $name): bool;

public static function getRelation(string $name): Relation;

/**
* Resets all values and related records
* @return static|RecordInterface
* @return static
*/
public function reset();

Expand All @@ -61,7 +62,7 @@ public function hasValue($column, bool $trueIfThereIsDefaultValue = false): bool
* @param string|Column $column
* @param mixed $value
* @param boolean $isFromDb
* @return static|RecordInterface
* @return static
*/
public function updateValue($column, $value, bool $isFromDb);

Expand All @@ -73,7 +74,6 @@ public function getPrimaryKeyValue();

/**
* Check if there is a value for primary key column
* @return bool
*/
public function hasPrimaryKeyValue(): bool;

Expand All @@ -95,7 +95,7 @@ public function getRelatedRecord(string $relationName, bool $loadIfNotSet = fals
/**
* Read related object(s). If there are already loaded object(s) - they will be overwritten
* @param string $relationName - name of relation defined in TableStructure
* @return static|RecordInterface
* @return static
*/
public function readRelatedRecord(string $relationName);

Expand All @@ -111,14 +111,14 @@ public function isRelatedRecordAttached(string $relationName): bool;
* @param array|Record|RecordsArray $relatedRecord
* @param bool|null $isFromDb - true: marks values as loaded from DB | null: autodetect
* @param bool $haltOnUnknownColumnNames - exception will be thrown is there is unknown column names in $data
* @return static|RecordInterface
* @return static
*/
public function updateRelatedRecord($relationName, $relatedRecord, ?bool $isFromDb = null, bool $haltOnUnknownColumnNames = true);

/**
* Remove related record
* @param string $relationName
* @return static|RecordInterface
* @return static
*/
public function unsetRelatedRecord(string $relationName);

Expand All @@ -128,17 +128,14 @@ public function unsetRelatedRecord(string $relationName);
* @param array $data
* @param bool $isFromDb - true: marks values as loaded from DB
* @param bool $haltOnUnknownColumnNames - exception will be thrown if there are unknown column names in $data
* @return static|RecordInterface
* @throws \InvalidArgumentException
* @return static
*/
public function fromData(array $data, bool $isFromDb = false, bool $haltOnUnknownColumnNames = true);

/**
* Fill record values from passed $data.
* All values are marked as loaded from DB and any unknows column names will raise exception
* @param array $data
* @return static|RecordInterface
* @throws \InvalidArgumentException
* @return static
*/
public function fromDbData(array $data);

Expand All @@ -147,7 +144,7 @@ public function fromDbData(array $data);
* @param int|float|string $pkValue
* @param array $columns - empty: get all columns
* @param array $readRelatedRecords - also read related records
* @return static|RecordInterface
* @return static
*/
public function fetchByPrimaryKey($pkValue, array $columns = [], array $readRelatedRecords = []);

Expand All @@ -157,7 +154,7 @@ public function fetchByPrimaryKey($pkValue, array $columns = [], array $readRela
* @param array $conditionsAndOptions
* @param array $columns - empty: get all columns
* @param array $readRelatedRecords - also read related records
* @return static|RecordInterface
* @return static
*/
public function fetch(array $conditionsAndOptions, array $columns = [], array $readRelatedRecords = []);

Expand All @@ -166,21 +163,14 @@ public function fetch(array $conditionsAndOptions, array $columns = [], array $r
* Note: record must exist in DB
* @param array $columns - columns to read
* @param array $readRelatedRecords - also read related records
* @return static|RecordInterface
* @throws RecordNotFoundException
* @return static
*/
public function reload(array $columns = [], array $readRelatedRecords = []);

/**
* Read values for specific columns
* @param array $columns - columns to read
* @return static|RecordInterface
* @throws OrmException
* @throws \UnexpectedValueException
* @throws \PDOException
* @throws \BadMethodCallException
* @throws \InvalidArgumentException
* @throws RecordNotFoundException
* @return static
*/
public function readColumns(array $columns = []);

Expand All @@ -190,8 +180,7 @@ public function readColumns(array $columns = []);
* @param array $data
* @param bool $isFromDb - true: marks values as loaded from DB
* @param bool $haltOnUnknownColumnNames - exception will be thrown is there is unknown column names in $data
* @return static|RecordInterface
* @throws \InvalidArgumentException
* @return static
*/
public function updateValues(array $data, bool $isFromDb = false, bool $haltOnUnknownColumnNames = true);

Expand All @@ -201,8 +190,7 @@ public function updateValues(array $data, bool $isFromDb = false, bool $haltOnUn
* Notes:
* - commit() and rollback() will throw exception if used without begin()
* - save() will throw exception if used after begin()
* @return static|RecordInterface
* @throws \BadMethodCallException
* @return static
*/
public function begin();

Expand All @@ -216,8 +204,7 @@ public function isCollectingUpdates(): bool;
/**
* Restore values updated since begin()
* Note: throws exception if used without begin()
* @return static|RecordInterface
* @throws \BadMethodCallException
* @return static
*/
public function rollback();

Expand All @@ -230,8 +217,7 @@ public function rollback();
* - false: ignore related records that exist in db but their pk value is not listed in current set of records
* Example: there are 3 records in DB: 1, 2, 3. You're trying to save records 2 and 3 (record 1 is absent).
* If $deleteNotListedRelatedRecords === true then record 1 will be deleted; else - it will remain untouched
* @return static|RecordInterface
* @throws \BadMethodCallException
* @return static
*/
public function commit(array $relationsToSave = [], bool $deleteNotListedRelatedRecords = false);

Expand All @@ -244,8 +230,7 @@ public function commit(array $relationsToSave = [], bool $deleteNotListedRelated
* - false: ignore related records that exist in db but their pk value is not listed in current set of records
* Example: there are 3 records in DB: 1, 2, 3. You're trying to save records 2 and 3 (record 1 is absent).
* If $deleteNotListedRelatedRecords === true then record 1 will be deleted; else - it will remain untouched
* @return static|RecordInterface
* @throws \BadMethodCallException
* @return static
*/
public function save(array $relationsToSave = [], bool $deleteNotListedRelatedRecords = false);

Expand All @@ -257,7 +242,6 @@ public function save(array $relationsToSave = [], bool $deleteNotListedRelatedRe
* - false: ignore related records that exist in db but their pk value is not listed in current set of records
* Example: there are 3 records in DB: 1, 2, 3. You're trying to save records 2 and 3 (record 1 is absent).
* If $deleteNotListedRelatedRecords === true then record 1 will be deleted; else - it will remain untouched
* @throws \BadMethodCallException
*/
public function saveRelations(array $relationsToSave = [], bool $deleteNotListedRelatedRecords = false);

Expand All @@ -266,8 +250,7 @@ public function saveRelations(array $relationsToSave = [], bool $deleteNotListed
* Note: this Record must exist in DB
* @param bool $resetAllValuesAfterDelete - true: will reset Record (default) | false: only primary key value will be reset
* @param bool $deleteFiles - true: delete all attached files | false: do not delete attached files
* @return static|RecordInterface
* @throws \BadMethodCallException
* @return static
*/
public function delete(bool $resetAllValuesAfterDelete = true, bool $deleteFiles = true);

Expand Down Expand Up @@ -306,10 +289,6 @@ public function toArrayWithoutFiles(
* @param bool $ignoreColumnsThatCannotBeSetManually - true: if column does not exist in DB - its value will not be returned
* @param bool $nullifyDbExprValues - true: if default value is DbExpr - replace it by null
* @return array
* @throws OrmException
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \BadMethodCallException
*/
public function getDefaults(array $columns = [], bool $ignoreColumnsThatCannotBeSetManually = true, bool $nullifyDbExprValues = true): array;

Expand Down

0 comments on commit 618c1eb

Please sign in to comment.