33namespace PageSpecificCss ;
44
55use PageSpecificCss \Css \Property \Property ;
6+ use PageSpecificCss \Css \Rule \Rule ;
67
78class CssStore
89{
@@ -11,7 +12,8 @@ class CssStore
1112
1213 public function addCssStyles ($ cssRules )
1314 {
14- $ this ->styles = array_merge_recursive ($ this ->styles , $ cssRules );
15+ $ this ->styles = array_merge ($ this ->styles , $ cssRules );
16+
1517 return $ this ;
1618 }
1719
@@ -32,9 +34,23 @@ public function dumpStyles($path)
3234
3335 public function compileStyles ()
3436 {
35- return join ('' , array_map (function ($ properties , $ key ) {
36- return $ this ->parseMediaToString ($ key , $ properties );
37- }, $ this ->styles , array_keys ($ this ->styles )));
37+
38+ // Structure rules in order, by media query
39+ $ styles = $ this ->prepareStylesForProcessing ();
40+
41+
42+ return join (
43+ '' ,
44+ array_map (
45+ function ($ styleGroup ) {
46+ $ media = key ($ styleGroup );
47+ $ rules = reset ($ styleGroup );
48+
49+ return $ this ->parseMediaToString ($ media , $ rules );
50+ },
51+ $ styles
52+ )
53+ );
3854 }
3955
4056 /**
@@ -46,35 +62,69 @@ public function compileStyles()
4662 */
4763 private function parseMediaToString ($ media , array $ rules )
4864 {
65+
4966 if ($ media == '' ) {
5067 return
51- join ('' , array_map (function ($ properties , $ selector ) {
52- return $ this ->parsePropertiesToString ($ selector , $ properties );
53- }, $ rules , array_keys ($ rules ))
68+ join (
69+ '' ,
70+ array_map (
71+ function ($ rule ) {
72+ /** @var Rule $rule */
73+ return $ this ->parsePropertiesToString ($ rule ->getSelector (), $ rule ->getProperties ());
74+ },
75+ $ rules
76+ )
5477 );
5578
5679 }
5780
58- return "$ media { " . join ('' , array_map (function ($ properties , $ selector ) {
59- return $ this ->parsePropertiesToString ($ selector , $ properties );
60- }, $ rules , array_keys ($ rules ))
61- ) . "} " ;
81+ return "$ media { " .join (
82+ '' ,
83+ array_map (
84+ function ($ rule , $ selector ) {
85+ /** @var Rule $rule */
86+ return $ this ->parsePropertiesToString ($ rule ->getSelector (), $ rule ->getProperties ());
87+ },
88+ $ rules
89+ )
90+ )."} " ;
6291
6392
6493 }
6594
6695 /**
6796 *
68- * @return string
97+ * @param $selector
98+ * @param array $properties
6999 *
100+ * @return string
70101 */
71102 private function parsePropertiesToString ($ selector , array $ properties )
72103 {
73- return "$ selector { " .
74- join ('' , array_map (function (Property $ property ) {
75- return $ property ->getName () . ': ' . $ property ->getValue () . '; ' ;
76- }, $ properties )
77- ) .
104+ return "$ selector { " .
105+ join (
106+ '' ,
107+ array_map (
108+ function (Property $ property ) {
109+ return $ property ->getName ().': ' .$ property ->getValue ().'; ' ;
110+ },
111+ $ properties
112+ )
113+ ).
78114 "} " ;
79115 }
116+
117+ private function prepareStylesForProcessing ()
118+ {
119+ // Group styles by order and media
120+ $ groupedStyles = [];
121+
122+ /** @var Rule $style */
123+ foreach ($ this ->styles as $ style ) {
124+ $ groupedStyles [$ style ->getOrder ()][$ style ->getMedia ()][] = $ style ;
125+ }
126+
127+
128+ return $ groupedStyles ;
129+ }
80130}
0 commit comments