[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/tobscure/json-api/src/ -> LinksTrait.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of JSON-API.
   5   *
   6   * (c) Toby Zerner <[email protected]>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Tobscure\JsonApi;
  13  
  14  trait LinksTrait
  15  {
  16      /**
  17       * The links array.
  18       *
  19       * @var array
  20       */
  21      protected $links;
  22  
  23      /**
  24       * Get the links.
  25       *
  26       * @return array
  27       */
  28      public function getLinks()
  29      {
  30          return $this->links;
  31      }
  32  
  33      /**
  34       * Set the links.
  35       *
  36       * @param array $links
  37       *
  38       * @return $this
  39       */
  40      public function setLinks(array $links)
  41      {
  42          $this->links = $links;
  43  
  44          return $this;
  45      }
  46  
  47      /**
  48       * Add a link.
  49       *
  50       * @param string $key
  51       * @param string $value
  52       *
  53       * @return $this
  54       */
  55      public function addLink($key, $value)
  56      {
  57          $this->links[$key] = $value;
  58  
  59          return $this;
  60      }
  61  
  62      /**
  63       * Add pagination links (first, prev, next, and last).
  64       *
  65       * @param string $url The base URL for pagination links.
  66       * @param array $queryParams The query params provided in the request.
  67       * @param int $offset The current offset.
  68       * @param int $limit The current limit.
  69       * @param int|null $total The total number of results, or null if unknown.
  70       *
  71       * @return void
  72       */
  73      public function addPaginationLinks($url, array $queryParams, $offset, $limit, $total = null)
  74      {
  75          if (isset($queryParams['page']['number'])) {
  76              $offset = floor($offset / $limit) * $limit;
  77          }
  78  
  79          $this->addPaginationLink('first', $url, $queryParams, 0, $limit);
  80  
  81          if ($offset > 0) {
  82              $this->addPaginationLink('prev', $url, $queryParams, max(0, $offset - $limit), $limit);
  83          }
  84  
  85          if ($total === null || $offset + $limit < $total) {
  86              $this->addPaginationLink('next', $url, $queryParams, $offset + $limit, $limit);
  87          }
  88  
  89          if ($total) {
  90              $this->addPaginationLink('last', $url, $queryParams, floor(($total - 1) / $limit) * $limit, $limit);
  91          }
  92      }
  93  
  94      /**
  95       * Add a pagination link.
  96       *
  97       * @param string $name The name of the link.
  98       * @param string $url The base URL for pagination links.
  99       * @param array $queryParams The query params provided in the request.
 100       * @param int $offset The offset to link to.
 101       * @param int $limit The current limit.
 102       *
 103       * @return void
 104       */
 105      protected function addPaginationLink($name, $url, array $queryParams, $offset, $limit)
 106      {
 107          if (! isset($queryParams['page']) || ! is_array($queryParams['page'])) {
 108              $queryParams['page'] = [];
 109          }
 110  
 111          $page = &$queryParams['page'];
 112  
 113          if (isset($page['number'])) {
 114              $page['number'] = floor($offset / $limit) + 1;
 115  
 116              if ($page['number'] <= 1) {
 117                  unset($page['number']);
 118              }
 119          } else {
 120              $page['offset'] = $offset;
 121  
 122              if ($page['offset'] <= 0) {
 123                  unset($page['offset']);
 124              }
 125          }
 126  
 127          if (isset($page['limit'])) {
 128              $page['limit'] = $limit;
 129          }
 130  
 131          $queryString = http_build_query($queryParams);
 132  
 133          $this->addLink($name, $url.($queryString ? '?'.$queryString : ''));
 134      }
 135  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer