-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisplayLogic.php
54 lines (42 loc) · 1.4 KB
/
DisplayLogic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Created by PhpStorm.
* User: Joel Small
* Date: 31/05/15
* Time: 12:47 PM
*/
namespace enigmatix\helpers;
use Yii;
class DisplayLogic {
static $defaultAction = 'change';
static function addLogic($view, $source, $target, $conditions)
{
$logic = static::evaluateConditions($conditions);
$action = static::buildAction($target);
$statement = static::buildStatement($logic, $action);
$bound = static::bindToField($source, $statement, static::$defaultAction);
static::register($view, $bound);
}
static function evaluateConditions(array $conditions, $source){
$script = '';
foreach ($conditions as $attribute => $value){
$script .= " && " . static::evaluateCondition($attribute, $value, $source);
}
return strlen($script) ? substr($script,4) : $script;
}
static function buildAction($target){
return "{{$target.hide}}else{{$target.show;}}";
}
static function evaluateCondition($attribute, $value, $source){
return "'$value'= $('#$source').attr('$attribute')'";
}
static function buildStatement($logic, $action){
return "if($logic)$action";
}
static function bindToField($field, $script, $event){
return "$('#$field').$event($script)'";
}
static function register($view, $script){
$view->registerJS($script);
}
}