Skip to content

Commit

Permalink
API CHANGE: DBField::hasValue() conflicts with ViewableData::hasValue…
Browse files Browse the repository at this point in the history
…(), use DBField::exists() instead.
  • Loading branch information
andrewandante committed Apr 11, 2012
1 parent 501c8f3 commit bdb312c
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 37 deletions.
4 changes: 2 additions & 2 deletions model/DataObject.php
Expand Up @@ -596,7 +596,7 @@ public function isEmpty(){
if(!array_key_exists($k, $customFields)) continue;

$dbObj = ($v instanceof DBField) ? $v : $this->dbObject($k);
$isEmpty = ($isEmpty && !$dbObj->hasValue());
$isEmpty = ($isEmpty && !$dbObj->exists());
}
}
return $isEmpty;
Expand Down Expand Up @@ -3346,7 +3346,7 @@ public function provideI18nEntities() {
function hasValue($field, $arguments = null, $cache = true) {
$obj = $this->dbObject($field);
if($obj) {
return $obj->hasValue();
return $obj->exists();
} else {
return parent::hasValue($field, $arguments, $cache);
}
Expand Down
2 changes: 1 addition & 1 deletion model/fieldtypes/CompositeDBField.php
Expand Up @@ -177,6 +177,6 @@ function isChanged();
*
* @return boolean
*/
function hasValue();
function exists();

}
12 changes: 3 additions & 9 deletions model/fieldtypes/DBField.php
Expand Up @@ -120,22 +120,16 @@ function setValue($value, $record = null) {
$this->value = $value;
}


/**
* Determines if the field has a value which
* is not considered to be 'null' in
* a database context.
*
* @return boolean
*/
function hasValue() {
return ($this->value);
}

/**
* @return bool
*/
public function exists() {
return $this->hasValue();
return ($this->value);
}

/**
Expand Down Expand Up @@ -167,7 +161,7 @@ function prepValueForDB($value) {
* @param array $manipulation
*/
function writeToManipulation(&$manipulation) {
$manipulation['fields'][$this->name] = $this->hasValue() ? $this->prepValueForDB($this->value) : $this->nullValue();
$manipulation['fields'][$this->name] = $this->exists() ? $this->prepValueForDB($this->value) : $this->nullValue();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions model/fieldtypes/HTMLText.php
Expand Up @@ -128,8 +128,8 @@ public function forTemplate() {
return ShortcodeParser::get_active()->parse($this->value);
}

public function hasValue() {
return parent::hasValue() && $this->value != '<p></p>';
public function exists() {
return parent::exists() && $this->value != '<p></p>';
}

public function scaffoldFormField($title = null, $params = null) {
Expand Down
4 changes: 2 additions & 2 deletions model/fieldtypes/HTMLVarchar.php
Expand Up @@ -14,8 +14,8 @@ public function forTemplate() {
return ShortcodeParser::get_active()->parse($this->value);
}

public function hasValue() {
return parent::hasValue() && $this->value != '<p></p>';
public function exists() {
return parent::exists() && $this->value != '<p></p>';
}

public function scaffoldFormField($title = null, $params = null) {
Expand Down
2 changes: 1 addition & 1 deletion model/fieldtypes/Money.php
Expand Up @@ -190,7 +190,7 @@ function setAmount($amount, $markChanged = true) {
/**
* @return boolean
*/
function hasValue() {
function exists() {
return ($this->getCurrency() && is_numeric($this->getAmount()));
}

Expand Down
4 changes: 2 additions & 2 deletions model/fieldtypes/StringField.php
Expand Up @@ -64,9 +64,9 @@ function getNullifyEmpty() {

/**
* (non-PHPdoc)
* @see core/model/fieldtypes/DBField#hasValue()
* @see core/model/fieldtypes/DBField#exists()
*/
function hasValue() {
function exists() {
return ($this->value || $this->value == '0') || ( !$this->nullifyEmpty && $this->value === '');
}

Expand Down
2 changes: 1 addition & 1 deletion model/fieldtypes/Varchar.php
Expand Up @@ -56,7 +56,7 @@ function requireField() {
* Return the first letter of the string followed by a .
*/
function Initial() {
if($this->hasValue()) return $this->value[0] . '.';
if($this->exists()) return $this->value[0] . '.';
}

/**
Expand Down
26 changes: 13 additions & 13 deletions tests/model/DBFieldTest.php
Expand Up @@ -157,42 +157,42 @@ function testPrepValueForDB() {
$this->assertEquals("00:00:00", $time->getValue());
}

function testHasValue() {
function testExists() {
$varcharField = new Varchar("testfield");
$this->assertTrue($varcharField->getNullifyEmpty());
$varcharField->setValue('abc');
$this->assertTrue($varcharField->hasValue());
$this->assertTrue($varcharField->exists());
$varcharField->setValue('');
$this->assertFalse($varcharField->hasValue());
$this->assertFalse($varcharField->exists());
$varcharField->setValue(null);
$this->assertFalse($varcharField->hasValue());
$this->assertFalse($varcharField->exists());

$varcharField = new Varchar("testfield", 50, array('nullifyEmpty'=>false));
$this->assertFalse($varcharField->getNullifyEmpty());
$varcharField->setValue('abc');
$this->assertTrue($varcharField->hasValue());
$this->assertTrue($varcharField->exists());
$varcharField->setValue('');
$this->assertTrue($varcharField->hasValue());
$this->assertTrue($varcharField->exists());
$varcharField->setValue(null);
$this->assertFalse($varcharField->hasValue());
$this->assertFalse($varcharField->exists());

$textField = new Text("testfield");
$this->assertTrue($textField->getNullifyEmpty());
$textField->setValue('abc');
$this->assertTrue($textField->hasValue());
$this->assertTrue($textField->exists());
$textField->setValue('');
$this->assertFalse($textField->hasValue());
$this->assertFalse($textField->exists());
$textField->setValue(null);
$this->assertFalse($textField->hasValue());
$this->assertFalse($textField->exists());

$textField = new Text("testfield", array('nullifyEmpty'=>false));
$this->assertFalse($textField->getNullifyEmpty());
$textField->setValue('abc');
$this->assertTrue($textField->hasValue());
$this->assertTrue($textField->exists());
$textField->setValue('');
$this->assertTrue($textField->hasValue());
$this->assertTrue($textField->exists());
$textField->setValue(null);
$this->assertFalse($textField->hasValue());
$this->assertFalse($textField->exists());
}

function testStringFieldsWithMultibyteData() {
Expand Down
8 changes: 4 additions & 4 deletions tests/model/MoneyTest.php
Expand Up @@ -207,23 +207,23 @@ function testSetValueAsMoney() {
);
}

function testHasValue() {
function testExists() {
$m1 = new Money();
$this->assertFalse($m1->hasValue());
$this->assertFalse($m1->exists());

$m2 = new Money();
$m2->setValue(array(
'Currency' => 'EUR',
'Amount' => 3.44
));
$this->assertTrue($m2->hasValue());
$this->assertTrue($m2->exists());

$m3 = new Money();
$m3->setValue(array(
'Currency' => 'EUR',
'Amount' => 0
));
$this->assertTrue($m3->hasValue());
$this->assertTrue($m3->exists());
}

function testLoadIntoDataObject() {
Expand Down

0 comments on commit bdb312c

Please sign in to comment.