11<?php
22namespace MoodleHQ \RTLCSS \Transformation ;
33
4+ use MoodleHQ \RTLCSS \Transformation \Operation \SizeFlipper ;
45use Sabberworm \CSS \Rule \Rule ;
56use Sabberworm \CSS \Value \RuleValueList ;
7+ use Sabberworm \CSS \Value \Size ;
68
79class FlipShadow implements TransformationInterface
810{
11+ /**
12+ * @var SizeFlipper
13+ */
14+ private $ sizeFlipper ;
15+
16+ public function __construct ()
17+ {
18+ $ this ->sizeFlipper = new SizeFlipper ();
19+ }
20+
921 /**
1022 * @inheritDoc
1123 */
@@ -21,9 +33,40 @@ public function transform(Rule $rule)
2133 {
2234 $ value = $ rule ->getValue ();
2335
24- // TODO Fix upstream, each shadow should be in a RuleValueList.
36+ // Flip X offset
37+ if ($ value instanceof RuleValueList) {
38+ $ parameters = $ value ->getListComponents ();
39+ $ index = $ this ->getOffsetXIndex ($ rule );
40+ /** @var Size $oldX */
41+ $ oldX = $ parameters [$ index ];
42+ $ parameters [$ index ] = $ this ->sizeFlipper ->invertSize ($ oldX );
43+
44+ $ value ->setListComponents ($ parameters );
45+ }
46+ }
47+
48+ /**
49+ * Retrieve the index of the X offset within the rule values
50+ *
51+ * @param Rule $rule
52+ *
53+ * @return int
54+ * @throws TransformationException
55+ */
56+ private function getOffsetXIndex (Rule $ rule )
57+ {
58+ $ property = $ rule ->getRule ();
59+ $ value = $ rule ->getValue ();
2560 if ($ value instanceof RuleValueList) {
26- // negate($value->getListComponents()[0]);
61+ $ parameters = $ value ->getListComponents ();
62+ // the offset X parameter can be either the 1st or 2nd parameter
63+ foreach ([0 , 1 ] as $ i ) {
64+ if (isset ($ parameters [$ i ]) && $ parameters [$ i ] instanceof Size) {
65+ return $ i ;
66+ }
67+ }
2768 }
69+
70+ throw new TransformationException ("Invalid value for \"$ property \"" );
2871 }
2972}
0 commit comments