PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 13 ■ DATABASE PATTERNS

if (! is_null( $field ) ) {
$this->field( $field );
}
}


// field names to which this is constrained
function getObjectFields() {
return $this->enforce;
}


// kick off a new field.
// will throw an error if a current field is not complete
// (ie age rather than age > 40)
// this method returns a reference to the current object
// allowing for fluent syntax
function field( $fieldname ) {
if (! $this->isVoid() && $this->currentfield->isIncomplete() ) {
throw new \Exception("Incomplete field");
}
$this->enforceField( $fieldname );
if ( isset( $this->fields[$fieldname] ) ) {
$this->currentfield=$this->fields[$fieldname];
} else {
$this->currentfield = new Field( $fieldname );
$this->fields[$fieldname]=$this->currentfield;
}
return $this;
}


// does the identity object have any fields yet
function isVoid() {
return empty( $this->fields );
}


// is the given fieldname legal?
function enforceField( $fieldname ) {
if (! in_array( $fieldname, $this->enforce ) &&
! empty( $this->enforce ) ) {
$forcelist = implode( ', ', $this->enforce );
throw new \Exception("{$fieldname} not a legal field ($forcelist)");
}
}


// add an equality operator to the current field
// ie 'age' becomes age=40
// returns a reference to the current object (via operator())
function eq( $value ) {
return $this->operator( "=", $value );
}


// less than
function lt( $value ) {
return $this->operator( "<", $value );
}

Free download pdf