1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* A simple extension to dropdown field, pre-configured to list countries. |
5
|
|
|
* It will default to the country of the current visitor. |
6
|
|
|
* |
7
|
|
|
* @package forms |
8
|
|
|
* @subpackage fields-relational |
9
|
|
|
*/ |
10
|
|
|
class ForumCountryDropdownField extends DropdownField |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var bool - Should we default the dropdown to the region determined from the user's locale? |
15
|
|
|
*/ |
16
|
|
|
private static $default_to_locale = true; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string - The region code to default to if default_to_locale is set to false, or we can't determine a region from a locale |
20
|
|
|
*/ |
21
|
|
|
private static $default_country = 'NZ'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Get the locale of the Member, or if we're not logged in or don't have a locale, use the default one |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
protected function locale() |
28
|
|
|
{ |
29
|
|
|
if (($member = Member::currentUser()) && $member->Locale) { |
30
|
|
|
return $member->Locale; |
31
|
|
|
} |
32
|
|
|
return i18n::get_locale(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __construct($name, $title = null, $source = null, $value = "", $form = null) |
36
|
|
|
{ |
37
|
|
|
if (!is_array($source)) { |
38
|
|
|
// Get a list of countries from Zend |
39
|
|
|
$source = Zend_Locale::getTranslationList('territory', $this->locale(), 2); |
40
|
|
|
|
41
|
|
|
// We want them ordered by display name, not country code |
42
|
|
|
|
43
|
|
|
// PHP 5.3 has an extension that sorts UTF-8 strings correctly |
44
|
|
|
if (class_exists('Collator') && ($collator = Collator::create($this->locale()))) { |
45
|
|
|
$collator->asort($source); |
46
|
|
|
} // Otherwise just put up with them being weirdly ordered for now |
47
|
|
|
else { |
48
|
|
|
asort($source); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// We don't want "unknown country" as an option |
52
|
|
|
unset($source['ZZ']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
parent::__construct($name, ($title===null) ? $name : $title, $source, $value, $form); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function Field($properties = array()) |
59
|
|
|
{ |
60
|
|
|
$source = $this->getSource(); |
61
|
|
|
|
62
|
|
|
if (!$this->value || !isset($source[$this->value])) { |
63
|
|
|
if ($this->config()->get('default_to_locale') && $this->locale()) { |
64
|
|
|
$locale = new Zend_Locale(); |
65
|
|
|
$locale->setLocale($this->locale()); |
66
|
|
|
$this->value = $locale->getRegion(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (!$this->value || !isset($source[$this->value])) { |
71
|
|
|
$this->value = $this->config()->get('default_country'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return parent::Field(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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.