Skip to content

Commit

Permalink
Simplifying Comparisson class and make it slightly more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jan 17, 2013
1 parent 743cba4 commit a192591
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 15 additions & 13 deletions lib/Cake/Model/Datasource/Database/Expression/Comparisson.php
Expand Up @@ -15,6 +15,7 @@
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

namespace Cake\Model\Datasource\Database\Expression;

use Cake\Model\Datasource\Database\Expression;
Expand All @@ -27,8 +28,19 @@ class Comparisson extends QueryExpression {

protected $_type;

public function __construct(array $condition, $types = [], $conjuntion = '=') {
parent::__construct($condition, $types, $conjuntion);
public function __construct($field, $value, $type, $conjuntion) {
$this->_field = $field;
$this->_value = $value;
$this->type($conjuntion);

if (is_string($type)) {
$this->_type = $type;
}
if (is_string($field) && isset($types[$this->_field])) {
$this->_type = current($types);
}

$this->_conditions[$field] = $value;
}

public function field($field) {
Expand All @@ -51,7 +63,7 @@ public function sql() {
$value = $this->_value;
$template = '%s %s (%s)';
if (!($this->_value instanceof Expression)) {
$value = $this->_bindValue($this->_field,$value, $this->_type);
$value = $this->_bindValue($this->_field, $value, $this->_type);
$template = '%s %s %s';
}

Expand All @@ -62,14 +74,4 @@ public function count() {
return 1;
}

protected function _addConditions(array $condition, array $types) {
$this->_conditions[] = current($condition);
$this->_field = key($condition);
$this->_value = current($condition);

if (isset($types[$this->_field])) {
$this->_type = current($types);
}
}

}
Expand Up @@ -258,7 +258,7 @@ protected function _parseCondition($field, $value, $types) {
}

if ($value instanceof Expression) {
return new Comparisson([$expression => $value], [$field => $type], $operator);
return new Comparisson($expression, $value, $type, $operator);
}

return sprintf($template, $expression, $operator, $this->_bindValue($field, $value, $type));
Expand Down

0 comments on commit a192591

Please sign in to comment.