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

Int::prepValueForDB()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 1
dl 0
loc 9
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Represents a signed 32 bit integer field.
4
 *
5
 * @package framework
6
 * @subpackage model
7
 */
8
class Int extends DBField {
9
10
	public function __construct($name = null, $defaultVal = 0) {
11
		$this->defaultVal = is_int($defaultVal) ? $defaultVal : 0;
12
13
		parent::__construct($name);
14
	}
15
16
	/**
17
	 * Returns the number, with commas added as appropriate, eg “1,000”.
18
	 */
19
	public function Formatted() {
20
		return number_format($this->value);
21
	}
22
23
	public function requireField() {
24
		$parts=Array(
25
			'datatype'=>'int',
26
			'precision'=>11,
27
			'null'=>'not null',
28
			'default'=>$this->defaultVal,
29
			'arrayValue'=>$this->arrayValue);
30
31
		$values=Array('type'=>'int', 'parts'=>$parts);
32
		DB::require_field($this->tableName, $this->name, $values);
33
	}
34
35
	public function Times() {
36
		$output = new ArrayList();
37
		for( $i = 0; $i < $this->value; $i++ )
38
			$output->push( new ArrayData( array( 'Number' => $i + 1 ) ) );
39
40
		return $output;
41
	}
42
43
	public function Nice() {
44
		return sprintf( '%d', $this->value );
45
	}
46
47
	public function scaffoldFormField($title = null, $params = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
		return new NumericField($this->name, $title);
49
	}
50
51
	public function nullValue() {
52
		return 0;
53
	}
54
55
	public function prepValueForDB($value) {
56
		if($value === true) {
57
			return 1;
58
		} elseif(empty($value) || !is_numeric($value)) {
59
			return 0;
60
		}
61
62
		return $value;
63
	}
64
65
}
66
67