/** * Adds one or many data entries for person with given $id * Expects JSON data with below structure * * [ * {"type": "custom", "label": "a label nr 1", "value": "a value1"}, * {"type": "custom", "label": "a label nr 2", "value": "a value2"}, * {"type": "custom", "label": "a label nr 3", "value": "a value3"} * ] * * @param string $id * @return Response */ public function apiPostAction($id) { $person = $this->personRepository->find(PersonId::fromString($id)); if (!$person) { abort(404); } $newData = Input::get(); foreach ($newData as $dataRow) { $person->addData(DataType::fromString($dataRow['type']), $dataRow['label'], $dataRow['value']); } $this->personRepository->update($person); return response()->json($person); }
/** * @param array $data * @return Person */ public static function fromDB(array $data) { $person = new Person(PersonId::fromString($data['id']), $data['name']); // add canonical when it's available if (isset($data['canonical'])) { $person = new Person(PersonId::fromString($data['id']), $data['name'], $data['canonical']); } foreach ($data['data'] as $entry) { $person->data[] = new Data(DataId::fromString($entry['id']), DataType::fromString($entry['type']), $entry['label'], $entry['value']); } foreach ($data['tags'] as $entry) { $person->tags[] = Tag::fromString($entry['tag']); } return $person; }
/** * @inheritdoc */ public function jsonSerialize() { return ['type' => $this->type->toString(), 'label' => $this->label, 'value' => (string) $this->value]; }