Paperyard
Rule.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Paperyard\Models\Rule;
4 
7 
8 class Rule extends Model
9 {
11  const TAG_REGEX = '/^([ÄäÜüÖöß\sa-zA-Z0-9]+,)*[ÄäÜüÖöß\sa-zA-Z0-9]+$/';
12 
14  protected $attributes = array(
15  'isActive' => 0
16  );
17 
19  protected $rules;
20 
22  protected $labels;
23 
25  public $errors = [];
26 
32  public function setIsActiveAttribute($value)
33  {
34  $this->attributes['isActive'] = (int)($value == 'on');
35  }
36 
40  private function validate()
41  {
42  // new validator object
43  $validator = new Validator($this->getAttributes());
44 
45  // pass rules
46  $validator->rules($this->rules);
47 
48  // add labels (we dont want to show internal names to the user)
49  $validator->labels($this->labels);
50 
51  // check rules
52  if(!$validator->validate()) {
53  $this->errors = $validator->errors();
54  }
55  }
56 
62  public function validateAndSave()
63  {
64  // check rules
65  $this->validate();
66 
67  // return result
68  if(empty($this->errors)) {
69  return $this->save();
70  } else {
71  return false;
72  }
73  }
74 
80  public function validateAndUpdate()
81  {
82  // check rules
83  $this->validate();
84 
85  // return result
86  if(empty($this->errors)) {
87  return $this->update();
88  } else {
89  return false;
90  }
91  }
92 }
setIsActiveAttribute($value)
Definition: Rule.php:32