urlEncode() public static method

public static urlEncode ( $string )
Example #1
0
 /**
  * Converts given object key and the object item to the internal url.
  *
  * @static
  *
  * @param  string $objectKey
  * @param  mixed  $primaryValues
  *
  * @return string
  */
 public function toUrl($objectKey, $primaryValues)
 {
     $url = 'object://' . $objectKey . '/';
     if (is_array($primaryValues)) {
         foreach ($primaryValues as $key => $val) {
             $url .= Tools::urlEncode($val) . ',';
         }
     } else {
         return $url . Tools::urlEncode($primaryValues);
     }
     return substr($url, 0, -1);
 }
Example #2
0
 public function testUrlEncode()
 {
     $encoded = Tools::urlEncode('path/to/test');
     $this->assertEquals('path%252Fto%252Ftest', $encoded);
 }
Example #3
0
 /**
  * @ApiDoc(
  *  section="Object Browser",
  *  description="General object item list output"
  * )
  *
  * @Rest\QueryParam(name="url", requirements=".+", strict=true, description="The object url")
  * @Rest\QueryParam(name="fields", requirements=".+", description="Comma separated list of field names")
  * @Rest\QueryParam(name="returnKey", requirements=".+", description="If the result should be indexed by the pk")
  * @Rest\QueryParam(name="returnKeyAsRequested", requirements=".+", description="If the result should be indexed by the pk as requested")
  *
  * @Rest\Get("/admin/objects")
  *
  * @param ParamFetcher $paramFetcher
  *
  * @return array
  * @throws \Exception
  * @throws ClassNotFoundException
  * @throws ObjectNotFoundException
  */
 public function getItemsByUrlAction(ParamFetcher $paramFetcher)
 {
     $url = $paramFetcher->get('url');
     $fields = $paramFetcher->get('fields');
     $returnKey = filter_var($paramFetcher->get('returnKey'), FILTER_VALIDATE_BOOLEAN);
     $returnKeyAsRequested = filter_var($paramFetcher->get('returnKeyAsRequested'), FILTER_VALIDATE_BOOLEAN);
     list($objectKey, $objectIds, ) = $this->getObjects()->parseUrl($url);
     //check if we got a id
     if ($objectIds[0] === '') {
         throw new \Exception(sprintf('No id given in uri %s.', $url));
     }
     $definition = $this->getObjects()->getDefinition($objectKey);
     if (!$definition) {
         throw new ObjectNotFoundException(sprintf('Object %s can not be found.', $objectKey));
     }
     if ($definition->getExcludeFromREST()) {
         return null;
     }
     $options['extraFields'] = $fields;
     $options['permissionCheck'] = true;
     $options['fields'][] = $definition->getLabelField();
     if ($definition->getSingleItemLabelField()) {
         $options['fields'][] = $definition->getSingleItemLabelField();
     }
     $items = array();
     if (count($objectIds) == 1) {
         if ($item = $this->getObjects()->get($objectKey, $objectIds[0], $options)) {
             $items[] = $item;
         }
     } else {
         foreach ($objectIds as $primaryKey) {
             if ($item = $this->getObjects()->get($objectKey, $primaryKey, $options)) {
                 $items[] = $item;
             }
         }
     }
     if ($returnKey || $returnKeyAsRequested) {
         $res = array();
         if ($returnKeyAsRequested) {
             //map requested id to real ids
             $requestedIds = explode('/', $this->getObjects()->getCroppedObjectId($url));
             $map = array();
             foreach ($requestedIds as $id) {
                 $pk = $this->getObjects()->normalizePkString($objectKey, $id);
                 if ($pk) {
                     $map[$this->getObjects()->getObjectUrlId($objectKey, $pk) . ''] = $id;
                 }
             }
             if (is_array($items)) {
                 foreach ($items as &$item) {
                     $pk = $this->getObjects()->getObjectUrlId($objectKey, $item);
                     $res[$map[$pk . '']] = $item;
                 }
             }
         } else {
             $primaryKeys = $this->getObjects()->getPrimaries($objectKey);
             $c = count($primaryKeys);
             $firstPK = key($primaryKeys);
             if (is_array($items)) {
                 foreach ($items as &$item) {
                     if ($c > 1) {
                         $keys = array();
                         foreach ($primaryKeys as $key => $field) {
                             $keys[] = Tools::urlEncode($item[$key]);
                         }
                         $res[implode(',', $keys)] = $item;
                     } else {
                         $res[$item[$firstPK]] = $item;
                     }
                 }
             }
         }
         return $res;
     } else {
         return $items;
     }
 }