Skip to content

Commit

Permalink
Removed Generic Throw Error
Browse files Browse the repository at this point in the history
Update PHPDoc comments
  • Loading branch information
byjg committed Mar 11, 2018
1 parent 2c439f0 commit ba768db
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 29 deletions.
8 changes: 8 additions & 0 deletions src/InvalidArgumentException.php
@@ -0,0 +1,8 @@
<?php

namespace ByJG\MicroOrm;

class InvalidArgumentException extends \Exception
{

}
3 changes: 2 additions & 1 deletion src/Mapper.php
Expand Up @@ -68,6 +68,7 @@ public function prepareField($field)
* @param \Closure|null|bool $updateMask
* @param \Closure $selectMask
* @return $this
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
public function addFieldMap($property, $fieldName, $updateMask = false, \Closure $selectMask = null)
{
Expand All @@ -80,7 +81,7 @@ public function addFieldMap($property, $fieldName, $updateMask = false, \Closure
}

if (!is_null($updateMask) && !($updateMask instanceof \Closure)) {
throw new \InvalidArgumentException('UpdateMask must be a \Closure or NULL');
throw new InvalidArgumentException('UpdateMask must be a \Closure or NULL');
}

$this->fieldMap[$this->prepareField($property)] = [
Expand Down
29 changes: 23 additions & 6 deletions src/Query.php
Expand Up @@ -33,9 +33,10 @@ public static function getInstance()
/**
* Example:
* $query->fields(['name', 'price']);
*
*
* @param array $fields
* @return $this
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function fields(array $fields)
{
Expand All @@ -50,6 +51,10 @@ public function fields(array $fields)
return $this;
}

/**
* @param \ByJG\MicroOrm\Mapper $mapper
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
private function addFieldFromMapper(Mapper $mapper)
{
$entityClass = $mapper->getEntity();
Expand Down Expand Up @@ -162,20 +167,31 @@ public function forUpdate()
return $this;
}

/**
* @param $start
* @param $end
* @return $this
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
public function limit($start, $end)
{
if (!is_null($this->top)) {
throw new \InvalidArgumentException('You cannot mix TOP and LIMIT');
throw new InvalidArgumentException('You cannot mix TOP and LIMIT');
}
$this->limitStart = $start;
$this->limitEnd = $end;
return $this;
}

/**
* @param $top
* @return $this
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
public function top($top)
{
if (!is_null($this->limitStart)) {
throw new \InvalidArgumentException('You cannot mix TOP and LIMIT');
throw new InvalidArgumentException('You cannot mix TOP and LIMIT');
}
$this->top = $top;
return $this;
Expand Down Expand Up @@ -219,6 +235,7 @@ protected function getWhere()
/**
* @param \ByJG\AnyDataset\DbDriverInterface|null $dbDriver
* @return array
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
public function build(DbDriverInterface $dbDriver = null)
{
Expand All @@ -243,21 +260,21 @@ public function build(DbDriverInterface $dbDriver = null)

if (!empty($this->forUpdate)) {
if (is_null($dbDriver)) {
throw new \InvalidArgumentException('To get FOR UPDATE working you have to pass the DbDriver');
throw new InvalidArgumentException('To get FOR UPDATE working you have to pass the DbDriver');
}
$sql = $dbDriver->getDbHelper()->forUpdate($sql);
}

if (!empty($this->top)) {
if (is_null($dbDriver)) {
throw new \InvalidArgumentException('To get Limit and Top working you have to pass the DbDriver');
throw new InvalidArgumentException('To get Limit and Top working you have to pass the DbDriver');
}
$sql = $dbDriver->getDbHelper()->top($sql, $this->top);
}

if (!empty($this->limitStart) || ($this->limitStart === 0)) {
if (is_null($dbDriver)) {
throw new \InvalidArgumentException('To get Limit and Top working you have to pass the DbDriver');
throw new InvalidArgumentException('To get Limit and Top working you have to pass the DbDriver');
}
$sql = $dbDriver->getDbHelper()->limit($sql, $this->limitStart, $this->limitEnd);
}
Expand Down
29 changes: 26 additions & 3 deletions src/Repository.php
Expand Up @@ -55,6 +55,8 @@ protected function getDbDriver()
/**
* @param array|string $id
* @return mixed|null
* @throws \ByJG\MicroOrm\InvalidArgumentException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function get($id)
{
Expand All @@ -70,6 +72,7 @@ public function get($id)
/**
* @param array $id
* @return mixed|null
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
public function delete($id)
{
Expand All @@ -82,8 +85,9 @@ public function delete($id)
}

/**
* @param $updatable
* @param \ByJG\MicroOrm\Updatable $updatable
* @return bool
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
public function deleteByQuery(Updatable $updatable)
{
Expand All @@ -100,6 +104,8 @@ public function deleteByQuery(Updatable $updatable)
* @param array $params
* @param bool $forUpdate
* @return array
* @throws \ByJG\MicroOrm\InvalidArgumentException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function getByFilter($filter, array $params, $forUpdate = false)
{
Expand All @@ -118,6 +124,8 @@ public function getByFilter($filter, array $params, $forUpdate = false)
* @param Query $query
* @param Mapper[] $mapper
* @return array
* @throws \ByJG\MicroOrm\InvalidArgumentException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function getByQuery(Query $query, array $mapper = [])
{
Expand Down Expand Up @@ -161,6 +169,8 @@ public function getByQuery(Query $query, array $mapper = [])

/**
* @param mixed $instance
* @throws \ByJG\MicroOrm\InvalidArgumentException
* @throws \ByJG\Serializer\Exception\InvalidArgumentException
*/
public function save($instance)
{
Expand Down Expand Up @@ -203,7 +213,7 @@ public function save($instance)
* @param \ByJG\MicroOrm\Updatable $updatable
* @param array $params
* @return int
* @throws \Exception
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
protected function insert(Updatable $updatable, array $params)
{
Expand All @@ -215,13 +225,26 @@ protected function insert(Updatable $updatable, array $params)
}
}

/**
* @param \ByJG\MicroOrm\Updatable $updatable
* @param array $params
* @return int
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
protected function insertWithAutoinc(Updatable $updatable, array $params)
{
$sql = $updatable->buildInsert($params, $this->getDbDriver()->getDbHelper());
$dbFunctions = $this->getDbDriver()->getDbHelper();
return $dbFunctions->executeAndGetInsertedId($this->getDbDriver(), $sql, $params);
}

/**
* @param \ByJG\MicroOrm\Updatable $updatable
* @param array $params
* @param $keyGen
* @return mixed
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
protected function insertWithKeyGen(Updatable $updatable, array $params, $keyGen)
{
$params[$this->mapper->getPrimaryKey()] = $keyGen;
Expand All @@ -233,7 +256,7 @@ protected function insertWithKeyGen(Updatable $updatable, array $params, $keyGen
/**
* @param \ByJG\MicroOrm\Updatable $updatable
* @param array $params
* @throws \Exception
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
protected function update(Updatable $updatable, array $params)
{
Expand Down
28 changes: 11 additions & 17 deletions src/Updatable.php
@@ -1,14 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: jg
* Date: 21/06/16
* Time: 12:01
*/

namespace ByJG\MicroOrm;


use ByJG\AnyDataset\DbFunctionsInterface;

class Updatable
Expand Down Expand Up @@ -90,16 +83,17 @@ protected function getWhere()
return [ implode(' AND ', $where), $params ];
}


/**
* @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper
* @param $params
* @return string
* @throws \Exception
* @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper
* @return null|string|string[]
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null)
{
if (empty($this->fields)) {
throw new \Exception('You must specifiy the fields for insert');
throw new InvalidArgumentException('You must specifiy the fields for insert');
}

$fields = $this->fields;
Expand All @@ -124,15 +118,15 @@ public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null)
}

/**
* @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper
* @param $params
* @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper
* @return array
* @throws \Exception
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
{
if (empty($this->fields)) {
throw new \InvalidArgumentException('You must specifiy the fields for insert');
throw new InvalidArgumentException('You must specifiy the fields for insert');
}

$fields = [];
Expand All @@ -146,7 +140,7 @@ public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)

$where = $this->getWhere();
if (is_null($where)) {
throw new \InvalidArgumentException('You must specifiy a where clause');
throw new InvalidArgumentException('You must specifiy a where clause');
}

$tableName = $this->table;
Expand All @@ -168,13 +162,13 @@ public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
/**
* @param $params
* @return array
* @throws \Exception
* @throws \ByJG\MicroOrm\InvalidArgumentException
*/
public function buildDelete(&$params)
{
$where = $this->getWhere();
if (is_null($where)) {
throw new \InvalidArgumentException('You must specifiy a where clause');
throw new InvalidArgumentException('You must specifiy a where clause');
}

$sql = 'DELETE FROM ' . $this->table
Expand Down
4 changes: 2 additions & 2 deletions tests/UpdatableTest.php
Expand Up @@ -94,7 +94,7 @@ public function testUpdate()
}

/**
* @expectedException \InvalidArgumentException
* @expectedException \ByJG\MicroOrm\InvalidArgumentException
*/
public function testUpdateError()
{
Expand Down Expand Up @@ -128,7 +128,7 @@ public function testDelete()
}

/**
* @expectedException \InvalidArgumentException
* @expectedException \ByJG\MicroOrm\InvalidArgumentException
*/
public function testDeleteError()
{
Expand Down

0 comments on commit ba768db

Please sign in to comment.