Example #1
0
 public function update($data, $commit = false)
 {
     $errors = array();
     if (isset($data['_id']) && $this->getId() !== $data['_id']) {
         $errors[] = PogginError::getMessage('ID_MISMATCH');
     }
     $newdata = $this->getDefaults();
     foreach ($newdata as $key => $value) {
         if (isset($data[$key])) {
             $newdata[$key] = $data[$key];
         }
     }
     $validate = $this->validate($newdata);
     if ($validate !== true) {
         $errors = array_merge($errors, $validate);
     }
     // if there are errors, return
     if (!empty($errors)) {
         return $errors;
     }
     // add MongoId
     $newdata = array('_id' => new MongoId($this->getId())) + $newdata;
     // set new $data
     $this->data = $newdata;
     if ($commit) {
         $this->commit(false);
     }
     // false, to not double validate().
     return true;
 }
Example #2
0
 public function validate($data = null)
 {
     $errors = array();
     // error collector array
     // if $data is not passed, use this instance's data.
     if ($data === null) {
         $data = $this->data;
     }
     // required
     if ('' == trim($data['title'])) {
         $errors[] = PogginError::getMessage('SONG_REQUIRED_TITLE');
     }
     if ('' == trim($data['text'])) {
         $errors[] = PogginError::getMessage('SONG_REQUIRED_TEXT');
     }
     // length
     if (64 < strlen($data['title'])) {
         $errors[] = PogginError::getMessage('SONG_LENGTH_TITLE');
     }
     if (64 < strlen($data['author'])) {
         $errors[] = PogginError::getMessage('SONG_LENGTH_AUTHOR');
     }
     if (4096 < strlen($data['text'])) {
         $errors[] = PogginError::getMessage('SONG_LENGTH_TEXT');
     }
     // invalid year input (not very extensive checking).
     if (!empty($data['year']) && !is_numeric($data['year'])) {
         $errors[] = PogginError::getMessage('SONG_INVALID_YEAR');
     }
     if (empty($errors)) {
         return true;
     }
     return $errors;
 }
Example #3
0
function pogginError($code, $status = 501)
{
    global $app;
    if (is_array($code)) {
        $response = $code;
    } else {
        $response = PogginError::getMessage($code);
    }
    $app->response()->header('Content-type', 'application/json');
    $app->halt($status, json_encode($response));
}