Completed
Push — 3.5 ( 1a9180...1bec8a )
by Daniel
24s
created

Float   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 10
c 0
b 0
f 0
wmc 12
lcom 2
cbo 3

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A requireField() 0 10 1
A Nice() 0 3 1
A Round() 0 3 1
A NiceRound() 0 3 1
A scaffoldFormField() 0 3 1
A nullValue() 0 3 1
A prepValueForDB() 0 9 4
1
<?php
2
/**
3
 * Represents a floating point field.
4
 *
5
 * @package framework
6
 * @subpackage model
7
 */
8
class Float extends DBField {
9
10
	public function __construct($name = null, $defaultVal = 0) {
11
		$this->defaultVal = is_float($defaultVal) ? $defaultVal : (float) 0;
12
13
		parent::__construct($name);
14
	}
15
16
	public function requireField() {
17
		$parts = Array(
18
			'datatype'=>'float',
19
			'null'=>'not null',
20
			'default'=>$this->defaultVal,
21
			'arrayValue'=>$this->arrayValue
22
		);
23
		$values = Array('type'=>'float', 'parts'=>$parts);
24
		DB::require_field($this->tableName, $this->name, $values);
25
	}
26
27
	/**
28
	 * Returns the number, with commas and decimal places as appropriate, eg “1,000.00”.
29
	 *
30
	 * @uses number_format()
31
	 */
32
	public function Nice() {
33
		return number_format($this->value, 2);
34
	}
35
36
	public function Round($precision = 3) {
37
		return round($this->value, $precision);
38
	}
39
40
	public function NiceRound($precision = 3) {
41
		return number_format(round($this->value, $precision), $precision);
42
	}
43
44
	public function scaffoldFormField($title = null) {
45
		return new NumericField($this->name, $title);
46
	}
47
48
	public function nullValue() {
49
		return 0;
50
	}
51
52
	public function prepValueForDB($value) {
53
		if($value === true) {
54
			return 1;
55
		} elseif(empty($value) || !is_numeric($value)) {
56
			return 0;
57
		}
58
59
		return $value;
60
	}
61
62
}
63