1
|
|
|
<?php namespace Cornford\Alerter; |
2
|
|
|
|
3
|
|
|
use Cornford\Alerter\Contracts\DisplayableInterface; |
4
|
|
|
use Cornford\Alerter\Exceptions\AlertDisplayViewException; |
5
|
|
|
use Cornford\Alerter\Exceptions\AlertDisplayViewPathException; |
6
|
|
|
use RecursiveDirectoryIterator; |
7
|
|
|
use RecursiveIteratorIterator; |
8
|
|
|
|
9
|
|
|
class AlertDisplay implements DisplayableInterface { |
10
|
|
|
|
11
|
|
|
const VIEW_SIMPLE = 'simple'; |
12
|
|
|
const VIEW_LIST = 'list'; |
13
|
|
|
const VIEW_BOOTSTRAP = 'bootstrap'; |
14
|
|
|
const VIEW_BOOTSTRAP_CLOSABLE = 'bootstrap-closeable'; |
15
|
|
|
const VIEW_FOUNDATION = 'foundation'; |
16
|
|
|
const VIEW_FOUNDATION_CLOSABLE = 'foundation-closable'; |
17
|
|
|
const VIEW_PURE = 'pure'; |
18
|
|
|
const VIEW_PURE_CLOSABLE = 'pure-closable'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The views base folder. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $viewPath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The Alert object. |
29
|
|
|
* |
30
|
|
|
* @var Alert |
31
|
|
|
*/ |
32
|
|
|
private $alert; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The view name. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $view; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Construct class with an alert object, a display view, and a view path. |
43
|
|
|
* |
44
|
|
|
* @param Alert $alert |
45
|
|
|
* @param string $view |
46
|
|
|
* @param string $viewPath |
47
|
|
|
* |
48
|
|
|
* @return self |
|
|
|
|
49
|
|
|
* |
50
|
|
|
* @throws \Cornford\Alerter\Exceptions\AlertDisplayViewPathException |
51
|
|
|
*/ |
52
|
|
|
public function __construct(Alert $alert, $viewPath, $view) |
53
|
|
|
{ |
54
|
|
|
$this->alert = $alert; |
55
|
|
|
|
56
|
|
|
if (!is_dir($viewPath)) { |
57
|
|
|
throw new AlertDisplayViewPathException('Could not locate the view path.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->viewPath = $viewPath; |
61
|
|
|
$this->view = $view; |
62
|
|
|
|
63
|
|
|
$this->view = $this->findDisplayViewFile(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the directory iterator. |
68
|
|
|
* |
69
|
|
|
* @return RecursiveIteratorIterator |
70
|
|
|
*/ |
71
|
|
|
protected function getDirectoryIterator() |
72
|
|
|
{ |
73
|
|
|
return new RecursiveIteratorIterator( |
74
|
|
|
new RecursiveDirectoryIterator($this->viewPath) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Track down the display view file. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
* |
83
|
|
|
* @throws \Cornford\Alerter\Exceptions\AlertDisplayViewException |
84
|
|
|
*/ |
85
|
|
|
protected function findDisplayViewFile() |
86
|
|
|
{ |
87
|
|
|
foreach($this->getDirectoryIterator() as $file) { |
88
|
|
|
$name = $file->getFilename(); |
89
|
|
|
|
90
|
|
|
if ($this->isDisplayViewFile($name)) { |
91
|
|
|
return $file->getPathname(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
throw new AlertDisplayViewException('Could not locate the view file.'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Is the given file a display view file? |
100
|
|
|
* |
101
|
|
|
* @param string $name |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
protected function isDisplayViewFile($name) |
106
|
|
|
{ |
107
|
|
|
return preg_match('/' . $this->view . '.php$/i', $name); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Return a compiled alert display view. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function display() |
116
|
|
|
{ |
117
|
|
|
ob_start(); |
118
|
|
|
require $this->view; |
119
|
|
|
|
120
|
|
|
return ob_get_clean(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.