|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2017 Romain CANON <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
7
|
|
|
* under the terms of the GNU General Public License, either |
|
8
|
|
|
* version 3 of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, see: |
|
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Romm\Formz\Condition\Items; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler; |
|
17
|
|
|
use Romm\Formz\Condition\Exceptions\InvalidConditionException; |
|
18
|
|
|
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject; |
|
19
|
|
|
use TYPO3\CMS\Extbase\Reflection\ObjectAccess; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* This condition will match when a field is valid (its validation returned no |
|
23
|
|
|
* error). |
|
24
|
|
|
*/ |
|
25
|
|
|
class FieldIsEmptyCondition extends AbstractConditionItem |
|
26
|
|
|
{ |
|
27
|
|
|
const CONDITION_NAME = 'fieldIsEmpty'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritdoc |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
protected static $javaScriptFiles = [ |
|
34
|
|
|
'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsEmpty.js' |
|
35
|
|
|
]; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
* @validate NotEmpty |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $fieldName; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritdoc |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getCssResult() |
|
47
|
|
|
{ |
|
48
|
|
|
return [ |
|
|
|
|
|
|
49
|
|
|
'[' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '=""]', |
|
50
|
|
|
':not([' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '])' |
|
51
|
|
|
]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @inheritdoc |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getJavaScriptResult() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getPhpResult(PhpConditionDataObject $dataObject) |
|
66
|
|
|
{ |
|
67
|
|
|
$value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName); |
|
68
|
|
|
|
|
69
|
|
|
return empty($value); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @see validateConditionConfiguration() |
|
74
|
|
|
* @throws InvalidConditionException |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function checkConditionConfiguration() |
|
78
|
|
|
{ |
|
79
|
|
|
$configuration = $this->formObject->getConfiguration(); |
|
80
|
|
|
|
|
81
|
|
|
if (false === $configuration->hasField($this->fieldName)) { |
|
82
|
|
|
throw InvalidConditionException::conditionFieldIsEmptyFieldNotFound($this->fieldName); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.