public function testToJson() { $this->transfer->shouldReceive('getProperties')->andReturn(['prop' => ['is_multiple' => false], 'not_in_data' => ['is_multiple' => false]]); $data = ['prop' => 'hello']; $this->transfer->fromArray($data); $result = json_encode($this->transfer); $this->assertJson($result); }
/** * @param array $data */ public function fromArray(array $data) { foreach ($data as $name => $rawValue) { try { $this->validateProperty($name); } catch (UnexpectedPropertyException $e) { continue; } $this->validateMulti($name, $rawValue); $type = $this->getCustomType($name); if (null === $type) { $value = $rawValue; } else { if (!$this->isMulti($name)) { $value = is_null($rawValue) ? null : AbstractTransfer::makeTransfer($type, $rawValue); } else { $value = []; foreach ($rawValue as $item) { $value[] = AbstractTransfer::makeTransfer($type, $item); } } } $this->data[$name] = $value; } }
/** * @return AbstractTransfer|null */ private function getCurrent() { // Check transfer first if (isset($this->transferData[$this->position])) { return $this->transferData[$this->position]; } // Check raw data next, build transfer if (isset($this->rawData[$this->position])) { $this->transferData[$this->position] = AbstractTransfer::makeTransfer($this->transferClass, $this->rawData[$this->position]); return $this->transferData[$this->position]; } return null; }