function test_init() { $record = new Dataface_Record('Profiles', array('id' => 3)); $this->assertEquals(array('id' => 3), $record->vals(array('id'))); $this->assertEquals(array('id' => 3), $record->values(array('id'))); $this->assertEquals(array('id' => 3), $record->getValues(array('id'))); $table =& $record->table(); $keys = array_keys($table->fields()); $expected = array(); foreach ($keys as $key) { $expected[$key] = null; } $expected['id'] = 3; $this->assertEquals($expected, $record->vals()); }
function evaluate($expr, $fieldname, Dataface_Record $record) { // Check the field type first. If this is a numeric field // we evaluate with math // If it is a string, we evaluate with string functions // Chop the leading '=' $expr = substr($expr, 1); $table = $record->table(); if ($table->isInt($fieldname) or $table->isFloat($fieldname)) { // This field is a numeric field so we treat it like // a numeric operation $oldval = $record->val($fieldname); if (!$oldval) { $oldval = 0; } $expr = preg_replace('/\\$\\$/', $oldval, $expr); if (preg_match('/^[\\+\\-\\*\\/]/', $expr)) { // If expression begins with an operation, we apply // the old value as the first operand of this // operation. $expr = $oldval . $expr; } if (!preg_match('/^[0-9\\.\\*\\+\\/\\-\\^\\(\\) ]+$/', $expr)) { throw new Exception("Invalid arithmetic expression."); } @eval('$expr=' . $expr . ';'); return $expr; } else { if ($table->isDate($fieldname)) { // This field is a date field so we treat it like a // date operation $oldtime = strtotime($record->strval($fieldname)); return date($expr, $oldtime); } else { // We assume it is a string $oldval = $record->val($fieldname); $expr = preg_replace('/\\$\\$/', $oldval, $expr); return $expr; } } }