public function transform(array $data) { $code = isset($data[$this->httpCodeKey]) ? $data[$this->httpCodeKey] : 200; $arrayResponse = ArrayUtils::fromObject($data); $response = new JsonResponse($arrayResponse, $code); return $response; }
public function testFromObject() { $this->assertEquals(ArrayUtils::fromObject(array()), array()); $this->assertEquals(ArrayUtils::fromObject(new \stdClass()), array()); $intKeysObject = new \stdClass(); $intKeysObject->{0} = 'a'; $intKeysObject->{1} = 'b'; $intKeysObject->{'2'} = 'c'; $intKeysObject->{'3'} = 'd'; $this->assertEquals(ArrayUtils::fromObject($intKeysObject), $this->_getArrayPreset('int_keys')); $associativeObject = new \stdClass(); $associativeObject->lorem = 'qwerty'; $associativeObject->ipsum = 'asd'; $associativeObject->dolor = 'qwe'; $associativeObject->sit = 'tl;dr'; $associativeObject->amet = 69; $this->assertEquals(ArrayUtils::fromObject($associativeObject), $this->_getArrayPreset('associative')); $item1 = new \stdClass(); $item1->id = 1; $item1->name = 'lorem'; $item1->categoryId = 5; $item1->date = '2013.07.08'; $item2 = new \stdClass(); $item2->id = 2; $item2->name = 'ipsum'; $item2->categoryId = 3; $item2->date = '2013.07.07'; $item3 = new \stdClass(); $item3->id = 5; $item3->name = 'dolor'; $item3->categoryId = 1; $item3->date = '2012.07.08'; $item4 = new \stdClass(); $item4->id = 6; $item4->name = 'sit'; $item4->categoryId = 3; $item4->date = '2013.12.08'; $item5 = new \stdClass(); $item5->id = 9; $item5->name = 'amet'; $item5->categoryId = 5; $item5->date = '2013.10.14'; $collection = new \stdClass(); $collection->{0} = $item1; $collection->{1} = $item2; $collection->{2} = $item3; $collection->{3} = $item4; $collection->{4} = $item5; $this->assertEquals(ArrayUtils::fromObject($collection), $this->_getArrayPreset('2D_collection_5')); $collectionNamed = new \stdClass(); $collectionNamed->lorem = $item1; $collectionNamed->ipsum = $item2; $collectionNamed->dolor = $item3; $collectionNamed->sit = $item4; $collectionNamed->amet = $item5; $this->assertEquals(ArrayUtils::fromObject($collectionNamed), $this->_getArrayPreset('2D_collection_5_named')); $collectionNamedOtherClass = new EmptyClass(); $collectionNamedOtherClass->lorem = $item1; $collectionNamedOtherClass->ipsum = $item2; $collectionNamedOtherClass->dolor = $item3; $collectionNamedOtherClass->sit = $item4; $collectionNamedOtherClass->amet = $item5; $this->assertEquals(ArrayUtils::fromObject($collectionNamedOtherClass), $this->_getArrayPreset('2D_collection_5_named')); $parentArray = array('key1' => 'val1', 'key2' => 'val2'); $this->assertEquals(ArrayUtils::fromObject($collectionNamedOtherClass, $parentArray), array_merge(array('key1' => 'val1', 'key2' => 'val2'), $this->_getArrayPreset('2D_collection_5_named'))); $this->assertEquals(ArrayUtils::fromObject($associativeObject, array(), array('lorem', 'ipsum')), array('lorem' => 'qwerty', 'ipsum' => 'asd')); $collectionArray = $this->_getArrayPreset('2D_collection_5'); $collectionArray = ArrayUtils::keyRemove($collectionArray, 'categoryId'); $collectionArray = ArrayUtils::keyRemove($collectionArray, 'date'); $collectionOfObjects = array($item1, $item2, $item3, $item4, $item5); $this->assertEquals(ArrayUtils::fromObject($collectionOfObjects, array(), array('id', 'name')), $collectionArray); $collectionArray = array(); foreach ($this->_getArrayPreset('2D_collection_5') as $item) { $collectionArray[] = new ToArrayClass($item['id'], $item['name'], $item['categoryId'], $item['date']); } $this->assertEquals(ArrayUtils::fromObject($collectionArray), $this->_getArrayPreset('2D_collection_5')); $collectionArray = array(); foreach ($this->_getArrayPreset('2D_collection_5') as $item) { $collectionArray[] = new ToArrayWrongClass($item['id'], $item['name'], $item['categoryId'], $item['date']); } $this->assertEquals(ArrayUtils::fromObject($collectionArray), $this->_getArrayPreset('2D_collection_5')); $someObj = new \stdClass(); $someObj->id = 45; $someObj->title = 'Lorem ipsum'; $this->assertEquals(array(123, 'title' => 'Lipsum.com', 'obj' => array('id' => 45, 'title' => 'Lorem ipsum'), 'enable' => false), ArrayUtils::fromObject(array(123, 'title' => 'Lipsum.com', 'obj' => $someObj, 'enable' => false))); }
/** * Transforms the whole data to JSON format. * * @return string */ public function toJson() { return json_encode(ArrayUtils::fromObject($this->data)); }
/** * Returns a string containing JavaScript code that will log all the arguments into browser's JavaScript console. * * Attempts to convert all objects into arrays. * * There is a shortcut alias function `\MD\console_string_dump()`. * * @param mixed $variable1 Variable to be dumped. * @param mixed $variable2 Another variable to be dumped. * @param mixed $variable3 Another variable to be dumped. * @param ... * @return string */ public static function consoleStringDump() { $arguments = func_get_args(); $output = '(function(w,u) {'; $output .= 'if(w.console===u)return;'; foreach ($arguments as $variable) { $dump = null; if (is_object($variable)) { $dump = json_encode(ArrayUtils::fromObject($variable)); } elseif (is_bool($variable)) { $dump = '"(bool) ' . ($variable ? 'true' : 'false') . '"'; } else { $dump = json_encode($variable); } $output .= 'w.console.log(' . $dump . ');'; } $output .= '})(window);'; return $output; }