Skip to content

Commit ff0581b

Browse files
committed
Update Controller/Result/Json setData
- deprecate this method to encourage usage of other methods in this class, - be stricter on types passed into this method,
1 parent 7e46cea commit ff0581b

File tree

1 file changed

+33
-8
lines changed
  • lib/internal/Magento/Framework/Controller/Result

1 file changed

+33
-8
lines changed

lib/internal/Magento/Framework/Controller/Result/Json.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,45 @@ public function __construct(
5050
/**
5151
* Set json data
5252
*
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
5757
* @throws \InvalidArgumentException
58+
* @throws \Magento\Framework\Exception\LocalizedException
5859
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
60+
* @deprecated
5961
*/
6062
public function setData($data, $cycleCheck = false, $options = [])
6163
{
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);
6670
}
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));
6792
return $this;
6893
}
6994

0 commit comments

Comments
 (0)