public function create($data) { $em = $this->getObjectManager(); $contact = new Contact(); $contact->exchangeArray($data); $em->persist($contact); $em->flush(); return new JsonModel(array('data' => $contact->getId())); }
/** * Transform request to contact. * * @param array $requestData * @param Contact $contact * * @return Contact */ public static function fromRequest(array $requestData, $contact) { $contactGetOrNull = function ($param) use($requestData) { return isset($requestData[$param]) ? $requestData[$param] : null; }; $contact->setTitle($contactGetOrNull('title'))->setName($contactGetOrNull('name'))->setEmail($contactGetOrNull('email')); $supervisorId = null; $supervisorJson = $contactGetOrNull('supervisor'); if (is_array($supervisorJson) && isset($supervisorJson['id'])) { $supervisorId = $supervisorJson['id']; } $contact->setSupervisorId($supervisorId); return $contact; }
/** * Transform contact to transient data * * @param Contact $contact * @returns array */ public static function validate(Contact $contact) { $isEmpty = function ($value) { $value = trim($value); return empty($value); }; $errors = []; if ($isEmpty($contact->getTitle())) { $errors['title'] = 'Title required'; } if ($isEmpty($contact->getName())) { $errors['name'] = 'Name required'; } if ($isEmpty($contact->getEmail())) { $errors['email'] = 'Email required'; } elseif (filter_var($contact->getEmail(), FILTER_VALIDATE_EMAIL) === false) { $errors['email'] = 'Email is invalid'; } return $errors; }
/** * Sets supervisor. * * @param Contact $supervisor * @return Contact */ public function setSupervisor($supervisor) { $this->supervisor = $supervisor; $this->setField('supervisor_contact_id', isset($supervisor) ? $supervisor->getId() : null); return $this; }
/** * Insert contact * * @param Contact $contact */ protected function contactUpdate(Contact $contact) { $db = $this->getDatabaseDriver(); $parts = array(); foreach ($contact->getFields() as $field => $fieldValue) { $parts[] = "`" . $field . "` = " . $db->quote($fieldValue); } $query = "\n UPDATE `contact`\n SET\n `updated_at` = NOW(),\n " . implode($parts, ', ') . "\n WHERE `id` = " . $db->quote($contact->getId()) . "\n LIMIT 1\n "; $db->query($query); }
public function testUpdateCanBeAccessed() { $data = array('firstName' => 'Jim', 'lastName' => 'Smith', 'middleInitial' => null, 'address1' => '123 Main St', 'address2' => 'Ste 400', 'city' => 'Pleasantville', 'state' => 'OK', 'zip' => '12345', 'zip4' => '6789', 'email' => '*****@*****.**', 'phoneNumber' => '8885551212', 'id' => 1); $contact = new Contact(); $contact->exchangeArray($data); $entityManager = $this->getMockBuilder('\\Doctrine\\Common\\Persistence\\ObjectManager')->disableOriginalConstructor()->getMock(); $entityManager->expects($this->once())->method('merge')->with($this->anything())->will($this->returnValue($contact)); $entityManager->expects($this->once())->method('flush'); $entityManager->expects($this->once())->method('find')->with('AddressBook\\Model\\Contact', 1)->will($this->returnValue($contact)); $this->controller->setObjectManager($entityManager); $this->routeMatch->setParam('id', '1'); $this->request->setMethod('put'); $this->request->setContent(json_encode($data)); $headers = new Headers(); $headers->addHeaderLine('Content-type', 'application/json'); $this->request->setHeaders($headers); $result = $this->controller->dispatch($this->request); $response = $this->controller->getResponse(); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('{"data":1}', $result->serialize()); }