Merge pull request #6564 from Quadra-Digital/dbfield-setter-improvements

ENHANCEMENT: DBField 'setter' methods return instance of themselves
This commit is contained in:
Daniel Hensby 2017-01-28 09:23:39 +00:00 committed by GitHub
commit 77bfea1197
3 changed files with 22 additions and 9 deletions

View File

@ -33,6 +33,7 @@ class DBDate extends DBField
* @config * @config
* @see DBDateTime::nice_format * @see DBDateTime::nice_format
* @see DBTime::nice_format * @see DBTime::nice_format
* @return $this
*/ */
private static $nice_format = 'd/m/Y'; private static $nice_format = 'd/m/Y';
@ -42,18 +43,19 @@ class DBDate extends DBField
// don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be // don't try to evaluate empty values with strtotime() below, as it returns "1970-01-01" when it should be
// saved as NULL in database // saved as NULL in database
$this->value = null; $this->value = null;
return; return $this;
} }
// @todo This needs tidy up (what if you only specify a month and a year, for example?) // @todo This needs tidy up (what if you only specify a month and a year, for example?)
if (is_array($value)) { if (is_array($value)) {
if (!empty($value['Day']) && !empty($value['Month']) && !empty($value['Year'])) { if (!empty($value['Day']) && !empty($value['Month']) && !empty($value['Year'])) {
$this->value = $value['Year'] . '-' . $value['Month'] . '-' . $value['Day']; $this->value = $value['Year'] . '-' . $value['Month'] . '-' . $value['Day'];
return;
} else {
// return nothing (so checks below don't fail on an empty array)
return null;
} }
/*
* return $this whether successfully set values from array based
* input or not (so checks below don't fail on an empty array)
*/
return $this;
} }
// Default to NZ date format - strtotime expects a US date // Default to NZ date format - strtotime expects a US date
@ -67,12 +69,11 @@ class DBDate extends DBField
try { try {
$date = new DateTime($value); $date = new DateTime($value);
$this->value = $date->format('Y-m-d'); $this->value = $date->format('Y-m-d');
return;
} catch (Exception $e) { } catch (Exception $e) {
$this->value = null; $this->value = null;
return;
} }
} }
return $this;
} }
/** /**

View File

@ -148,7 +148,7 @@ abstract class DBField extends ViewableData
* *
* @param string $name * @param string $name
* *
* @return DBField * @return $this
*/ */
public function setName($name) public function setName($name)
{ {
@ -196,10 +196,12 @@ abstract class DBField extends ViewableData
* @param bool $markChanged Indicate wether this field should be marked changed. * @param bool $markChanged Indicate wether this field should be marked changed.
* Set to FALSE if you are initializing this field after construction, rather * Set to FALSE if you are initializing this field after construction, rather
* than setting a new value. * than setting a new value.
* @return $this
*/ */
public function setValue($value, $record = null, $markChanged = true) public function setValue($value, $record = null, $markChanged = true)
{ {
$this->value = $value; $this->value = $value;
return $this;
} }
/** /**
@ -446,7 +448,9 @@ abstract class DBField extends ViewableData
{ {
$fieldName = $this->name; $fieldName = $this->name;
if (empty($fieldName)) { if (empty($fieldName)) {
throw new \BadMethodCallException("DBField::saveInto() Called on a nameless '" . get_class($this) . "' object"); throw new \BadMethodCallException(
"DBField::saveInto() Called on a nameless '" . get_class($this) . "' object"
);
} }
$dataObject->$fieldName = $this->value; $dataObject->$fieldName = $this->value;
} }

View File

@ -103,10 +103,12 @@ class DBMoney extends DBComposite
/** /**
* @param string $currency * @param string $currency
* @param bool $markChanged * @param bool $markChanged
* @return $this
*/ */
public function setCurrency($currency, $markChanged = true) public function setCurrency($currency, $markChanged = true)
{ {
$this->setField('Currency', $currency, $markChanged); $this->setField('Currency', $currency, $markChanged);
return $this;
} }
/** /**
@ -120,10 +122,12 @@ class DBMoney extends DBComposite
/** /**
* @param float $amount * @param float $amount
* @param bool $markChanged * @param bool $markChanged
* @return $this
*/ */
public function setAmount($amount, $markChanged = true) public function setAmount($amount, $markChanged = true)
{ {
$this->setField('Amount', (float)$amount, $markChanged); $this->setField('Amount', (float)$amount, $markChanged);
return $this;
} }
/** /**
@ -145,11 +149,13 @@ class DBMoney extends DBComposite
/** /**
* @param string $locale * @param string $locale
* @return $this
*/ */
public function setLocale($locale) public function setLocale($locale)
{ {
$this->locale = $locale; $this->locale = $locale;
$this->currencyLib->setLocale($locale); $this->currencyLib->setLocale($locale);
return $this;
} }
/** /**
@ -214,10 +220,12 @@ class DBMoney extends DBComposite
/** /**
* @param array $arr * @param array $arr
* @return $this
*/ */
public function setAllowedCurrencies($arr) public function setAllowedCurrencies($arr)
{ {
$this->allowedCurrencies = $arr; $this->allowedCurrencies = $arr;
return $this;
} }
/** /**