1: <?php
2:
3: namespace Zippy\Binding;
4:
5: use Zippy\Interfaces\Binding;
6:
7: /**
8: * Реализует привязку данных к свойству объекта.
9: * При изменении значеня поля изменяется соответствующее поле в заданном объекте и наоборот.
10: * @see Binding
11: */
12: class PropertyBinding implements Binding
13: {
14: protected $obj = null;
15: protected $propertyname = "";
16:
17: /**
18: * Конструктор
19: * @param mixed Объект
20: * @param string Название свойства объекта (должно быть public)
21: *
22: * class Myobj
23: * {
24: * public $myvalue
25: * } ;
26: *
27: * $o = new Myobj() ;
28: *
29: * new PropertyBinding($o,'myvalue');
30: *
31: */
32: public function __construct($obj, $propertyname) {
33: $this->obj = $obj;
34: $this->propertyname = $propertyname;
35: }
36:
37: public function getValue() {
38: if (false) {
39: if (!in_array($this->propertyname, array_keys(get_object_vars($this->obj)), false)) {
40: throw new \Zippy\Exception(sprintf(ERROR_NOT_FOUND_PROPERTY_BINDING, $this->propertyname, get_class($this->obj)));
41: };
42: }
43: return $this->obj->{$this->propertyname};
44: }
45:
46: public function getPropertyName() {
47: return $this->propertyname;
48: }
49:
50: public function setValue($data) {
51: if (false) {
52: if (!in_array($this->propertyname, array_keys(get_object_vars($this->obj)), false)) {
53: throw new \Zippy\Exception(sprintf(ERROR_NOT_FOUND_PROPERTY_BINDING, $this->propertyname, get_class($this->obj)));
54: };
55: }
56: $this->obj->{$this->propertyname} = $data;
57: }
58:
59: }
60: