@@ -50,20 +50,45 @@ public function __construct(
50
50
/**
51
51
* Set json data
52
52
*
53
- * @param mixed $data
54
- * @param boolean $cycleCheck Optional; whether or not to check for object recursion; off by default
55
- * @param array $options Additional options used during encoding
56
- * @return $this
53
+ * @param array|string|\Magento\Framework\DataObject $data
54
+ * @param bool $cycleCheck
55
+ * @param array $options
56
+ * @return Json
57
57
* @throws \InvalidArgumentException
58
+ * @throws \Magento\Framework\Exception\LocalizedException
58
59
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
60
+ * @deprecated
59
61
*/
60
62
public function setData ($ data , $ cycleCheck = false , $ options = [])
61
63
{
62
- if (is_object ($ data ) && method_exists ($ data , 'toJson ' )) {
63
- $ this ->json = $ data ->toJson ();
64
- } else {
65
- $ this ->json = $ this ->serializer ->serialize ($ data );
64
+ if ($ data instanceof \Magento \Framework \DataObject) {
65
+ return $ this ->setArrayData ($ data ->toArray ());
66
+ }
67
+
68
+ if (is_array ($ data )) {
69
+ return $ this ->setArrayData ($ data );
66
70
}
71
+
72
+ //Should we validate the json here?
73
+ if (is_string ($ data )) {
74
+ return $ this ->setJsonData ($ data );
75
+ }
76
+
77
+ throw new \Magento \Framework \Exception \LocalizedException (
78
+ new \Magento \Framework \Phrase ('Invalid argument type ' )
79
+ );
80
+ }
81
+
82
+ /**
83
+ * Should cover this with a test or 2
84
+ *
85
+ * @param array $data
86
+ * @return $this
87
+ * @throws \InvalidArgumentException
88
+ */
89
+ public function setArrayData (array $ data )
90
+ {
91
+ $ this ->setJsonData ($ this ->serializer ->serialize ($ data ));
67
92
return $ this ;
68
93
}
69
94
0 commit comments