Skip to content

Commit

Permalink
Added trait to help with checking allowed values
Browse files Browse the repository at this point in the history
  • Loading branch information
rikbruil committed Mar 17, 2017
1 parent 5404c39 commit ebaa30f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 48 deletions.
36 changes: 36 additions & 0 deletions src/Helper/TypeCheckerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rb\Specification\Doctrine\Helper;

use Rb\Specification\Doctrine\Exception\InvalidArgumentException;

trait TypeCheckerTrait
{
/**
* @var string[]
*/
protected $validTypes;

/**
* @var string
*/
private $type;

/**
* @param string $type
*
* @throws InvalidArgumentException
*/
public function setType($type)
{
if (! in_array($type, $this->validTypes, true)) {
throw new InvalidArgumentException(sprintf(
'"%s" is not a valid type! Valid types: %s',
$type,
implode(', ', $this->validTypes)
));
}

$this->type = $type;
}
}
27 changes: 4 additions & 23 deletions src/Query/GroupBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@

use Doctrine\ORM\QueryBuilder;
use Rb\Specification\Doctrine\AbstractSpecification;
use Rb\Specification\Doctrine\Exception\InvalidArgumentException;
use Rb\Specification\Doctrine\Helper\TypeCheckerTrait;

/**
* @author Kyle Tucker <kyleatucker@gmail.com>
*/
class GroupBy extends AbstractSpecification
{
use TypeCheckerTrait;

const GROUP_BY = 'groupBy';
const ADD_GROUP_BY = 'addGroupBy';

/** @var string[] */
protected static $types = [self::GROUP_BY, self::ADD_GROUP_BY];

/** @var string */
protected $type;
protected $validTypes = [self::GROUP_BY, self::ADD_GROUP_BY];

/**
* Constructor.
Expand All @@ -45,22 +44,4 @@ public function modify(QueryBuilder $queryBuilder, $dqlAlias)
]
);
}

/**
* @param string $type
*
* @throws InvalidArgumentException
*/
public function setType($type)
{
if (! in_array($type, self::$types, true)) {
throw new InvalidArgumentException(sprintf(
'"%s" is not a valid type! Valid types: %s',
$type,
implode(', ', self::$types)
));
}

$this->type = $type;
}
}
29 changes: 4 additions & 25 deletions src/Query/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,26 @@
namespace Rb\Specification\Doctrine\Query;

use Doctrine\ORM\QueryBuilder;
use Rb\Specification\Doctrine\Exception\InvalidArgumentException;
use Rb\Specification\Doctrine\Helper\TypeCheckerTrait;
use Rb\Specification\Doctrine\SpecificationInterface;

/**
* Select will modify the query-builder so you can specify SELECT-statements.
*/
class Select implements SpecificationInterface
{
use TypeCheckerTrait;

const SELECT = 'select';
const ADD_SELECT = 'addSelect';

protected static $types = [self::SELECT, self::ADD_SELECT];
protected $validTypes = [self::SELECT, self::ADD_SELECT];

/**
* @var string|array
*/
protected $select;

/**
* @var string
*/
protected $type;

/**
* @param string|array $select
* @param string $type
Expand All @@ -44,24 +41,6 @@ public function modify(QueryBuilder $queryBuilder, $dqlAlias)
call_user_func_array([$queryBuilder, $this->type], [$this->select]);
}

/**
* @param string $type
*
* @throws InvalidArgumentException
*/
public function setType($type)
{
if (! in_array($type, self::$types, true)) {
throw new InvalidArgumentException(sprintf(
'"%s" is not a valid type! Valid types: %s',
$type,
implode(', ', self::$types)
));
}

$this->type = $type;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit ebaa30f

Please sign in to comment.