Example #1
0
 /**
  * __call
  * Magic Method for fetch on routes
  *
  * @param $method - a string whose title case is a class in the
  *                  Yabacon\Paystack\Routes namespace implementing
  *                  Yabacon\Paystack\Contracts\RouteInterface
  * @param $args - arguments sent to the magic method
  *
  * @return the result of calling /{route}/get on the api
  *
  * @access public
  * @see    Yabacon\Paystack\Routes\Router
  * @since  1.0
  */
 public function __call($method, $args)
 {
     /*
     attempt to call fetch when the route is called directly
     translates to /{root}/{get}/{id}
     */
     if (in_array($method, $this->routes, true) && count($args) === 1) {
         $route = new Router($method, $this);
         // no params, just one arg... the id
         $args = [[], [Router::ID_KEY => $args[0]]];
         return $route->__call('fetch', $args);
     }
     // Not found is it plural?
     $is_plural = strripos($method, 's') === strlen($method) - 1;
     $singular_form = substr($method, 0, strlen($method) - 1);
     if ($is_plural && in_array($singular_form, $this->routes, true)) {
         $route = new Router($singular_form, $this);
         if (count($args) === 1 && is_array($args[0]) || count($args) === 0) {
             return $route->__call('getList', $args);
         }
     }
     // Should never get here
     throw new \InvalidArgumentException('Route "' . $method . '" can only accept ' . ($is_plural ? 'an optional array of paging arguments (perPage, page)' : 'an id or code') . '.');
 }