1: <?php
2: /**
3: * Created by PhpStorm.
4: * User: manus
5: * Date: 10/03/16
6: * Time: 16:23
7: */
8:
9: namespace GLFramework\DaMa;
10:
11:
12: use GLFramework\Model;
13:
14: class Association
15: {
16: public $nameInFile = array();
17: public $nameInModel;
18: public $constant = false;
19: public $index = false;
20: public $parser = null;
21:
22: /**
23: * @return mixed
24: */
25: public function getNameInFile()
26: {
27: return $this->nameInFile;
28: }
29:
30: /**
31: * @param mixed $nameInFile
32: */
33: public function addNameInFile($nameInFile)
34: {
35: $this->nameInFile[] = $nameInFile;
36: }
37:
38: /**
39: * @return mixed
40: */
41: public function getNameInModel()
42: {
43: return $this->nameInModel;
44: }
45:
46: /**
47: * @param mixed $nameInModel
48: */
49: public function setNameInModel($nameInModel)
50: {
51: $this->nameInModel = $nameInModel;
52: }
53:
54: /**
55: * @return null
56: */
57: public function getParser()
58: {
59: return $this->parser;
60: }
61:
62: /**
63: * @param null $parser
64: */
65: public function setParser($parser)
66: {
67: $this->parser = $parser;
68: }
69:
70: /**
71: * @return boolean
72: */
73: public function isConstant()
74: {
75: return $this->constant;
76: }
77:
78: /**
79: * @param boolean $constant
80: */
81: public function setConstant($constant)
82: {
83: $this->constant = $constant;
84: }
85:
86: /**
87: * @param $model Model
88: * @param $row
89: * @return bool
90: */
91: public function fill($model, $row)
92: {
93: if(!$this->constant)
94: {
95: foreach($this->nameInFile as $subkey)
96: {
97: if(isset($row[$subkey]))
98: {
99: $model->{$this->nameInModel} = $this->parse($row[$subkey]);
100: return true;
101: }
102: }
103: }
104: else
105: {
106: $model->{$this->nameInModel} = $this->constant;
107: return true;
108: }
109: return false;
110: }
111:
112: private function parse($value)
113: {
114: if($this->parser != null)
115: {
116: return call_user_func($this->parser, $value);
117: }
118: return $value;
119: }
120:
121: public function index()
122: {
123: $this->index = true;
124: return $this;
125: }
126:
127: }