|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class to provide access to custom texts for emails |
|
5
|
|
|
* |
|
6
|
|
|
* @since 0.2 |
|
7
|
|
|
* |
|
8
|
|
|
* @file SWL_CustomTexts.php |
|
9
|
|
|
* @ingroup SemanticWatchlist |
|
10
|
|
|
* |
|
11
|
|
|
* @author Nischay Nahata |
|
12
|
|
|
*/ |
|
13
|
|
|
class SWLCustomTexts { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Group for this CustomTexts |
|
17
|
|
|
* |
|
18
|
|
|
* @since 0.2 |
|
19
|
|
|
* |
|
20
|
|
|
* @var SWLGroup |
|
21
|
|
|
*/ |
|
22
|
|
|
private $group; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Array holding custom texts to be sent in mails |
|
26
|
|
|
* |
|
27
|
|
|
* @since 0.2 |
|
28
|
|
|
* |
|
29
|
|
|
* @var array or null |
|
30
|
|
|
*/ |
|
31
|
|
|
private $customTexts = null; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct( SWLGroup $group ) { |
|
34
|
|
|
$this->group = $group; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Sets an array of CustomTexts by reading from the db |
|
39
|
|
|
* for this group. |
|
40
|
|
|
* |
|
41
|
|
|
* @since 0.2 |
|
42
|
|
|
*/ |
|
43
|
|
|
private function initCustomTexts() { |
|
44
|
|
|
if( !is_null( $this->customTexts ) ) { |
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
$this->customTexts = array(); |
|
48
|
|
|
$dbr = wfGetDB( DB_REPLICA ); |
|
49
|
|
|
$row = $dbr->selectRow( |
|
50
|
|
|
'swl_groups', |
|
51
|
|
|
'group_custom_texts', |
|
52
|
|
|
array( 'group_name' => $this->group->getName() ), |
|
53
|
|
|
'SWL::initCustomTexts' |
|
54
|
|
|
); |
|
55
|
|
|
$set = explode( '|', $row->group_custom_texts ); |
|
56
|
|
|
|
|
57
|
|
|
foreach( $set as $elem ) { |
|
58
|
|
|
$parts = explode( '~', $elem ); |
|
59
|
|
|
if( !array_key_exists( $parts[0], $this->customTexts ) ) { |
|
60
|
|
|
$this->customTexts[$parts[0]] = array(); |
|
61
|
|
|
} |
|
62
|
|
|
$this->customTexts[$parts[0]][$parts[1]] = $parts[2]; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Returns an array of CustomTexts set by the admin in WatchlistConditions |
|
68
|
|
|
* for this group and property. |
|
69
|
|
|
* |
|
70
|
|
|
* @since 0.2 |
|
71
|
|
|
* |
|
72
|
|
|
* @param SMWDIProperty $property |
|
73
|
|
|
* @param String $newValue |
|
74
|
|
|
* |
|
75
|
|
|
* @return String or false |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getPropertyCustomText( SMWDIProperty $property, $newValue ) { |
|
78
|
|
|
$this->initCustomTexts(); |
|
79
|
|
|
if( array_key_exists( $property->getLabel(), $this->customTexts ) && array_key_exists( $newValue, $this->customTexts[$property->getLabel()] ) ) { |
|
80
|
|
|
return $this->customTexts[$property->getLabel()][$newValue]; |
|
81
|
|
|
} else { |
|
82
|
|
|
return false; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
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.