示例#1
0
文件: Tariffs.php 项目: RobBickel/las
 /**
  * Write method - add/edit the tariff
  *
  * @package     las
  * @version     1.0
  *
  * @param string $method type: create/update
  * @return mixed
  */
 public function write($method = 'create')
 {
     $validation = new \Las\Extension\Validation();
     $validation->add('name', new Validator\PresenceOf());
     $validation->add('name', new Validator\StringLength(['min' => 3, 'max' => 32]));
     $validation->add('amount', new Validator\PresenceOf());
     $validation->add('amount', new Validator\Regex(['pattern' => '/\\d+(\\.\\d{2})?/']));
     $validation->add('priority', new Validator\Between(['minimum' => 10, 'maximum' => 99]));
     $validation->add('downloadRate', new Validator\Between(['minimum' => 0, 'maximum' => Tariffs::rate('download')]));
     $validation->add('downloadCeil', new Validator\Between(['minimum' => 0, 'maximum' => Networks::ceil('download')]));
     $validation->add('uploadRate', new Validator\Between(['minimum' => 0, 'maximum' => Tariffs::rate('upload')]));
     $validation->add('uploadCeil', new Validator\Between(['minimum' => 0, 'maximum' => Networks::ceil('upload')]));
     $validation->add('limit', new Validator\Regex(['allowEmpty' => true, 'pattern' => '/\\d+/']));
     $validation->add('description', new Validator\StringLength(['max' => 1024]));
     $validation->add('status', new Validator\InclusionIn(['domain' => Tariffs::status()]));
     $validation->setLabels(['name' => __('Name'), 'amount' => __('Amount'), 'priority' => __('Priority'), 'downloadRate' => __('Download rate'), 'downloadCeil' => __('Download ceil'), 'uploadRate' => __('Upload rate'), 'uploadCeil' => __('Upload ceil'), 'limit' => __('Limit'), 'description' => __('Description'), 'status' => __('Status')]);
     $messages = $validation->validate($_POST);
     // Return messages if validation not pass
     if (count($messages)) {
         return $validation->getMessages();
     } else {
         $this->name = $this->request->getPost('name', 'string');
         $this->amount = $this->request->getPost('amount');
         $this->priority = $this->request->getPost('priority', 'int');
         $this->downloadRate = $this->request->getPost('downloadRate', 'float');
         $this->downloadCeil = $this->request->getPost('downloadCeil', 'float');
         $this->uploadRate = $this->request->getPost('uploadRate', 'float');
         $this->uploadCeil = $this->request->getPost('uploadCeil', 'float');
         $this->limit = $this->request->getPost('limit', 'int', null, ture);
         $this->description = $this->request->getPost('description');
         $this->status = $this->request->getPost('status', 'int');
         $this->date = date('Y-m-d H:i:s');
         // Try to write the record
         if ($this->{$method}() === true) {
             return $this;
         } else {
             \Las\Bootstrap::log($this->getMessages());
             return $this->getMessages();
         }
     }
 }