API CHANGE: DBField::hasValue() conflicts with ViewableData::hasValue(), use DBField::exists() instead.

This commit is contained in:
Andrew O'Neil 2012-04-11 14:48:06 +12:00
parent 501c8f3f31
commit bdb312c665
10 changed files with 31 additions and 37 deletions

View File

@ -596,7 +596,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
if(!array_key_exists($k, $customFields)) continue; if(!array_key_exists($k, $customFields)) continue;
$dbObj = ($v instanceof DBField) ? $v : $this->dbObject($k); $dbObj = ($v instanceof DBField) ? $v : $this->dbObject($k);
$isEmpty = ($isEmpty && !$dbObj->hasValue()); $isEmpty = ($isEmpty && !$dbObj->exists());
} }
} }
return $isEmpty; return $isEmpty;
@ -3346,7 +3346,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
function hasValue($field, $arguments = null, $cache = true) { function hasValue($field, $arguments = null, $cache = true) {
$obj = $this->dbObject($field); $obj = $this->dbObject($field);
if($obj) { if($obj) {
return $obj->hasValue(); return $obj->exists();
} else { } else {
return parent::hasValue($field, $arguments, $cache); return parent::hasValue($field, $arguments, $cache);
} }

View File

@ -177,6 +177,6 @@ interface CompositeDBField {
* *
* @return boolean * @return boolean
*/ */
function hasValue(); function exists();
} }

View File

@ -120,6 +120,7 @@ abstract class DBField extends ViewableData {
$this->value = $value; $this->value = $value;
} }
/** /**
* Determines if the field has a value which * Determines if the field has a value which
* is not considered to be 'null' in * is not considered to be 'null' in
@ -127,15 +128,8 @@ abstract class DBField extends ViewableData {
* *
* @return boolean * @return boolean
*/ */
function hasValue() {
return ($this->value);
}
/**
* @return bool
*/
public function exists() { public function exists() {
return $this->hasValue(); return ($this->value);
} }
/** /**
@ -167,7 +161,7 @@ abstract class DBField extends ViewableData {
* @param array $manipulation * @param array $manipulation
*/ */
function writeToManipulation(&$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();
} }
/** /**

View File

@ -128,8 +128,8 @@ class HTMLText extends Text {
return ShortcodeParser::get_active()->parse($this->value); return ShortcodeParser::get_active()->parse($this->value);
} }
public function hasValue() { public function exists() {
return parent::hasValue() && $this->value != '<p></p>'; return parent::exists() && $this->value != '<p></p>';
} }
public function scaffoldFormField($title = null, $params = null) { public function scaffoldFormField($title = null, $params = null) {

View File

@ -14,8 +14,8 @@ class HTMLVarchar extends Varchar {
return ShortcodeParser::get_active()->parse($this->value); return ShortcodeParser::get_active()->parse($this->value);
} }
public function hasValue() { public function exists() {
return parent::hasValue() && $this->value != '<p></p>'; return parent::exists() && $this->value != '<p></p>';
} }
public function scaffoldFormField($title = null, $params = null) { public function scaffoldFormField($title = null, $params = null) {

View File

@ -190,7 +190,7 @@ class Money extends DBField implements CompositeDBField {
/** /**
* @return boolean * @return boolean
*/ */
function hasValue() { function exists() {
return ($this->getCurrency() && is_numeric($this->getAmount())); return ($this->getCurrency() && is_numeric($this->getAmount()));
} }

View File

@ -64,9 +64,9 @@ abstract class StringField extends DBField {
/** /**
* (non-PHPdoc) * (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 === ''); return ($this->value || $this->value == '0') || ( !$this->nullifyEmpty && $this->value === '');
} }

View File

@ -56,7 +56,7 @@ class Varchar extends StringField {
* Return the first letter of the string followed by a . * Return the first letter of the string followed by a .
*/ */
function Initial() { function Initial() {
if($this->hasValue()) return $this->value[0] . '.'; if($this->exists()) return $this->value[0] . '.';
} }
/** /**

View File

@ -157,42 +157,42 @@ class DBFieldTest extends SapphireTest {
$this->assertEquals("00:00:00", $time->getValue()); $this->assertEquals("00:00:00", $time->getValue());
} }
function testHasValue() { function testExists() {
$varcharField = new Varchar("testfield"); $varcharField = new Varchar("testfield");
$this->assertTrue($varcharField->getNullifyEmpty()); $this->assertTrue($varcharField->getNullifyEmpty());
$varcharField->setValue('abc'); $varcharField->setValue('abc');
$this->assertTrue($varcharField->hasValue()); $this->assertTrue($varcharField->exists());
$varcharField->setValue(''); $varcharField->setValue('');
$this->assertFalse($varcharField->hasValue()); $this->assertFalse($varcharField->exists());
$varcharField->setValue(null); $varcharField->setValue(null);
$this->assertFalse($varcharField->hasValue()); $this->assertFalse($varcharField->exists());
$varcharField = new Varchar("testfield", 50, array('nullifyEmpty'=>false)); $varcharField = new Varchar("testfield", 50, array('nullifyEmpty'=>false));
$this->assertFalse($varcharField->getNullifyEmpty()); $this->assertFalse($varcharField->getNullifyEmpty());
$varcharField->setValue('abc'); $varcharField->setValue('abc');
$this->assertTrue($varcharField->hasValue()); $this->assertTrue($varcharField->exists());
$varcharField->setValue(''); $varcharField->setValue('');
$this->assertTrue($varcharField->hasValue()); $this->assertTrue($varcharField->exists());
$varcharField->setValue(null); $varcharField->setValue(null);
$this->assertFalse($varcharField->hasValue()); $this->assertFalse($varcharField->exists());
$textField = new Text("testfield"); $textField = new Text("testfield");
$this->assertTrue($textField->getNullifyEmpty()); $this->assertTrue($textField->getNullifyEmpty());
$textField->setValue('abc'); $textField->setValue('abc');
$this->assertTrue($textField->hasValue()); $this->assertTrue($textField->exists());
$textField->setValue(''); $textField->setValue('');
$this->assertFalse($textField->hasValue()); $this->assertFalse($textField->exists());
$textField->setValue(null); $textField->setValue(null);
$this->assertFalse($textField->hasValue()); $this->assertFalse($textField->exists());
$textField = new Text("testfield", array('nullifyEmpty'=>false)); $textField = new Text("testfield", array('nullifyEmpty'=>false));
$this->assertFalse($textField->getNullifyEmpty()); $this->assertFalse($textField->getNullifyEmpty());
$textField->setValue('abc'); $textField->setValue('abc');
$this->assertTrue($textField->hasValue()); $this->assertTrue($textField->exists());
$textField->setValue(''); $textField->setValue('');
$this->assertTrue($textField->hasValue()); $this->assertTrue($textField->exists());
$textField->setValue(null); $textField->setValue(null);
$this->assertFalse($textField->hasValue()); $this->assertFalse($textField->exists());
} }
function testStringFieldsWithMultibyteData() { function testStringFieldsWithMultibyteData() {

View File

@ -207,23 +207,23 @@ class MoneyTest extends SapphireTest {
); );
} }
function testHasValue() { function testExists() {
$m1 = new Money(); $m1 = new Money();
$this->assertFalse($m1->hasValue()); $this->assertFalse($m1->exists());
$m2 = new Money(); $m2 = new Money();
$m2->setValue(array( $m2->setValue(array(
'Currency' => 'EUR', 'Currency' => 'EUR',
'Amount' => 3.44 'Amount' => 3.44
)); ));
$this->assertTrue($m2->hasValue()); $this->assertTrue($m2->exists());
$m3 = new Money(); $m3 = new Money();
$m3->setValue(array( $m3->setValue(array(
'Currency' => 'EUR', 'Currency' => 'EUR',
'Amount' => 0 'Amount' => 0
)); ));
$this->assertTrue($m3->hasValue()); $this->assertTrue($m3->exists());
} }
function testLoadIntoDataObject() { function testLoadIntoDataObject() {