예제 #1
0
 /**
  * @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();
 }
예제 #2
0
 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;
 }
예제 #3
0
 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)))));
     }
 }
예제 #5
0
 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));
 }
예제 #6
0
 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());
 }
예제 #7
0
 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);
     }
 }
예제 #8
0
 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;
 }
예제 #9
0
 public function testCreatingResultSetWithNullKeyname()
 {
     $resultSet = ResultSet::create([1, 2, 3]);
 }
예제 #10
0
 /**
  * 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();
     }
 }
예제 #11
0
파일: Custom.php 프로젝트: jdrich/drest
 /**
  * @see Drest\Service\Action\AbstractAction::execute()
  */
 public function execute()
 {
     // execute my own custom logic
     return ResultSet::create(array('name' => 'lee', 'email' => '*****@*****.**'), 'user');
 }
예제 #12
0
 /**
  * @see \DrestCommon\Representation\InterfaceRepresentation::write()
  */
 public function write(ResultSet $data)
 {
     $dataArray = $data->toArray();
     $this->formatData($dataArray);
     $this->data = json_encode($dataArray);
 }
예제 #13
0
 /**
  * @expectedException \Exception
  */
 public function testDataWithNoToStringObject()
 {
     $obj = new \StdClass();
     $data = array('obj' => $obj);
     $representation = new Xml();
     $representation->write(ResultSet::create($data, 'user'));
 }
예제 #14
0
 /**
  * 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);
 }