1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Collections\Enum |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/collections |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Collections; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Lang\Object; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Interface for lists of objects that can be returned in sequence. |
27
|
|
|
* |
28
|
|
|
* Successive objects are obtained by the nextElement method. |
29
|
|
|
* |
30
|
|
|
* @author Tim Wagner <[email protected]> |
31
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
33
|
|
|
* @link https://github.com/appserver-io/collections |
34
|
|
|
* @link http://www.appserver.io |
35
|
|
|
*/ |
36
|
|
|
class Enum extends Object implements EnumerationInterface |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The possible items. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $items = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The point to the actual item. |
48
|
|
|
* |
49
|
|
|
* @var integer |
50
|
|
|
*/ |
51
|
|
|
protected $itemPointer = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Standard constructor that adds the array passed |
55
|
|
|
* as parameter to the internal member variable. |
56
|
|
|
* |
57
|
|
|
* @param array $items An array to initialize the Enumeration |
58
|
|
|
*/ |
59
|
|
|
public function __construct($items = array()) |
60
|
|
|
{ |
61
|
|
|
$this->items = $items; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Tests whether there are elements remaining in the enumeration. |
66
|
|
|
* |
67
|
|
|
* @return true if there is at least one more element in the enumeration, that is, if the next call to nextElement will not throw a NoSuchElementException. |
68
|
|
|
* @see \AppserverIo\Collections\Enumeration::hasMoreElements() |
69
|
|
|
*/ |
70
|
|
|
public function hasMoreElements() |
71
|
|
|
{ |
72
|
|
|
if (count($this->items) > $this->itemPointer) { |
73
|
|
|
return true; |
|
|
|
|
74
|
|
|
} else { |
75
|
|
|
return false; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Obtain the next element in the enumeration. |
81
|
|
|
* |
82
|
|
|
* @return mixed The next element in the enumeration |
83
|
|
|
* @throws \AppserverIo\Collections\NoSuchElementException if there are no more elements |
84
|
|
|
* @see \AppserverIo\Collections\EnumerationInterface::nextElement() |
85
|
|
|
*/ |
86
|
|
|
public function nextElement() |
87
|
|
|
{ |
88
|
|
|
if (array_key_exists($this->itemPointer, $this->items)) { |
89
|
|
|
return $this->items[$this->itemPointer ++]; |
90
|
|
|
} else { |
91
|
|
|
throw new NoSuchElementException('No such element was found'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.