/** * @test */ public function camelCaseToSnakeCaseCorrectness() { self::assertSame('hello_world', Utils::camelCaseToSnakeCase('HelloWorld')); self::assertSame('hello_world', Utils::camelCaseToSnakeCase('Hello_world')); self::assertSame('hello_world', Utils::camelCaseToSnakeCase('hello_world')); self::assertSame('helloworld', Utils::camelCaseToSnakeCase('helloworld')); self::assertSame('0', Utils::camelCaseToSnakeCase('0')); self::assertSame('', Utils::camelCaseToSnakeCase('')); }
/** * This magic method handles the magic getter and setter methods for the structure properties. * Property name will be extracted from the getter/setter method name and passed to the * {BaseStructure::get}/{BaseStructure::set} methods; the name of the magic methods should begin * with get/set (for getter and setter respectively) prefix followed by the name of the property; * the name of teh property transformed to snake case will be passed as the first argument to * delegate method and all of the other argument passed to the magic method will be passed to the * delegate method. * * For ex. * $obj->getHelloWorld() will call the $obj->get('hello_world') and return it's return value. * $obj->setHelloWorld('Die') will call the $obj->set('hello_world', 'Die') and return it's return value. * * @param string $name * @param array $args * @return mixed the property value if the getter called or the {BaseStructure} instance if the setter called. * @throws \BadMethodCallException if the magic method does not starts with the get or set prefix. */ public function __call($name, $args) { $type = null; $sub = substr($name, 0, 3); if ($sub === 'get') { $type = 'get'; } elseif ($sub === 'set') { $type = 'set'; } if ($type !== null) { $key = Utils::camelCaseToSnakeCase(substr($name, strlen($type))); array_unshift($args, $key); return call_user_func_array([$this, $type], $args); } throw new \BadMethodCallException(sprintf('Only getter and setter magic method types can be handled be structure. ' . 'Check the correctness of calling method with name \'%s\' from class \'%s\'.', $name, self::class)); }
/** * @param $path * @param $params * @param string $httpMethod * @param bool|true $apiCall * @param int $apiVer * @param bool|true $keyRequired * @return array * * @throws \RuntimeException * @throws \InvalidArgumentException * @throws InvalidResponseException */ protected function request($path, $params = [], $httpMethod = 'GET', $apiCall = true, $apiVer = 1, $keyRequired = true) { if ($keyRequired && $apiCall) { $params['key'] = $this->getApiKey(); } switch ($httpMethod) { case 'GET': case 'POST': break; default: throw new \InvalidArgumentException('Invalid http method. Only GET and POST allowed'); } $reqUrl = "{$this->getUrl()}/{$path}" . ($apiCall ? '/v' . Utils::apiVersionToString($apiVer) : null); /** * @var $response \GuzzleHttp\Psr7\Response */ $response = $this->client->request($httpMethod, $reqUrl, [$httpMethod === "GET" ? "query" : "form_params" => $params, 'cookies' => $this->cookies]); $statusCode = $response->getStatusCode(); $contType = $response->getHeaderLine('Content-Type'); if ($statusCode === 200 || !$apiCall) { if (strpos($contType, 'application/json') === 0) { return json_decode($response->getBody()->getContents(), true); } elseif (strpos($contType, 'text/xml') === 0) { $xml = simplexml_load_string($response->getBody()); // filtering plain objects foreach ($xml as $name => $val) { if ($xml[$name] instanceof \SimpleXMLElement) { unset($xml[$name]); } } return $xml; } return $response->getBody(); } throw new InvalidResponseException("Failed to handle response received from url {$reqUrl}. Response code: {$statusCode}"); }