1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\semantic\widgets\business; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\widgets\dataform\DataForm; |
6
|
|
|
use Ajax\JsUtils; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author jc |
10
|
|
|
*/ |
11
|
|
|
abstract class BusinessForm extends DataForm { |
12
|
|
|
protected $_fieldsOrder; |
13
|
|
|
/** |
14
|
|
|
* |
15
|
|
|
* {@inheritdoc} |
16
|
|
|
* |
17
|
|
|
* @see \Ajax\semantic\widgets\dataform\DataForm::__construct() |
18
|
|
|
*/ |
19
|
|
|
public function __construct($identifier,$modelInstance=null,$fieldsOrder,$fieldsDefinition,$fields=[],$captions=[],$separators=[]) { |
20
|
|
|
if(!isset($modelInstance)){ |
21
|
|
|
$modelInstance=$this->getDefaultModelInstance(); |
22
|
|
|
} |
23
|
|
|
parent::__construct($identifier,$modelInstance); |
24
|
|
|
$this->_initForm($fieldsOrder, $fieldsDefinition,$fields,$captions,$separators); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
abstract protected function getDefaultModelInstance(); |
28
|
|
|
|
29
|
|
|
protected function _initForm($fieldsOrder,$fieldsDefinition,$fields=[],$captions=[],$separators=[]){ |
30
|
|
|
$this->_fieldsOrder=$fieldsOrder; |
31
|
|
|
$this->setFields($fields); |
32
|
|
|
$this->setSeparators($separators); |
33
|
|
|
$this->fieldsAs($fieldsDefinition); |
34
|
|
|
$this->setCaptions($captions); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function _getIndex($fieldName){ |
38
|
|
|
$index=$fieldName; |
39
|
|
|
if(\is_string($fieldName)){ |
40
|
|
|
$index=\array_search($fieldName, $this->_fieldsOrder); |
41
|
|
|
} |
42
|
|
|
return $index; |
43
|
|
|
} |
44
|
|
|
protected function _fieldAs($elementCallback,&$index,$attributes=NULL,$prefix=null){ |
45
|
|
|
$index=$this->_getIndex($index); |
46
|
|
|
return parent::_fieldAs($elementCallback, $index,$attributes,$prefix); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
public function removeField($fieldName){ |
51
|
|
|
parent::removeField($fieldName); |
52
|
|
|
\array_splice($this->_fieldsOrder,$this->_getIndex($fieldName),1); |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function compile(JsUtils $js=NULL,&$view=NULL){ |
57
|
|
|
return parent::compile($js,$view); |
58
|
|
|
} |
59
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.