1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\ViewHelpers\Backend\Document; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 Rafael Kähm <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
use TYPO3\CMS\Fluid\Core\ViewHelper\AbstractConditionViewHelper; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Condition for checking if type is a string. |
32
|
|
|
*/ |
33
|
|
|
class IsBoostedFieldViewHelper extends AbstractConditionViewHelper |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initialize ViewHelper arguments |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function initializeArguments() |
42
|
|
|
{ |
43
|
|
|
parent::initializeArguments(); |
44
|
|
|
$this->registerArgument('fieldName', 'string', 'Fieldname to be verified.', true); |
45
|
|
|
$this->registerArgument('document', '\Apache_Solr_Document', '\Apache_Solr_Document to be verified.', true); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* This method decides if the condition is true or false |
50
|
|
|
* |
51
|
|
|
* @param array $arguments ViewHelper arguments to evaluate the condition for this ViewHelper. |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
2 |
|
protected static function evaluateCondition($arguments = null) |
55
|
|
|
{ |
56
|
|
|
/* @var $document \Apache_Solr_Document */ |
57
|
2 |
|
$document = $arguments['document']; |
58
|
2 |
|
return $document->getFieldBoost($arguments['fieldName']); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
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.