1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Element; |
4
|
|
|
|
5
|
|
|
class Password extends NamedFormElement |
6
|
|
|
{ |
7
|
|
|
public function __construct($path, $label = null) |
8
|
|
|
{ |
9
|
|
|
parent::__construct($path, $label); |
10
|
|
|
|
11
|
|
|
$this->setHtmlAttributes([ |
12
|
|
|
'class' => 'form-control', |
13
|
|
|
'type' => 'password', |
14
|
|
|
]); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $allowEmpty = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $view = 'form.element.password'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Illuminate\Http\Request $request |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public function save(\Illuminate\Http\Request $request) |
33
|
|
|
{ |
34
|
|
|
$value = $this->getValueFromModel(); |
35
|
|
|
|
36
|
|
|
if (! $this->isAllowedEmptyValue() and $this->getModel()->exists and empty($value)) { |
|
|
|
|
37
|
|
|
return; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
parent::save($request); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function getValidationRules() |
47
|
|
|
{ |
48
|
|
|
$data = parent::getValidationRules(); |
49
|
|
|
|
50
|
|
|
if (! $this->isAllowedEmptyValue() and $this->getModel()->exists) { |
|
|
|
|
51
|
|
|
foreach ($data as $field => $rules) { |
52
|
|
|
foreach ($rules as $i => $rule) { |
53
|
|
|
if ($rule == 'required') { |
54
|
|
|
unset($data[$field][$i]); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $data; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function isAllowedEmptyValue() |
67
|
|
|
{ |
68
|
|
|
return $this->allowEmpty; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function allowEmptyValue() |
75
|
|
|
{ |
76
|
|
|
$this->allowEmpty = true; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public function hashWithBcrypt() |
85
|
|
|
{ |
86
|
|
|
return $this->mutateValue(function ($value) { |
87
|
|
|
return bcrypt($value); |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function hashWithMD5() |
95
|
|
|
{ |
96
|
|
|
return $this->mutateValue(function ($value) { |
97
|
|
|
return md5($value); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
|
|
public function hashWithSHA1() |
105
|
|
|
{ |
106
|
|
|
return $this->mutateValue(function ($value) { |
107
|
|
|
return sha1($value); |
108
|
|
|
}); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.