Skip to content

Commit

Permalink
Added conditions, which is part of the way towards issue #9
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbyoung committed Jul 7, 2016
1 parent 47c7f87 commit 744b578
Show file tree
Hide file tree
Showing 14 changed files with 546 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/Opulence/Cryptography/Encryption/Encrypter.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private function createHmac(
private function deriveKeys(string $cipher, string $keySalt) : DerivedKeys
{
// Extract the number of bytes from the cipher
$keyByteLength = $this->getCipherByteLength($cipher);
$keyByteLength = $this->getKeyByteLengthForCipher($cipher);

if ($this->secret->getType() === SecretTypes::KEY) {
return $this->keyDeriver->deriveKeysFromKey($this->secret->getValue(), $keySalt, $keyByteLength);
Expand All @@ -196,7 +196,7 @@ private function deriveKeys(string $cipher, string $keySalt) : DerivedKeys
* @param string $cipher The cipher whose bytes we want
* @return int The number of bytes
*/
private function getCipherByteLength(string $cipher) : int
private function getKeyByteLengthForCipher(string $cipher) : int
{
return (int)\mb_substr($cipher, 4, 3, "8bit") / 8;
}
Expand Down Expand Up @@ -260,8 +260,8 @@ private function setCipher(string $cipher)
private function validateSecret(string $cipher)
{
if ($this->secret->getType() === SecretTypes::KEY) {
if (\mb_strlen($this->secret->getValue(), "8bit") < $this->getCipherByteLength($cipher)) {
throw new EncryptionException("Key must be at least {$this->getCipherByteLength($cipher)} bytes long");
if (\mb_strlen($this->secret->getValue(), "8bit") < $this->getKeyByteLengthForCipher($cipher)) {
throw new EncryptionException("Key must be at least {$this->getKeyByteLengthForCipher($cipher)} bytes long");
}
} elseif (\mb_strlen($this->secret->getValue(), "8bit") === 0) {
throw new EncryptionException("Password cannot be empty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function getEncrypter() : IEncrypter
throw new RuntimeException("\"ENCRYPTION_KEY\" value not set in environment. Check that you have it set in an environment config file such as \".env.app.php\". Note: \".env.example.php\" is only a template for environment config files - it is not actually used.");
}

$decodedEncryptionKey = hex2bin($encodedEncryptionKey);
$decodedEncryptionKey = \hex2bin($encodedEncryptionKey);

if (\mb_strlen($decodedEncryptionKey, "8bit") < 32) {
throw new RuntimeException("The minimum length encryption key has been upgraded from 16 bytes to 32 bytes. Please re-run \"php apex encryption:generatekey\" to create a new, suitably-long encryption key.");
Expand Down
55 changes: 55 additions & 0 deletions src/Opulence/QueryBuilders/Conditions/BetweenCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2016 David Young
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md
*/
namespace Opulence\QueryBuilders\Conditions;

use PDO;

/**
* Defines the BETWEEN condition
*/
class BetweenCondition extends Condition
{
/** @var mixed The min value */
protected $min = "";
/** @var mixed The min value */
protected $max = "";
/** @var int The data type of the min/max */
protected $dataType = PDO::PARAM_STR;

/**
* @inheritdoc
* @param mixed $min The min value
* @param mixed $max The max value
* @param int $dataType The PDO data type for the min and max
*/
public function __construct(string $column, $min, $max, $dataType = PDO::PARAM_STR)
{
parent::__construct($column);

$this->min = $min;
$this->max = $max;
$this->dataType = $dataType;
}

/**
* @inheritdoc
*/
public function getParameters() : array
{
return [[$this->min, $this->dataType], [$this->max, $this->dataType]];
}

/**
* @inheritdoc
*/
public function getSql() : string
{
return "{$this->column} BETWEEN ? AND ?";
}
}
26 changes: 26 additions & 0 deletions src/Opulence/QueryBuilders/Conditions/Condition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2016 David Young
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md
*/
namespace Opulence\QueryBuilders\Conditions;

/**
* Defines the base condition
*/
abstract class Condition implements ICondition
{
/** @var string The column */
protected $column = "";

/**
* @param string $column The column
*/
public function __construct(string $column)
{
$this->column = $column;
}
}
72 changes: 72 additions & 0 deletions src/Opulence/QueryBuilders/Conditions/ConditionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2016 David Young
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md
*/
namespace Opulence\QueryBuilders\Conditions;

use InvalidArgumentException;
use PDO;

/**
* Defines the condition factory
*/
class ConditionFactory
{
/**
* Creates a new BETWEEN condition
*
* @param string $column The name of the column
* @param mixed $min The min value
* @param mixed $max The max value
* @param int $dataType The PDO data type for the min and max
* @return BetweenCondition The condition
*/
public function between(string $column, $min, $max, $dataType = PDO::PARAM_STR) : BetweenCondition
{
return new BetweenCondition($column, $min, $max, $dataType);
}

/**
* Creates a new IN condition
*
* @param string $column The name of the column
* @param array|string $parametersOrExpression Either the parameters or the sub-expression
* @return InCondition The condition
* @throws InvalidArgumentException Thrown if the parameters are not in the correct format
*/
public function in(string $column, $parametersOrExpression) : InCondition
{
return new InCondition($column, $parametersOrExpression);
}

/**
* Creates a new NOT BETWEEN condition
*
* @param string $column The name of the column
* @param mixed $min The min value
* @param mixed $max The max value
* @param int $dataType The PDO data type for the min and max
* @return NotBetweenCondition The condition
*/
public function notBetween(string $column, $min, $max, $dataType = PDO::PARAM_STR) : NotBetweenCondition
{
return new NotBetweenCondition($column, $min, $max, $dataType);
}

/**
* Creates a new NOT IN condition
*
* @param string $column The name of the column
* @param array|string $parametersOrExpression Either the parameters or the sub-expression
* @return NotInCondition The condition
* @throws InvalidArgumentException Thrown if the parameters are not in the correct format
*/
public function notIn(string $column, $parametersOrExpression) : NotInCondition
{
return new NotInCondition($column, $parametersOrExpression);
}
}
29 changes: 29 additions & 0 deletions src/Opulence/QueryBuilders/Conditions/ICondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2016 David Young
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md
*/
namespace Opulence\QueryBuilders\Conditions;

/**
* The condition
*/
interface ICondition
{
/**
* Gets the parameters bound by the condition
*
* @return array The list of parameters
*/
public function getParameters() : array;

/**
* Gets the SQL generated by the condition
*
* @return string The SQL
*/
public function getSql() : string;
}
70 changes: 70 additions & 0 deletions src/Opulence/QueryBuilders/Conditions/InCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2016 David Young
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md
*/
namespace Opulence\QueryBuilders\Conditions;

use InvalidArgumentException;

/**
* Defines the IN condition
*/
class InCondition extends Condition
{
/** @var string The sub-expression */
protected $expression = "";
/** @var array The list of parameters bound to the query */
protected $parameters = [];
/** @var bool True if we're using parameters, otherwise we're using a sub-expression */
protected $usingParameters = true;

/**
* @inheritdoc
* @param array|string $parametersOrExpression Either the parameters or the sub-expression
* @throws InvalidArgumentException Thrown if the parameters are not in the correct format
*/
public function __construct(string $column, $parametersOrExpression)
{
parent::__construct($column);

if (is_string($parametersOrExpression)) {
$this->usingParameters = false;
$this->expression = $parametersOrExpression;
} else if (is_array($parametersOrExpression)) {
$this->usingParameters = true;
$this->parameters = $parametersOrExpression;
} else {
throw new InvalidArgumentException("Must pass either parameters or sub-expression to IN condition");
}
}

/**
* @inheritdoc
*/
public function getParameters() : array
{
return $this->parameters;
}

/**
* @inheritdoc
*/
public function getSql() : string
{
$sql = "{$this->column} IN (";

if ($this->usingParameters) {
$sql .= implode(",", array_fill(0, count($this->parameters), "?"));
} else {
$sql .= $this->expression;
}

$sql .= ")";

return $sql;
}
}
23 changes: 23 additions & 0 deletions src/Opulence/QueryBuilders/Conditions/NotBetweenCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2016 David Young
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md
*/
namespace Opulence\QueryBuilders\Conditions;

/**
* Defines the NOT BETWEEN condition
*/
class NotBetweenCondition extends BetweenCondition
{
/**
* @inheritdoc
*/
public function getSql() : string
{
return "{$this->column} NOT BETWEEN ? AND ?";
}
}
33 changes: 33 additions & 0 deletions src/Opulence/QueryBuilders/Conditions/NotInCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2016 David Young
* @license https://github.com/opulencephp/Opulence/blob/master/LICENSE.md
*/
namespace Opulence\QueryBuilders\Conditions;

/**
* Defines the NOT IN condition
*/
class NotInCondition extends InCondition
{
/**
* @inheritdoc
*/
public function getSql() : string
{
$sql = "{$this->column} NOT IN (";

if ($this->usingParameters) {
$sql .= implode(",", array_fill(0, count($this->parameters), "?"));
} else {
$sql .= $this->expression;
}

$sql .= ")";

return $sql;
}
}
Loading

0 comments on commit 744b578

Please sign in to comment.