/** * @see Drest\Writer\Writer::write() */ public function write(ResultSet $data) { $this->xml = new \DomDocument('1.0', 'UTF-8'); $this->xml->formatOutput = true; $dataArray = $data->toArray(); if (key($dataArray) === 0) { // If there is no key, we need to use a default $this->xml->appendChild($this->convertArrayToXml('result', $dataArray)); } else { $this->xml->appendChild($this->convertArrayToXml(key($dataArray), $dataArray[key($dataArray)])); } $this->data = $this->xml->saveXML(); }
public function execute() { $matchedRoute = $this->getMatchedRoute(); $classMetaData = $matchedRoute->getClassMetaData(); $elementName = $classMetaData->getEntityAlias(); $em = $this->getEntityManager(); $qb = $em->createQueryBuilder()->select($elementName)->from($classMetaData->getClassName(), $elementName); foreach ($matchedRoute->getRouteParams() as $key => $value) { $qb->andWhere($elementName . '.' . $key . ' = :' . $key); $qb->setParameter($key, $value); } try { $object = $qb->getQuery()->getSingleResult(ORM\Query::HYDRATE_OBJECT); } catch (\Exception $e) { return $this->handleError($e, Response::STATUS_CODE_404); } $this->runHandle($object); // Attempt to save the modified resource try { $em->persist($object); $em->flush($object); $location = $matchedRoute->getOriginLocation($object, $this->getRequest()->getUrl(), $this->getEntityManager()); $this->getResponse()->setStatusCode(Response::STATUS_CODE_200); $resultSet = ResultSet::create(array('location' => $location ? $location : 'unknown'), 'response'); } catch (\Exception $e) { return $this->handleError($e, Response::STATUS_CODE_500); } return $resultSet; }
public function execute() { $matchedRoute = $this->getMatchedRoute(); $classMetaData = $matchedRoute->getClassMetaData(); $elementName = $classMetaData->getEntityAlias(); $em = $this->getEntityManager(); $qb = $em->createQueryBuilder()->select($elementName)->from($classMetaData->getClassName(), $elementName); foreach ($matchedRoute->getRouteParams() as $key => $value) { $qb->andWhere($elementName . '.' . $key . ' = :' . $key); $qb->setParameter($key, $value); } try { $object = $qb->getQuery()->getSingleResult(ORM\Query::HYDRATE_OBJECT); } catch (\Exception $e) { return $this->handleError($e, Response::STATUS_CODE_404); } try { $em->remove($object); $em->flush(); $this->getResponse()->setStatusCode(Response::STATUS_CODE_200); return ResultSet::create(array('successfully deleted'), 'response'); } catch (\Exception $e) { return $this->handleError($e, Response::STATUS_CODE_500); } }
/** * update the representation to match the data contained within a client data object * - This will call the write method that will store its representation in the $data array */ public function update($object) { if (is_object($object)) { $objectVars = get_object_vars($object); $this->repIntoArray($objectVars); $this->write(ResultSet::create($objectVars, strtolower(implode('', array_slice(explode('\\', get_class($object)), -1))))); } }
public function testPostElementRequest() { $dm = $this->_getDrestManager($this->_em); $representation = new \DrestCommon\Representation\Json(); // id's added for comparison but not used for persistence (see DrestTests\Entities\Typical\User::populatePost()) $user = array('id' => 1, 'username' => 'leedavis81', 'email_address' => '*****@*****.**', 'phone_numbers' => array(array('id' => 1, 'number' => '02087856589'), array('id' => 2, 'number' => '07584565445'), array('id' => 3, 'number' => '02078545896'))); $representation->write(\DrestCommon\ResultSet::create($user, 'user')); $request = \Symfony\Component\HttpFoundation\Request::create('/user', 'POST', [], [], [], array('HTTP_CONTENT_TYPE' => $representation->getContentType()), $representation->__toString()); $response = $dm->dispatch($request); $this->assertEquals(201, $response->getStatusCode()); $this->assertEquals($dm->getRequest()->getUrl() . '/user/1', $response->getHttpHeader('Location')); // Ensure this item exists in persistence $query = $this->_em->createQuery('SELECT u, p FROM DrestTests\\Entities\\Typical\\User u JOIN u.phone_numbers p'); $this->assertEquals($user, $query->getSingleResult(\Doctrine\ORM\Query::HYDRATE_ARRAY)); }
public function testPutElementRequest() { $dm = $this->_getDrestManager($this->_em); $representation = new \DrestCommon\Representation\Json(); $user = new \DrestTests\Entities\CMS\User(); $user->setEmailAddress('*****@*****.**'); $user->setUsername('leedavis81'); $this->_em->persist($user); $this->_em->flush(); $this->_em->refresh($user); $putEmail = '*****@*****.**'; $representation->write(\DrestCommon\ResultSet::create(array('email_address' => $putEmail), 'user')); $request = \Symfony\Component\HttpFoundation\Request::create('/user/' . $user->getId(), 'PUT', array(), array(), array(), array('HTTP_CONTENT_TYPE' => $representation->getContentType()), $representation->__toString()); $response = $dm->dispatch($request); $this->assertEquals(200, $response->getStatusCode()); $putUser = $this->_em->find('DrestTests\\Entities\\CMS\\User', $user->getId()); $this->assertEquals($putEmail, $putUser->getEmailAddress()); }
public function execute() { $matchedRoute = $this->getMatchedRoute(); $classMetaData = $matchedRoute->getClassMetaData(); $elementName = $classMetaData->getEntityAlias(); $em = $this->getEntityManager(); $qb = $em->createQueryBuilder()->delete($classMetaData->getClassName(), $elementName); foreach ($matchedRoute->getRouteParams() as $key => $value) { $qb->andWhere($elementName . '.' . $key . ' = :' . $key); $qb->setParameter($key, $value); } try { $qb->getQuery()->execute(); $this->getResponse()->setStatusCode(Response::STATUS_CODE_200); return ResultSet::create(['successfully deleted'], 'response'); } catch (\Exception $e) { return $this->handleError($e, Response::STATUS_CODE_500); } }
public function execute() { $classMetaData = $this->getMatchedRoute()->getClassMetaData(); $em = $this->getEntityManager(); $entityClass = $classMetaData->getClassName(); $object = new $entityClass(); $this->runHandle($object); try { $em->persist($object); $em->flush($object); $this->getResponse()->setStatusCode(Response::STATUS_CODE_201); if (($location = $this->getMatchedRoute()->getOriginLocation($object, $this->getRequest()->getUrl(), $this->getEntityManager())) !== false) { $this->getResponse()->setHttpHeader('Location', $location); } $resultSet = ResultSet::create(['location' => $location ? $location : 'unknown'], 'response'); } catch (\Exception $e) { return $this->handleError($e, Response::STATUS_CODE_500); } return $resultSet; }
public function testCreatingResultSetWithNullKeyname() { $resultSet = ResultSet::create([1, 2, 3]); }
/** * Configure the expose object to filter out fields that are not allowed to be use by the client. * Unlike the configuring of the Pull request, this function will return the formatted array in a ResultSet object * This is only applicable for a HTTP push (POST/PUT/PATCH) call * @param array $pushed - the data push on the request * @throws \Drest\DrestException * @return \DrestCommon\ResultSet * * @todo: this should follow the same pattern as configurePullRequest */ public function configurePushRequest($pushed) { // Offset the array by one of it has a string key and is size of 1 if (sizeof($pushed) == 1 && is_string(key($pushed))) { $rootKey = key($pushed); $pushed = $this->filterPushExpose($pushed[key($pushed)], $this->fields); return ResultSet::create($pushed, $rootKey); } else { throw DrestException::unableToHandleACollectionPush(); } }
/** * @see Drest\Service\Action\AbstractAction::execute() */ public function execute() { // execute my own custom logic return ResultSet::create(array('name' => 'lee', 'email' => '*****@*****.**'), 'user'); }
/** * @see \DrestCommon\Representation\InterfaceRepresentation::write() */ public function write(ResultSet $data) { $dataArray = $data->toArray(); $this->formatData($dataArray); $this->data = json_encode($dataArray); }
/** * @expectedException \Exception */ public function testDataWithNoToStringObject() { $obj = new \StdClass(); $data = array('obj' => $obj); $representation = new Xml(); $representation->write(ResultSet::create($data, 'user')); }
/** * Method used to write to the $data array. * - wraps results in a single entry array keyed by entity name. * Eg array(user1, user2) becomes array('users' => array(user1, user2)) - this is useful for a more descriptive output of collection resources * - Removes any addition expose fields required for a partial DQL query * @param array $data - the data fetched from the database * @param string $keyName - the key name to use to wrap the data in. If null will attempt to pluralise the entity name on collection request, or singularize on single element request * @return ResultSet $data */ public function createResultSet(array $data, $keyName = null) { $matchedRoute = $this->getMatchedRoute(); $classMetaData = $matchedRoute->getClassMetaData(); // Recursively remove any additionally added pk fields ($data must be a single record hierarchy. Iterate if we're getting a collection) if ($matchedRoute->isCollection()) { for ($x = 0; $x < sizeof($data); $x++) { $this->removeAddedKeyFields($this->addedKeyFields, $data[$x]); } } else { $this->removeAddedKeyFields($this->addedKeyFields, $data); } if (is_null($keyName)) { reset($data); if (sizeof($data) === 1 && is_string(key($data))) { // Use the single keyed array as the result set key $keyName = key($data); $data = $data[key($data)]; } else { $keyName = $matchedRoute->isCollection() ? $classMetaData->getCollectionName() : $classMetaData->getElementName(); } } return ResultSet::create($data, $keyName); }