1818 *
1919 * @author Raul Fraile <raulfraile@gmail.com>
2020 */
21- class ThemeResolver
21+ class ThemeResolver implements \Countable
2222{
2323
2424 /** @var ThemeInterface[] An array of ThemeInterface objects */
2525 protected $ themes ;
2626
27+ protected $ default = null ;
28+
2729 /**
2830 * Constructor.
2931 *
@@ -40,9 +42,15 @@ public function __construct(array $themes = array())
4042 * @param ThemeInterface $theme A ThemeInterface instance
4143 * @param $key
4244 */
43- public function addTheme (ThemeInterface $ theme , $ key )
45+ public function addTheme (ThemeInterface $ theme , $ key, $ default = false )
4446 {
4547 $ this ->themes [$ key ] = $ theme ;
48+
49+ if ($ default ) {
50+ $ this ->default = $ key ;
51+ }
52+
53+ return true ;
4654 }
4755
4856 /**
@@ -64,6 +72,10 @@ public function resolve($format)
6472
6573 public function getTheme ($ key , $ format )
6674 {
75+ if (!array_key_exists ('theme_ ' . $ key , $ this ->themes )) {
76+ return $ this ->getDefaultTheme ();
77+ }
78+
6779 /** @var $theme ThemeInterface */
6880 $ theme = $ this ->themes ['theme_ ' . $ key ];
6981
@@ -88,4 +100,30 @@ protected function supportsFormat(ThemeInterface $theme, $format)
88100 {
89101 return in_array ($ format , $ theme ->getFormats ());
90102 }
103+
104+ /**
105+ * Gets the default theme.
106+ *
107+ * @return ThemeInterface
108+ */
109+ protected function getDefaultTheme ()
110+ {
111+ if (!is_null ($ this ->default ) && array_key_exists ($ this ->default , $ this ->themes )) {
112+ return $ this ->themes [$ this ->default ];
113+ }
114+
115+ return null ;
116+ }
117+
118+ /**
119+ * Count number of themes.
120+ *
121+ * @return int Number of registered themes
122+ */
123+ public function count ()
124+ {
125+ return count ($ this ->themes );
126+ }
127+
128+
91129}
0 commit comments