@@ -30,6 +30,13 @@ final class Adjustment {
3030 */
3131 protected $ amount ;
3232
33+ /**
34+ * The adjustment percentage.
35+ *
36+ * @var string
37+ */
38+ protected $ percentage ;
39+
3340 /**
3441 * The source identifier of the adjustment.
3542 *
@@ -67,13 +74,20 @@ public function __construct(array $definition) {
6774 if (empty ($ types [$ definition ['type ' ]])) {
6875 throw new \InvalidArgumentException (sprintf ('%s is an invalid adjustment type. ' , $ definition ['type ' ]));
6976 }
77+ if (!empty ($ definition ['percentage ' ])) {
78+ if (is_float ($ definition ['percentage ' ])) {
79+ throw new \InvalidArgumentException (sprintf ('The provided percentage "%s" must be a string, not a float. ' , $ definition ['percentage ' ]));
80+ }
81+ if (!is_numeric ($ definition ['percentage ' ])) {
82+ throw new \InvalidArgumentException (sprintf ('The provided percentage "%s" is not a numeric value. ' , $ definition ['percentage ' ]));
83+ }
84+ }
7085
7186 $ this ->type = $ definition ['type ' ];
7287 $ this ->label = $ definition ['label ' ];
7388 $ this ->amount = $ definition ['amount ' ];
74- if (!empty ($ definition ['source_id ' ])) {
75- $ this ->sourceId = $ definition ['source_id ' ];
76- }
89+ $ this ->percentage = !empty ($ definition ['percentage ' ]) ? $ definition ['percentage ' ] : NULL ;
90+ $ this ->sourceId = !empty ($ definition ['source_id ' ]) ? $ definition ['source_id ' ] : NULL ;
7791 $ this ->included = !empty ($ definition ['included ' ]);
7892 }
7993
@@ -97,16 +111,6 @@ public function getLabel() {
97111 return $ this ->label ;
98112 }
99113
100- /**
101- * Get the source identifier.
102- *
103- * @return string
104- * The source identifier.
105- */
106- public function getSourceId () {
107- return $ this ->sourceId ;
108- }
109-
110114 /**
111115 * Gets the adjustment amount.
112116 *
@@ -121,7 +125,7 @@ public function getAmount() {
121125 * Gets whether the adjustment is positive.
122126 *
123127 * @return bool
124- * TRUE if the adjustmnet is positive, FALSE otherwise.
128+ * TRUE if the adjustment is positive, FALSE otherwise.
125129 */
126130 public function isPositive () {
127131 return $ this ->amount ->getNumber () >= 0 ;
@@ -137,6 +141,27 @@ public function isNegative() {
137141 return $ this ->amount ->getNumber () < 0 ;
138142 }
139143
144+ /**
145+ * Gets the adjustment percentage.
146+ *
147+ * @return string|null
148+ * The percentage as a decimal. For example, "0.2" for a 20% adjustment.
149+ * Otherwise NULL, if the adjustment was not calculated from a percentage.
150+ */
151+ public function getPercentage () {
152+ return $ this ->percentage ;
153+ }
154+
155+ /**
156+ * Get the source identifier.
157+ *
158+ * @return string
159+ * The source identifier.
160+ */
161+ public function getSourceId () {
162+ return $ this ->sourceId ;
163+ }
164+
140165 /**
141166 * Gets whether the adjustment is included in the base price.
142167 *
0 commit comments