/** * Track an action * * Params is an array of * * array( * 'user' => 'username/uniqueid', * 'url' => 'urlbeingviewed', * 'actionname' => 'the name of the action' * 'actionid' => 'uniqueid for the actor in the action' * 'remoteip' => 'the remote ip' * ) * * @param array $params */ public function trackAction($params) { $entry = new TrackerEntry(); $entry->bind($params); $validator = new ModelValidator(); if (!$validator->isValid($entry)) { throw new InvalidModelException($validator->getMessages()); } // Save the entry $this->dbService->createObject($entry); }
function testValidation() { $contact = new Contact(); $validator = new ModelValidator(); $this->assertFalse($validator->isValid($contact)); $msgs = $validator->getMessages(); $this->assertEqual(1, count($msgs)); // Set an email address that's invalid $contact->firstname = 'Firstname'; $contact->email = 'nothing'; $this->assertFalse($validator->isValid($contact)); $contact->email = '*****@*****.**'; $this->assertTrue($validator->isValid($contact)); }
function testRequiredFields() { $properties = array('variable' => 'avalue'); include_once APP_DIR . '/model/SampleModel.php'; include_once APP_DIR . '/model/EmptyModel.php'; $modelValidator = new ModelValidator(); $sample = new SampleModel(); // No constraints, so should be valid $this->assertTrue($modelValidator->isValid($sample)); $requiredFields = array('variable'); $sample->requiredFields = $requiredFields; $this->assertFalse($modelValidator->isValid($sample)); $sample->bind($properties); $this->assertEqual($sample->variable, 'avalue'); // Should pass $this->assertTrue($modelValidator->isValid($sample)); $sample->variable = ''; // Should now fail $this->assertFalse($modelValidator->isValid($sample)); }
/** * Validate the model * * If this model has a method called 'validate', model will be passed to * ModelValidator::validate() and the validations will be checked, and boolean * result returned. Otherwise, returns true. * * @return boolean */ public function validates() { if (method_exists($this, 'validate') === true) { $result = ModelValidator::validate($this); if ($result === true) { $return = true; } else { $this->errors = $result; $return = false; } return $return; } else { return true; } }
/** * Update a given user. * @param NovemberUser $userToEdit * @param array $params */ public function updateUser($userToEdit, $params, $synch = true) { if (isset($params['email'])) { // Check if there's a user with this email first. $existing = $this->dbService->getByField(array('email' => $params['email']), $this->userClass); if ($existing && $existing->id != $userToEdit->getId()) { throw new ExistingUserException($params['email'] . ' already exists'); } } // Make sure no role is being changed! we do that in another method. unset($params['role']); $newPass = null; if (isset($params['password'])) { $newPass = $params['password']; $userToEdit->generateSaltedPassword($params['password']); unset($params['password']); } $userToEdit->bind($params); $validator = new ModelValidator(); if (!$validator->isValid($userToEdit)) { throw new InvalidModelException($validator->getMessages()); } $ret = $this->dbService->updateObject($userToEdit); $this->authComponent->updateUser($userToEdit, $newPass); return $ret; }
/** * Saves an object with the given parameters. * If the param['id'] field is filled out, then that object is loaded * and updated, otherwise a new object of $type is created * * @param array $object * @param string $type * @return The created object */ public function saveObject($params, $type = '') { $oldObject = null; if (is_array($params)) { $this->typeManager->includeType(ucfirst($type)); if (isset($params['id'])) { $object = $this->getById((int) $params['id'], $type); } else { $object = new $type(); za()->inject($object); } /* @var $object MappedObject */ if ($object instanceof MappedObject) { $object->bind($params); } else { throw new InvalidModelException(array("Class " . get_class($object) . " must subclass MappedObject to use saveObject")); } // Validate $modelValidator = new ModelValidator(); if (!$modelValidator->isValid($object)) { throw new InvalidModelException($modelValidator->getMessages()); } } else { $object = $params; } if ($object == null) { throw new Exception("Cannot save null object"); } $refObj = new ReflectionObject($object); if ($refObj->hasProperty('updated')) { $object->updated = date('Y-m-d H:i:s', time()); } if ($refObj->hasProperty('modifier')) { $object->modifier = za()->getUser()->getUsername(); } if (get_class($object) == 'stdClass') { throw new Exception("Cannot save stdClass"); } $success = false; $this->triggerObjectEvent($object, 'beforeSave'); if ($object->id) { $success = $this->updateObject($object); } else { $success = $this->createObject($object); } $this->triggerObjectEvent($object, 'saved'); if ($success) { if ($this->searchService != null) { $this->searchService->index($object); } return $object; } return null; }