diff --git a/composer.json b/composer.json index 949535b..a0dc609 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,6 @@ ], "require": { "php": ">=5.6.0", - "psr/log": "1.0.*", "guzzlehttp/psr7": "1.4.*", "php-http/client-implementation": "@stable" }, diff --git a/src/ACL/ACLClient.php b/src/ACL/ACLClient.php index e3ea41a..bd4a611 100644 --- a/src/ACL/ACLClient.php +++ b/src/ACL/ACLClient.php @@ -25,8 +25,7 @@ * Class ACLClient * @package DCarbone\PHPConsulAPI\ACL */ -class ACLClient extends AbstractClient -{ +class ACLClient extends AbstractClient { /** * @param \DCarbone\PHPConsulAPI\ACL\ACLEntry $acl * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions @@ -36,21 +35,22 @@ class ACLClient extends AbstractClient * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function create(ACLEntry $acl, WriteOptions $writeOptions = null) - { + public function create(ACLEntry $acl, WriteOptions $writeOptions = null) { $r = new Request('put', 'v1/acl/create', $this->c, $acl); $r->setWriteOptions($writeOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return ['', null, $err]; + } $wm = $this->buildWriteMeta($duration); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return ['', $wm, $err]; + } return [$data, $wm, null]; } @@ -63,15 +63,15 @@ public function create(ACLEntry $acl, WriteOptions $writeOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function update(ACLEntry $acl, WriteOptions $writeOptions = null) - { + public function update(ACLEntry $acl, WriteOptions $writeOptions = null) { $r = new Request('PUT', 'v1/acl/update', $this->c, $acl); $r->setWriteOptions($writeOptions); list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -84,15 +84,15 @@ public function update(ACLEntry $acl, WriteOptions $writeOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function destroy($id, WriteOptions $writeOptions = null) - { + public function destroy($id, WriteOptions $writeOptions = null) { $r = new Request('PUT', sprintf('v1/acl/destroy/%s', $id), $this->c); $r->setWriteOptions($writeOptions); list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -106,21 +106,22 @@ public function destroy($id, WriteOptions $writeOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function cloneACL($id, WriteOptions $writeOptions = null) - { + public function cloneACL($id, WriteOptions $writeOptions = null) { $r = new Request('PUT', sprintf('v1/acl/clone/%s', $id), $this->c); $r->setWriteOptions($writeOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return ['', null, $err]; + } $wm = $this->buildWriteMeta($duration); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return ['', $wm, $err]; + } return [$data, $wm, null]; } @@ -134,25 +135,25 @@ public function cloneACL($id, WriteOptions $writeOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function info($id, QueryOptions $queryOptions = null) - { + public function info($id, QueryOptions $queryOptions = null) { $r = new Request('GET', sprintf('v1/acl/info/%s', $id), $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $entries = []; - foreach($data as $entry) - { + foreach ($data as $entry) { $entries[] = new ACLEntry($entry); } @@ -167,26 +168,26 @@ public function info($id, QueryOptions $queryOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function listACLs(QueryOptions $queryOptions = null) - { + public function listACLs(QueryOptions $queryOptions = null) { $r = new Request('GET', 'v1/acl/list', $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $entries = []; - foreach($data as $entry) - { + foreach ($data as $entry) { $entries[] = new ACLEntry($entry); } diff --git a/src/ACL/ACLEntry.php b/src/ACL/ACLEntry.php index 3349322..a73b7b6 100644 --- a/src/ACL/ACLEntry.php +++ b/src/ACL/ACLEntry.php @@ -22,8 +22,7 @@ * Class ACLEntry * @package DCarbone\PHPConsulAPI\ACL */ -class ACLEntry extends AbstractModel -{ +class ACLEntry extends AbstractModel { /** @var int */ public $CreateIndex = 0; /** @var int */ @@ -40,96 +39,84 @@ class ACLEntry extends AbstractModel /** * @return int */ - public function getCreateIndex() - { + public function getCreateIndex() { return $this->CreateIndex; } /** * @param int $CreateIndex */ - public function setCreateIndex($CreateIndex) - { + public function setCreateIndex($CreateIndex) { $this->CreateIndex = $CreateIndex; } /** * @return int */ - public function getModifyIndex() - { + public function getModifyIndex() { return $this->ModifyIndex; } /** * @param int $ModifyIndex */ - public function setModifyIndex($ModifyIndex) - { + public function setModifyIndex($ModifyIndex) { $this->ModifyIndex = $ModifyIndex; } /** * @return string */ - public function getID() - { + public function getID() { return $this->ID; } /** * @param string $ID */ - public function setID($ID) - { + public function setID($ID) { $this->ID = $ID; } /** * @return string */ - public function getName() - { + public function getName() { return $this->Name; } /** * @param string $Name */ - public function setName($Name) - { + public function setName($Name) { $this->Name = $Name; } /** * @return string */ - public function getType() - { + public function getType() { return $this->Type; } /** * @param string $Type */ - public function setType($Type) - { + public function setType($Type) { $this->Type = $Type; } /** * @return string */ - public function getRules() - { + public function getRules() { return $this->Rules; } /** * @param string $Rules */ - public function setRules($Rules) - { + public function setRules($Rules) { $this->Rules = $Rules; } } \ No newline at end of file diff --git a/src/AbstractClient.php b/src/AbstractClient.php index d66de03..629d979 100644 --- a/src/AbstractClient.php +++ b/src/AbstractClient.php @@ -16,6 +16,7 @@ limitations under the License. */ +use Http\Client\HttpClient; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; @@ -23,8 +24,7 @@ * Class AbstractClient * @package DCarbone\PHPConsulAPI */ -abstract class AbstractClient -{ +abstract class AbstractClient { /** @var Config */ protected $c; @@ -32,53 +32,53 @@ abstract class AbstractClient * AbstractConsulClient constructor. * @param Config $config */ - public function __construct(Config $config) - { + public function __construct(Config $config) { $this->c = $config; } /** * @param array $r * @return array( - * @type int query duration in microseconds - * @type ResponseInterface|null response object - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type int query duration in microseconds + * @type ResponseInterface|null response object + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - protected function requireOK(array $r) - { + protected function requireOK(array $r) { // If a previous error occurred, just return as-is. - if (null !== $r[2]) + if (null !== $r[2]) { return $r; + } // If we have any kind of response... - if (null !== $r[1]) - { + if (null !== $r[1]) { // If this is a response... - if ($r[1] instanceof ResponseInterface) - { + if ($r[1] instanceof ResponseInterface) { // Get the response code... $code = $r[1]->getStatusCode(); // If 200, move right along - if (200 === $code) + if (200 === $code) { return $r; + } // Otherwise, return error - return [$r[0], $r[1], new Error(sprintf( - '%s - Non-200 response seen. Response code: %d. Message: %s', - get_class($this), - $code, - $r[1]->getReasonPhrase() - ))]; - - } - else - { - return [$r[0], $r[1], new Error(sprintf( - '%s - Expected response to be instance of \\Psr\\Message\\ResponseInterface, %s seen.', - is_object($r[1]) ? get_class($r[1]) : gettype($r[1]) - ))]; + return [$r[0], + $r[1], + new Error(sprintf( + '%s - Non-200 response seen. Response code: %d. Message: %s', + get_class($this), + $code, + $r[1]->getReasonPhrase() + ))]; + + } else { + return [$r[0], + $r[1], + new Error(sprintf( + '%s - Expected response to be instance of \\Psr\\Message\\ResponseInterface, %s seen.', + is_object($r[1]) ? get_class($r[1]) : gettype($r[1]) + ))]; } } @@ -88,27 +88,24 @@ protected function requireOK(array $r) /** * @param Request $r * @return array( - * @type int duration in microseconds - * @type \Psr\Http\Message\ResponseInterface|null http response - * @type \DCarbone\PHPConsulAPI\Error|null any seen errors + * @type int duration in microseconds + * @type \Psr\Http\Message\ResponseInterface|null http response + * @type \DCarbone\PHPConsulAPI\Error|null any seen errors * ) */ - protected function doRequest(Request $r) - { + protected function doRequest(Request $r) { $rt = microtime(true); $response = null; $err = null; - try - { + try { // If we actually have a client defined... - if (isset($this->c->HttpClient)) + if (isset($this->c->HttpClient) && $this->c->HttpClient instanceof HttpClient) { $response = $this->c->HttpClient->sendRequest($r->toPsrRequest()); - // Otherwise, throw error to be caught below - else + } // Otherwise, throw error to be caught below + else { throw new \RuntimeException('Unable to execute query as no HttpClient has been defined.'); - } - catch (\Exception $e) - { + } + } catch (\Exception $e) { // If there has been an exception of any kind, catch it and create Error object $err = new Error(sprintf( '%s - Error seen while executing "%s". Message: "%s"', @@ -128,25 +125,27 @@ protected function doRequest(Request $r) * @param Uri $uri * @return QueryMeta */ - protected function buildQueryMeta($duration, ResponseInterface $response, Uri $uri) - { + protected function buildQueryMeta($duration, ResponseInterface $response, Uri $uri) { $qm = new QueryMeta(); - $qm->requestTime = $duration; - $qm->requestUrl = (string)$uri; + $qm->RequestTime = $duration; + $qm->RequestUrl = (string)$uri; - if ('' !== ($h = $response->getHeaderLine('X-Consul-Index'))) - $qm->lastIndex = (int)$h; + if ('' !== ($h = $response->getHeaderLine('X-Consul-Index'))) { + $qm->LastIndex = (int)$h; + } - if ('' !== ($h = $response->getHeaderLine('X-Consul-KnownLeader'))) - $qm->knownLeader = (bool)$h; - else if ('' !== ($h = $response->getHeaderLine('X-Consul-Knownleader'))) - $qm->knownLeader = (bool)$h; + if ('' !== ($h = $response->getHeaderLine('X-Consul-KnownLeader'))) { + $qm->KnownLeader = (bool)$h; + } else if ('' !== ($h = $response->getHeaderLine('X-Consul-Knownleader'))) { + $qm->KnownLeader = (bool)$h; + } - if ('' !== ($h = $response->getHeaderLine('X-Consul-LastContact'))) - $qm->lastContact = (int)$h; - else if ('' !== ($h = $response->getHeaderLine('X-Consul-Lastcontact'))) - $qm->lastContact = (int)$h; + if ('' !== ($h = $response->getHeaderLine('X-Consul-LastContact'))) { + $qm->LastContact = (int)$h; + } else if ('' !== ($h = $response->getHeaderLine('X-Consul-Lastcontact'))) { + $qm->LastContact = (int)$h; + } return $qm; } @@ -155,12 +154,9 @@ protected function buildQueryMeta($duration, ResponseInterface $response, Uri $u * @param int $duration * @return WriteMeta */ - protected function buildWriteMeta($duration) - { + protected function buildWriteMeta($duration) { $wm = new WriteMeta(); - $wm->requestTime = $duration; - - Logger::debug(sprintf('WriteMeta built: %s', json_encode($wm))); + $wm->RequestTime = $duration; return $wm; } @@ -168,22 +164,23 @@ protected function buildWriteMeta($duration) /** * @param StreamInterface $body * @return array( - * @type array|string|bool|int|float decoded response - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type array|string|bool|int|float decoded response + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - protected function decodeBody(StreamInterface $body) - { + protected function decodeBody(StreamInterface $body) { $data = @json_decode((string)$body, true); $err = json_last_error(); - if (JSON_ERROR_NONE === $err) + if (JSON_ERROR_NONE === $err) { return [$data, null]; + } - return [null, new Error(sprintf( - '%s - Unable to parse response as JSON. Message: %s', - get_class($this), - PHP_VERSION_ID >= 50500 ? json_last_error_msg() : (string)$err - ))]; + return [null, + new Error(sprintf( + '%s - Unable to parse response as JSON. Message: %s', + get_class($this), + PHP_VERSION_ID >= 50500 ? json_last_error_msg() : (string)$err + ))]; } } \ No newline at end of file diff --git a/src/AbstractCollection.php b/src/AbstractCollection.php deleted file mode 100644 index 5b50caa..0000000 --- a/src/AbstractCollection.php +++ /dev/null @@ -1,242 +0,0 @@ - $v) - { - $this[$k] = $v; - } - } - - /** - * @return array - */ - public function __sleep() - { - return ['_storage']; - } - - /** - * @return array - */ - public function __debugInfo() - { - return $this->_storage; - } - - /** - * @return mixed - */ - public function reset() - { - return reset($this->_storage); - } - - /** - * @return bool - */ - public function isEmpty() - { - return 0 === count($this); - } - - /** - * @return array - */ - public function keys() - { - return array_keys($this->_storage); - } - - /** - * @return array - */ - public function values() - { - return array_values($this->_storage); - } - - /** - * @return mixed - */ - public function current() - { - return current($this->_storage); - } - - public function next() - { - next($this->_storage); - } - - /** - * @return string|int - */ - public function key() - { - return key($this->_storage); - } - - /** - * @return bool - */ - public function valid() - { - return null !== key($this->_storage); - } - - public function rewind() - { - reset($this->_storage); - } - - /** - * @param mixed $offset - * @return bool - */ - public function offsetExists($offset) - { - return isset($this->_storage[$offset]) || array_key_exists($offset, $this->_storage); - } - - /** - * @param string|int $offset - * @return mixed|null - */ - public function offsetGet($offset) - { - if (isset($this[$offset])) - return $this->_storage[$offset]; - - $this->_triggerOutOfBoundsError($offset); - - return null; - } - - /** - * @param null|string|int $offset - * @param mixed $value - */ - public function offsetSet($offset, $value) - { - if (null === $offset) - $this->_storage[] = $value; - else - $this->_storage[$offset] = $value; - } - - /** - * @param string|int $offset - */ - public function offsetUnset($offset) - { - if (isset($this[$offset])) - unset($this->_storage[$offset]); - } - - /** - * @return array - */ - public function jsonSerialize() - { - return $this->_storage; - } - - /** - * @return int - */ - public function count() - { - return count($this->_storage); - } - - /** - * @return string - */ - public function __toString() - { - return get_class($this); - } - - /** - * @param mixed $key - * @param int $level - */ - protected function _triggerOutOfBoundsError($key, $level = E_USER_NOTICE) - { - $matches = $this->_findKeyMatches($key); - if (0 === count($matches)) - { - trigger_error( - sprintf( - '%s - "%s" is not a property on this object.', - get_class($this), - is_string($key) ? $key : gettype($key) - ), - $level - ); - } - else - { - trigger_error( - sprintf( - '%s - "%s" is not a property on this object. Did you mean one of the following: ["%s"]?', - get_class($this), - $key, - implode('", "', $matches) - ), - $level - ); - } - } - - /** - * @param mixed $key - * @return array - */ - protected function _findKeyMatches($key) - { - $possibleMatches = []; - if (is_string($key)) - { - $regex = sprintf('{^.*%s.*$}i', substr($key, 0, 2)); - foreach($this as $k => $_) - { - if (preg_match($regex, $k)) - $possibleMatches[] = $k; - } - } - return $possibleMatches; - } -} \ No newline at end of file diff --git a/src/AbstractModel.php b/src/AbstractModel.php index 015bb19..35eec62 100644 --- a/src/AbstractModel.php +++ b/src/AbstractModel.php @@ -20,37 +20,37 @@ * Class AbstractModel * @package DCarbone\PHPConsulAPI */ -abstract class AbstractModel implements \JsonSerializable -{ +abstract class AbstractModel implements \JsonSerializable { + /** * AbstractObjectModel constructor. * - * Convenience method to help set scalar types. For object types, it is recommended - * that the implementing class have it's own constructor + * Convenience method to help set scalar types. Any extending class must have a constructor that builds any + * array / object properties it may have. * * @param array $data */ - public function __construct(array $data = []) - { - foreach($data as $k => $v) - { + public function __construct(array $data = []) { + foreach ($data as $k => $v) { $this->{$k} = $v; } } /** + * TODO: More specific omission of values? + * + * Produces equivalent of `json:",omitempty"` tag + * * @return array */ - function jsonSerialize() - { + function jsonSerialize() { return array_filter((array)$this); } /** * @return string */ - public function __toString() - { + public function __toString() { return get_class($this); } } \ No newline at end of file diff --git a/src/AbstractOptions.php b/src/AbstractOptions.php deleted file mode 100644 index 358532f..0000000 --- a/src/AbstractOptions.php +++ /dev/null @@ -1,114 +0,0 @@ -_definition = $this->getDefinition(); - parent::__construct($data + $this->_definition); - } - - /** - * @return array - */ - abstract protected function getDefinition(); - - /** - * @return array - */ - public function __sleep() - { - return ['_storage', '_definition']; - } - - /** - * @param mixed $offset - * @return bool - */ - public function offsetExists($offset) - { - return isset($this->_definition[$offset]) || array_key_exists($offset, $this->_definition); - } - - /** - * @param int|string $offset - * @return mixed - */ - public function offsetGet($offset) - { - if (isset($this[$offset])) - return $this->{sprintf('get%s', $offset)}(); - - throw $this->_createOutOfBoundsException($offset); - } - - /** - * @param int|null|string $offset - * @param mixed $value - */ - public function offsetSet($offset, $value) - { - if (isset($this[$offset])) - { - if (null === $value) - $this->_storage[$offset] = null; - else - $this->{sprintf('set%s', $offset)}($value); - } - else - { - throw $this->_createOutOfBoundsException($offset); - } - } - - /** - * @param mixed $key - * @return \OutOfBoundsException - */ - protected function _createOutOfBoundsException($key) - { - $matches = $this->_findKeyMatches($key); - if (0 === count($matches)) - { - return new \OutOfBoundsException(sprintf( - '%s - "%s" is not a property on this object.', - get_class($this), - is_string($key) ? $key : gettype($key) - )); - } - - return new \OutOfBoundsException(sprintf( - '%s - "%s" is not a property on this object. Did you mean one of the following: ["%s"]?', - get_class($this), - $key, - implode('", "', $matches) - )); - } -} \ No newline at end of file diff --git a/src/Agent/AgentCheck.php b/src/Agent/AgentCheck.php index 3f86646..fd9b47f 100644 --- a/src/Agent/AgentCheck.php +++ b/src/Agent/AgentCheck.php @@ -22,8 +22,7 @@ * Class AgentCheck * @package DCarbone\PHPConsulAPI\Agent */ -class AgentCheck extends AbstractModel -{ +class AgentCheck extends AbstractModel { /** @var string */ public $Node = ''; /** @var string */ @@ -44,8 +43,7 @@ class AgentCheck extends AbstractModel /** * @return string */ - public function getNode() - { + public function getNode() { return $this->Node; } @@ -53,8 +51,7 @@ public function getNode() * @param string $Node * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function setNode($Node) - { + public function setNode($Node) { $this->Node = $Node; return $this; } @@ -62,8 +59,7 @@ public function setNode($Node) /** * @return string */ - public function getCheckID() - { + public function getCheckID() { return $this->CheckID; } @@ -71,8 +67,7 @@ public function getCheckID() * @param string $CheckID * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function setCheckID($CheckID) - { + public function setCheckID($CheckID) { $this->CheckID = $CheckID; return $this; } @@ -80,8 +75,7 @@ public function setCheckID($CheckID) /** * @return string */ - public function getName() - { + public function getName() { return $this->Name; } @@ -89,8 +83,7 @@ public function getName() * @param string $Name * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function setName($Name) - { + public function setName($Name) { $this->Name = $Name; return $this; } @@ -98,8 +91,7 @@ public function setName($Name) /** * @return string */ - public function getStatus() - { + public function getStatus() { return $this->Status; } @@ -107,8 +99,7 @@ public function getStatus() * @param string $Status * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function setStatus($Status) - { + public function setStatus($Status) { $this->Status = $Status; return $this; } @@ -116,8 +107,7 @@ public function setStatus($Status) /** * @return string */ - public function getNotes() - { + public function getNotes() { return $this->Notes; } @@ -125,8 +115,7 @@ public function getNotes() * @param string $Notes * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function setNotes($Notes) - { + public function setNotes($Notes) { $this->Notes = $Notes; return $this; } @@ -134,8 +123,7 @@ public function setNotes($Notes) /** * @return string */ - public function getOutput() - { + public function getOutput() { return $this->Output; } @@ -143,8 +131,7 @@ public function getOutput() * @param string $Output * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function setOutput($Output) - { + public function setOutput($Output) { $this->Output = $Output; return $this; } @@ -152,8 +139,7 @@ public function setOutput($Output) /** * @return string */ - public function getServiceID() - { + public function getServiceID() { return $this->ServiceID; } @@ -161,8 +147,7 @@ public function getServiceID() * @param string $ServiceID * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function setServiceID($ServiceID) - { + public function setServiceID($ServiceID) { $this->ServiceID = $ServiceID; return $this; } @@ -170,8 +155,7 @@ public function setServiceID($ServiceID) /** * @return string */ - public function getServiceName() - { + public function getServiceName() { return $this->ServiceName; } @@ -179,8 +163,7 @@ public function getServiceName() * @param string $ServiceName * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function setServiceName($ServiceName) - { + public function setServiceName($ServiceName) { $this->ServiceName = $ServiceName; return $this; } @@ -188,8 +171,7 @@ public function setServiceName($ServiceName) /** * @return string */ - public function __toString() - { + public function __toString() { return (string)$this->CheckID; } } \ No newline at end of file diff --git a/src/Agent/AgentCheckRegistration.php b/src/Agent/AgentCheckRegistration.php index bcb245f..e749206 100644 --- a/src/Agent/AgentCheckRegistration.php +++ b/src/Agent/AgentCheckRegistration.php @@ -20,8 +20,7 @@ * Class AgentCheckRegistration * @package DCarbone\PHPConsulAPI\Agent */ -class AgentCheckRegistration extends AgentServiceCheck -{ +class AgentCheckRegistration extends AgentServiceCheck { /** @var string */ public $ID = ''; /** @var string */ @@ -34,8 +33,7 @@ class AgentCheckRegistration extends AgentServiceCheck /** * @return string */ - public function getID() - { + public function getID() { return $this->ID; } @@ -43,8 +41,7 @@ public function getID() * @param string $ID * @return \DCarbone\PHPConsulAPI\Agent\AgentCheckRegistration */ - public function setID($ID) - { + public function setID($ID) { $this->ID = $ID; return $this; } @@ -52,8 +49,7 @@ public function setID($ID) /** * @return string */ - public function getName() - { + public function getName() { return $this->Name; } @@ -61,8 +57,7 @@ public function getName() * @param string $Name * @return \DCarbone\PHPConsulAPI\Agent\AgentCheckRegistration */ - public function setName($Name) - { + public function setName($Name) { $this->Name = $Name; return $this; } @@ -70,8 +65,7 @@ public function setName($Name) /** * @return string */ - public function getNotes() - { + public function getNotes() { return $this->Notes; } @@ -79,8 +73,7 @@ public function getNotes() * @param string $Notes * @return \DCarbone\PHPConsulAPI\Agent\AgentCheckRegistration */ - public function setNotes($Notes) - { + public function setNotes($Notes) { $this->Notes = $Notes; return $this; } @@ -88,8 +81,7 @@ public function setNotes($Notes) /** * @return string */ - public function getServiceID() - { + public function getServiceID() { return $this->ServiceID; } @@ -97,8 +89,7 @@ public function getServiceID() * @param string $ServiceID * @return \DCarbone\PHPConsulAPI\Agent\AgentCheckRegistration */ - public function setServiceID($ServiceID) - { + public function setServiceID($ServiceID) { $this->ServiceID = $ServiceID; return $this; } @@ -106,8 +97,7 @@ public function setServiceID($ServiceID) /** * @return string */ - public function __toString() - { + public function __toString() { return (string)$this->Name; } } \ No newline at end of file diff --git a/src/Agent/AgentCheckUpdate.php b/src/Agent/AgentCheckUpdate.php index 8b02844..8a34ede 100644 --- a/src/Agent/AgentCheckUpdate.php +++ b/src/Agent/AgentCheckUpdate.php @@ -22,8 +22,7 @@ * Class AgentCheckUpdate * @package DCarbone\PHPConsulAPI\Agent */ -class AgentCheckUpdate extends AbstractModel -{ +class AgentCheckUpdate extends AbstractModel { /** @var string */ public $Status = ''; /** @var string */ @@ -32,8 +31,7 @@ class AgentCheckUpdate extends AbstractModel /** * @return string */ - public function getStatus() - { + public function getStatus() { return $this->Status; } @@ -41,8 +39,7 @@ public function getStatus() * @param string $Status * @return \DCarbone\PHPConsulAPI\Agent\AgentCheckUpdate */ - public function setStatus($Status) - { + public function setStatus($Status) { $this->Status = $Status; return $this; } @@ -50,8 +47,7 @@ public function setStatus($Status) /** * @return string */ - public function getOutput() - { + public function getOutput() { return $this->Output; } @@ -59,8 +55,7 @@ public function getOutput() * @param string $Output * @return \DCarbone\PHPConsulAPI\Agent\AgentCheckUpdate */ - public function setOutput($Output) - { + public function setOutput($Output) { $this->Output = $Output; return $this; } diff --git a/src/Agent/AgentClient.php b/src/Agent/AgentClient.php index be56e3c..b8df1ab 100644 --- a/src/Agent/AgentClient.php +++ b/src/Agent/AgentClient.php @@ -23,8 +23,7 @@ * Class AgentClient * @package DCarbone\PHPConsulAPI\Agent */ -class AgentClient extends AbstractClient -{ +class AgentClient extends AbstractClient { /** @var null|AgentSelf */ private $_self = null; @@ -35,21 +34,22 @@ class AgentClient extends AbstractClient * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function self() - { + public function self() { $r = new Request('get', 'v1/agent/self', $this->c); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $this->_self = new AgentSelf($data); @@ -58,17 +58,16 @@ public function self() /** * @return array( - * @type string name of node or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type string name of node or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function nodeName() - { - if (null === $this->_self) - { + public function nodeName() { + if (null === $this->_self) { list($_, $_, $err) = $this->self(); - if (null !== $err) + if (null !== $err) { return ['', $err]; + } } return [$this->_self->Config->NodeName, null]; @@ -76,28 +75,28 @@ public function nodeName() /** * @return array( - * @type AgentCheck[]|null array of agent checks or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type AgentCheck[]|null array of agent checks or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function checks() - { + public function checks() { $r = new Request('get', 'v1/agent/checks', $this->c); /** @var \Psr\Http\Message\ResponseInterface $response */ list($_, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } $checks = []; - foreach($data as $k => $v) - { + foreach ($data as $k => $v) { $checks[$k] = new AgentCheck($v); } @@ -106,28 +105,28 @@ public function checks() /** * @return array( - * @type AgentService[]|null list of agent services or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type AgentService[]|null list of agent services or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function services() - { + public function services() { $r = new Request('get', 'v1/agent/services', $this->c); /** @var \Psr\Http\Message\ResponseInterface $response */ list($_, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } $services = []; - foreach($data as $k => $v) - { + foreach ($data as $k => $v) { $services[$k] = new AgentService($v); } @@ -136,28 +135,28 @@ public function services() /** * @return array( - * @type AgentMember[]|null array of agent members or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type AgentMember[]|null array of agent members or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function members() - { + public function members() { $r = new Request('get', 'v1/agent/members', $this->c); /** @var \Psr\Http\Message\ResponseInterface $response */ list($_, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } $members = []; - foreach($data as $v) - { + foreach ($data as $v) { $members[] = new AgentMember($v); } @@ -173,8 +172,7 @@ public function members() * @type \DCarbone\PHPConsulAPI\Error|null * ) */ - public function serviceRegister(AgentServiceRegistration $agentServiceRegistration) - { + public function serviceRegister(AgentServiceRegistration $agentServiceRegistration) { $r = new Request('put', 'v1/agent/service/register', $this->c, $agentServiceRegistration); list($_, $_, $err) = $this->requireOK($this->doRequest($r)); @@ -188,8 +186,7 @@ public function serviceRegister(AgentServiceRegistration $agentServiceRegistrati * @param string $serviceID * @return \DCarbone\PHPConsulAPI\Error|null */ - public function serviceDeregister($serviceID) - { + public function serviceDeregister($serviceID) { $r = new Request('put', sprintf('v1/agent/service/deregister/%s', $serviceID), $this->c); list($_, $_, $err) = $this->requireOK($this->doRequest($r)); @@ -204,8 +201,7 @@ public function serviceDeregister($serviceID) * @param string $note * @return \DCarbone\PHPConsulAPI\Error|null */ - public function passTTL($checkID, $note) - { + public function passTTL($checkID, $note) { return $this->updateTTL($checkID, $note, 'passing'); } @@ -216,8 +212,7 @@ public function passTTL($checkID, $note) * @param string $note * @return \DCarbone\PHPConsulAPI\Error|null */ - public function warnTTL($checkID, $note) - { + public function warnTTL($checkID, $note) { return $this->updateTTL($checkID, $note, 'warning'); } @@ -228,8 +223,7 @@ public function warnTTL($checkID, $note) * @param string $note * @return \DCarbone\PHPConsulAPI\Error|null */ - public function failTTL($checkID, $note) - { + public function failTTL($checkID, $note) { return $this->updateTTL($checkID, $note, 'critical'); } @@ -241,9 +235,11 @@ public function failTTL($checkID, $note) * @param string $status * @return \DCarbone\PHPConsulAPI\Error|null */ - public function updateTTL($checkID, $output, $status) - { - $r = new Request('put', sprintf('v1/agent/check/update/%s', $checkID), $this->c, new AgentCheckUpdate(['Output' => $output, 'Status' => $status])); + public function updateTTL($checkID, $output, $status) { + $r = new Request('put', + sprintf('v1/agent/check/update/%s', $checkID), + $this->c, + new AgentCheckUpdate(['Output' => $output, 'Status' => $status])); list($_, $_, $err) = $this->requireOK($this->doRequest($r)); @@ -257,8 +253,7 @@ public function updateTTL($checkID, $output, $status) * @type \DCarbone\PHPConsulAPI\Error|null * ) */ - public function checkRegister(AgentCheckRegistration $agentCheckRegistration) - { + public function checkRegister(AgentCheckRegistration $agentCheckRegistration) { $r = new Request('put', 'v1/agent/check/register', $this->c, $agentCheckRegistration); list($_, $_, $err) = $this->requireOK($this->doRequest($r)); @@ -270,8 +265,7 @@ public function checkRegister(AgentCheckRegistration $agentCheckRegistration) * @param string $checkID * @return \DCarbone\PHPConsulAPI\Error|null */ - public function checkDeregister($checkID) - { + public function checkDeregister($checkID) { $r = new Request('put', sprintf('v1/agent/check/deregister/%s', $checkID), $this->c); list($_, $_, $err) = $this->requireOK($this->doRequest($r)); @@ -284,11 +278,11 @@ public function checkDeregister($checkID) * @param bool $wan * @return \DCarbone\PHPConsulAPI\Error|null */ - public function join($addr, $wan = false) - { + public function join($addr, $wan = false) { $r = new Request('put', sprintf('v1/agent/join/%s', $addr), $this->c); - if ($wan) - $r->params->set('wan', 1); + if ($wan) { + $r->params->set('wan', '1'); + } list($_, $_, $err) = $this->requireOK($this->doRequest($r)); @@ -299,8 +293,7 @@ public function join($addr, $wan = false) * @param string $node * @return \DCarbone\PHPConsulAPI\Error|null */ - public function forceLeave($node) - { + public function forceLeave($node) { $r = new Request('put', sprintf('v1/agent/force-leave/%s', $node), $this->c); list($_, $_, $err) = $this->requireOK($this->doRequest($r)); @@ -313,8 +306,7 @@ public function forceLeave($node) * @param string $reason * @return \DCarbone\PHPConsulAPI\Error|null */ - public function enableServiceMaintenance($serviceID, $reason = '') - { + public function enableServiceMaintenance($serviceID, $reason = '') { $r = new Request('put', sprintf('v1/agent/service/maintenance/%s', $serviceID), $this->c); $r->params->set('enable', 'true'); $r->params->set('reason', $reason); @@ -328,8 +320,7 @@ public function enableServiceMaintenance($serviceID, $reason = '') * @param string $serviceID * @return \DCarbone\PHPConsulAPI\Error|null */ - public function disableServiceMaintenance($serviceID) - { + public function disableServiceMaintenance($serviceID) { $r = new Request('put', sprintf('v1/agent/service/maintenance/%s', $serviceID), $this->c); $r->params->set('enable', 'false'); @@ -342,8 +333,7 @@ public function disableServiceMaintenance($serviceID) * @param string $reason * @return \DCarbone\PHPConsulAPI\Error|null */ - public function enableNodeMaintenance($reason = '') - { + public function enableNodeMaintenance($reason = '') { $r = new Request('put', 'v1/agent/maintenance', $this->c); $r->params->set('enable', 'true'); $r->params->set('reason', $reason); @@ -356,8 +346,7 @@ public function enableNodeMaintenance($reason = '') /** * @return \DCarbone\PHPConsulAPI\Error|null */ - public function disableNodeMaintenance() - { + public function disableNodeMaintenance() { $r = new Request('put', 'v1/agent/maintenance', $this->c); $r->params->set('enable', 'false'); @@ -369,8 +358,7 @@ public function disableNodeMaintenance() /** * @return \DCarbone\PHPConsulAPI\Error|null */ - public function reload() - { + public function reload() { $r = new Request('put', 'v1/agent/reload', $this->c); list($_, $_, $err) = $this->requireOK($this->doRequest($r)); @@ -381,8 +369,7 @@ public function reload() /** * @return \DCarbone\PHPConsulAPI\Error|null */ - public function leave() - { + public function leave() { $r = new Request('put', 'v1/agent/leave', $this->c); list($_, $_, $err) = $this->requireOK($this->doRequest($r)); diff --git a/src/Agent/AgentMember.php b/src/Agent/AgentMember.php index d13ae65..93becfa 100644 --- a/src/Agent/AgentMember.php +++ b/src/Agent/AgentMember.php @@ -23,8 +23,7 @@ * Class AgentMember * @package DCarbone\PHPConsulAPI\Agent */ -class AgentMember extends AbstractModel -{ +class AgentMember extends AbstractModel { use HasStringTags; /** @var string */ @@ -51,88 +50,77 @@ class AgentMember extends AbstractModel /** * @return string */ - public function getName() - { + public function getName() { return $this->Name; } /** * @return string */ - public function getAddr() - { + public function getAddr() { return $this->Addr; } /** * @return int */ - public function getPort() - { + public function getPort() { return $this->Port; } /** * @return string */ - public function getStatus() - { + public function getStatus() { return $this->Status; } /** * @return int */ - public function getProtocolMin() - { + public function getProtocolMin() { return $this->ProtocolMin; } /** * @return int */ - public function getProtocolMax() - { + public function getProtocolMax() { return $this->ProtocolMax; } /** * @return int */ - public function getProtocolCur() - { + public function getProtocolCur() { return $this->ProtocolCur; } /** * @return int */ - public function getDelegateMin() - { + public function getDelegateMin() { return $this->DelegateMin; } /** * @return int */ - public function getDelegateMax() - { + public function getDelegateMax() { return $this->DelegateMax; } /** * @return int */ - public function getDelegateCur() - { + public function getDelegateCur() { return $this->DelegateCur; } /** * @return string */ - public function __toString() - { + public function __toString() { return $this->Name; } } \ No newline at end of file diff --git a/src/Agent/AgentSelf.php b/src/Agent/AgentSelf.php index d19e363..da6c896 100644 --- a/src/Agent/AgentSelf.php +++ b/src/Agent/AgentSelf.php @@ -22,8 +22,7 @@ * Class AgentSelf * @package DCarbone\PHPConsulAPI\Agent */ -class AgentSelf extends AbstractModel -{ +class AgentSelf extends AbstractModel { /** @var \DCarbone\PHPConsulAPI\Agent\AgentSelfConfig */ public $Config = null; /** @var \DCarbone\PHPConsulAPI\Agent\AgentSelfCoord */ @@ -35,38 +34,37 @@ class AgentSelf extends AbstractModel * AgentSelf constructor. * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); - if (null !== $this->Config && !($this->Config instanceof AgentSelfConfig)) + if (null !== $this->Config && !($this->Config instanceof AgentSelfConfig)) { $this->Config = new AgentSelfConfig((array)$this->Config); - if (null !== $this->Coord && !($this->Coord instanceof AgentSelfCoord)) + } + if (null !== $this->Coord && !($this->Coord instanceof AgentSelfCoord)) { $this->Coord = new AgentSelfCoord((array)$this->Coord); - if (null !== $this->Member && !($this->Member instanceof AgentMember)) + } + if (null !== $this->Member && !($this->Member instanceof AgentMember)) { $this->Member = new AgentMember((array)$this->Member); + } } /** * @return \DCarbone\PHPConsulAPI\Agent\AgentSelfConfig */ - public function getConfig() - { + public function getConfig() { return $this->Config; } /** * @return \DCarbone\PHPConsulAPI\Agent\AgentSelfCoord */ - public function getCoord() - { + public function getCoord() { return $this->Coord; } /** * @return \DCarbone\PHPConsulAPI\Agent\AgentMember */ - public function getMember() - { + public function getMember() { return $this->Member; } } \ No newline at end of file diff --git a/src/Agent/AgentSelfConfig.php b/src/Agent/AgentSelfConfig.php index 7f9682a..c708062 100644 --- a/src/Agent/AgentSelfConfig.php +++ b/src/Agent/AgentSelfConfig.php @@ -22,8 +22,7 @@ * Class AgentSelfConfig * @package DCarbone\PHPConsulAPI\Agent */ -class AgentSelfConfig extends AbstractModel -{ +class AgentSelfConfig extends AbstractModel { /** @var bool */ public $Bootstrap = false; /** @var bool */ @@ -90,248 +89,217 @@ class AgentSelfConfig extends AbstractModel /** * @return boolean */ - public function isBootstrap() - { + public function isBootstrap() { return $this->Bootstrap; } /** * @return boolean */ - public function isDevMode() - { + public function isDevMode() { return $this->DevMode; } /** * @return int */ - public function getBootstrapExpect() - { + public function getBootstrapExpect() { return $this->BootstrapExpect; } /** * @return boolean */ - public function isServer() - { + public function isServer() { return $this->Server; } /** * @return string */ - public function getDatacenter() - { + public function getDatacenter() { return $this->Datacenter; } /** * @return string */ - public function getDataDir() - { + public function getDataDir() { return $this->DataDir; } /** * @return string */ - public function getDNSRecursor() - { + public function getDNSRecursor() { return $this->DNSRecursor; } /** * @return string[] */ - public function getDNSRecursors() - { + public function getDNSRecursors() { return $this->DNSRecursors; } /** * @return string[] */ - public function getDNSConfig() - { + public function getDNSConfig() { return $this->DNSConfig; } /** * @return string */ - public function getDomain() - { + public function getDomain() { return $this->Domain; } /** * @return string */ - public function getLogLevel() - { + public function getLogLevel() { return $this->LogLevel; } /** * @return string */ - public function getNodeName() - { + public function getNodeName() { return $this->NodeName; } /** * @return string */ - public function getClientAddr() - { + public function getClientAddr() { return $this->ClientAddr; } /** * @return string */ - public function getBindAddr() - { + public function getBindAddr() { return $this->BindAddr; } /** * @return string */ - public function getAdvertiseAddr() - { + public function getAdvertiseAddr() { return $this->AdvertiseAddr; } /** * @return string[] */ - public function getPorts() - { + public function getPorts() { return $this->Ports; } /** * @return boolean */ - public function isLeaveOnTerm() - { + public function isLeaveOnTerm() { return $this->LeaveOnTerm; } /** * @return boolean */ - public function isSkipLeaveOnInt() - { + public function isSkipLeaveOnInt() { return $this->SkipLeaveOnInt; } /** * @return string[] */ - public function getTelemetry() - { + public function getTelemetry() { return $this->Telemetry; } /** * @return int */ - public function getProtocol() - { + public function getProtocol() { return $this->Protocol; } /** * @return boolean */ - public function isEnableDebug() - { + public function isEnableDebug() { return $this->EnableDebug; } /** * @return boolean */ - public function isVerifyIncoming() - { + public function isVerifyIncoming() { return $this->VerifyIncoming; } /** * @return boolean */ - public function isVerifyOutgoing() - { + public function isVerifyOutgoing() { return $this->VerifyOutgoing; } /** * @return string */ - public function getCAFile() - { + public function getCAFile() { return $this->CAFile; } /** * @return string */ - public function getCertFile() - { + public function getCertFile() { return $this->CertFile; } /** * @return string */ - public function getKeyFile() - { + public function getKeyFile() { return $this->KeyFile; } /** * @return string[] */ - public function getStartJoin() - { + public function getStartJoin() { return $this->StartJoin; } /** * @return string */ - public function getUiDir() - { + public function getUiDir() { return $this->UiDir; } /** * @return string */ - public function getPidFile() - { + public function getPidFile() { return $this->PidFile; } /** * @return boolean */ - public function isEnableSyslog() - { + public function isEnableSyslog() { return $this->EnableSyslog; } /** * @return boolean */ - public function isRejoinAfterLeave() - { + public function isRejoinAfterLeave() { return $this->RejoinAfterLeave; } } \ No newline at end of file diff --git a/src/Agent/AgentSelfCoord.php b/src/Agent/AgentSelfCoord.php index 01829a1..7b51d41 100644 --- a/src/Agent/AgentSelfCoord.php +++ b/src/Agent/AgentSelfCoord.php @@ -22,8 +22,7 @@ * Class AgentSelfCoord * @package DCarbone\PHPConsulAPI\Agent */ -class AgentSelfCoord extends AbstractModel -{ +class AgentSelfCoord extends AbstractModel { /** @var array */ public $Vec = []; /** @var float */ @@ -36,32 +35,28 @@ class AgentSelfCoord extends AbstractModel /** * @return array */ - public function getVec() - { + public function getVec() { return $this->Vec; } /** * @return float */ - public function getError() - { + public function getError() { return $this->Error; } /** * @return float */ - public function getAdjustment() - { + public function getAdjustment() { return $this->Adjustment; } /** * @return float */ - public function getHeight() - { + public function getHeight() { return $this->Height; } } \ No newline at end of file diff --git a/src/Agent/AgentService.php b/src/Agent/AgentService.php index 4203ed3..4f50cf0 100644 --- a/src/Agent/AgentService.php +++ b/src/Agent/AgentService.php @@ -23,8 +23,7 @@ * Class AgentService * @package DCarbone\PHPConsulAPI\Agent */ -class AgentService extends AbstractModel -{ +class AgentService extends AbstractModel { use HasStringTags; /** @var string */ @@ -41,48 +40,42 @@ class AgentService extends AbstractModel /** * @return string */ - public function getID() - { + public function getID() { return $this->ID; } /** * @return string */ - public function getService() - { + public function getService() { return $this->Service; } /** * @return string */ - public function getAddress() - { + public function getAddress() { return $this->Address; } /** * @return int */ - public function getPort() - { + public function getPort() { return $this->Port; } /** * @return boolean */ - public function isEnableTagOverride() - { + public function isEnableTagOverride() { return $this->EnableTagOverride; } /** * @return string */ - public function __toString() - { + public function __toString() { return $this->ID; } } \ No newline at end of file diff --git a/src/Agent/AgentServiceCheck.php b/src/Agent/AgentServiceCheck.php index 94b88d9..7f3cdef 100644 --- a/src/Agent/AgentServiceCheck.php +++ b/src/Agent/AgentServiceCheck.php @@ -22,8 +22,7 @@ * Class AgentServiceCheck * @package DCarbone\PHPConsulAPI\Agent */ -class AgentServiceCheck extends AbstractModel -{ +class AgentServiceCheck extends AbstractModel { /** @var string */ public $Script = ''; /** @var string */ @@ -46,8 +45,7 @@ class AgentServiceCheck extends AbstractModel /** * @return string */ - public function getScript() - { + public function getScript() { return $this->Script; } @@ -55,8 +53,7 @@ public function getScript() * @param string $Script * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */ - public function setScript($Script) - { + public function setScript($Script) { $this->Script = $Script; return $this; } @@ -64,8 +61,7 @@ public function setScript($Script) /** * @return string */ - public function getDockerContainerID() - { + public function getDockerContainerID() { return $this->DockerContainerID; } @@ -73,8 +69,7 @@ public function getDockerContainerID() * @param string $DockerContainerID * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */ - public function setDockerContainerID($DockerContainerID) - { + public function setDockerContainerID($DockerContainerID) { $this->DockerContainerID = $DockerContainerID; return $this; } @@ -82,8 +77,7 @@ public function setDockerContainerID($DockerContainerID) /** * @return string */ - public function getShell() - { + public function getShell() { return $this->Shell; } @@ -91,8 +85,7 @@ public function getShell() * @param string $Shell * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */ - public function setShell($Shell) - { + public function setShell($Shell) { $this->Shell = $Shell; return $this; } @@ -100,8 +93,7 @@ public function setShell($Shell) /** * @return string */ - public function getInterval() - { + public function getInterval() { return $this->Interval; } @@ -109,8 +101,7 @@ public function getInterval() * @param string $Interval * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */ - public function setInterval($Interval) - { + public function setInterval($Interval) { $this->Interval = $Interval; return $this; } @@ -118,8 +109,7 @@ public function setInterval($Interval) /** * @return string */ - public function getTimeout() - { + public function getTimeout() { return $this->Timeout; } @@ -127,8 +117,7 @@ public function getTimeout() * @param string $Timeout * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */ - public function setTimeout($Timeout) - { + public function setTimeout($Timeout) { $this->Timeout = $Timeout; return $this; } @@ -136,8 +125,7 @@ public function setTimeout($Timeout) /** * @return string */ - public function getTTL() - { + public function getTTL() { return $this->TTL; } @@ -145,8 +133,7 @@ public function getTTL() * @param string $TTL * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */ - public function setTTL($TTL) - { + public function setTTL($TTL) { $this->TTL = $TTL; return $this; } @@ -154,8 +141,7 @@ public function setTTL($TTL) /** * @return string */ - public function getHTTP() - { + public function getHTTP() { return $this->HTTP; } @@ -163,8 +149,7 @@ public function getHTTP() * @param string $HTTP * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */ - public function setHTTP($HTTP) - { + public function setHTTP($HTTP) { $this->HTTP = $HTTP; return $this; } @@ -172,8 +157,7 @@ public function setHTTP($HTTP) /** * @return string */ - public function getTCP() - { + public function getTCP() { return $this->TCP; } @@ -181,8 +165,7 @@ public function getTCP() * @param string $TCP * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */ - public function setTCP($TCP) - { + public function setTCP($TCP) { $this->TCP = $TCP; return $this; } @@ -190,8 +173,7 @@ public function setTCP($TCP) /** * @return string */ - public function getStatus() - { + public function getStatus() { return $this->Status; } @@ -199,8 +181,7 @@ public function getStatus() * @param string $Status * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceCheck */ - public function setStatus($Status) - { + public function setStatus($Status) { $this->Status = $Status; return $this; } diff --git a/src/Agent/AgentServiceRegistration.php b/src/Agent/AgentServiceRegistration.php index 35c4334..d44e572 100644 --- a/src/Agent/AgentServiceRegistration.php +++ b/src/Agent/AgentServiceRegistration.php @@ -24,8 +24,7 @@ * Class AgentServiceRegistration * @package DCarbone\PHPConsulAPI\Agent */ -class AgentServiceRegistration extends AbstractModel -{ +class AgentServiceRegistration extends AbstractModel { use HasStringTags, HasSettableStringTags; /** @var string */ @@ -47,22 +46,20 @@ class AgentServiceRegistration extends AbstractModel * AgentServiceRegistration constructor. * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); - if (null !== $this->Check && !($this->Check instanceof AgentCheck)) + if (null !== $this->Check && !($this->Check instanceof AgentCheck)) { $this->Check = new AgentCheck((array)$this->Check); + } - if (0 < count($this->Checks)) - { + if (0 < count($this->Checks)) { $this->Checks = array_filter($this->Checks); - if (0 < ($cnt = count($this->Checks))) - { - for ($i = 0, $cnt = count($this->Checks); $i < $cnt; $i++) - { - if (!($this->Checks[$i] instanceof AgentCheck)) + if (0 < ($cnt = count($this->Checks))) { + for ($i = 0, $cnt = count($this->Checks); $i < $cnt; $i++) { + if (!($this->Checks[$i] instanceof AgentCheck)) { $this->Checks[$i] = new AgentCheck($this->Checks[$i]); + } } } } @@ -71,8 +68,7 @@ public function __construct(array $data = []) /** * @return string */ - public function getID() - { + public function getID() { return $this->ID; } @@ -80,8 +76,7 @@ public function getID() * @param string $ID * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration */ - public function setID($ID) - { + public function setID($ID) { $this->ID = $ID; return $this; } @@ -89,8 +84,7 @@ public function setID($ID) /** * @return string */ - public function getName() - { + public function getName() { return $this->Name; } @@ -98,8 +92,7 @@ public function getName() * @param string $Name * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration */ - public function setName($Name) - { + public function setName($Name) { $this->Name = $Name; return $this; } @@ -107,8 +100,7 @@ public function setName($Name) /** * @return int */ - public function getPort() - { + public function getPort() { return $this->Port; } @@ -116,8 +108,7 @@ public function getPort() * @param int $Port * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration */ - public function setPort($Port) - { + public function setPort($Port) { $this->Port = $Port; return $this; } @@ -125,8 +116,7 @@ public function setPort($Port) /** * @return string */ - public function getAddress() - { + public function getAddress() { return $this->Address; } @@ -134,8 +124,7 @@ public function getAddress() * @param string $Address * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration */ - public function setAddress($Address) - { + public function setAddress($Address) { $this->Address = $Address; return $this; } @@ -143,8 +132,7 @@ public function setAddress($Address) /** * @return boolean */ - public function isEnableTagOverride() - { + public function isEnableTagOverride() { return $this->EnableTagOverride; } @@ -152,8 +140,7 @@ public function isEnableTagOverride() * @param boolean $EnableTagOverride * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration */ - public function setEnableTagOverride($EnableTagOverride) - { + public function setEnableTagOverride($EnableTagOverride) { $this->EnableTagOverride = $EnableTagOverride; return $this; } @@ -161,8 +148,7 @@ public function setEnableTagOverride($EnableTagOverride) /** * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function getCheck() - { + public function getCheck() { return $this->Check; } @@ -170,8 +156,7 @@ public function getCheck() * @param \DCarbone\PHPConsulAPI\Agent\AgentCheck $Check * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration */ - public function setCheck(AgentCheck $Check) - { + public function setCheck(AgentCheck $Check) { $this->Check = $Check; return $this; } @@ -179,8 +164,7 @@ public function setCheck(AgentCheck $Check) /** * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck[] */ - public function getChecks() - { + public function getChecks() { return $this->Checks; } @@ -188,10 +172,8 @@ public function getChecks() * @param \DCarbone\PHPConsulAPI\Agent\AgentCheck[] $Checks * @return \DCarbone\PHPConsulAPI\Agent\AgentServiceRegistration */ - public function setChecks(array $Checks) - { - foreach($Checks as $Check) - { + public function setChecks(array $Checks) { + foreach ($Checks as $Check) { $this->addCheck($Check); } return $this; @@ -201,8 +183,7 @@ public function setChecks(array $Checks) * @param \DCarbone\PHPConsulAPI\Agent\AgentCheck $Check * @return $this */ - public function addCheck(AgentCheck $Check) - { + public function addCheck(AgentCheck $Check) { $this->Checks[] = $Check; return $this; } @@ -210,8 +191,7 @@ public function addCheck(AgentCheck $Check) /** * @return string */ - public function __toString() - { + public function __toString() { return $this->Name; } } \ No newline at end of file diff --git a/src/Catalog/CatalogClient.php b/src/Catalog/CatalogClient.php index 7aafbb6..d65fcdf 100644 --- a/src/Catalog/CatalogClient.php +++ b/src/Catalog/CatalogClient.php @@ -25,24 +25,23 @@ * Class CatalogClient * @package DCarbone\PHPConsulAPI\Catalog */ -class CatalogClient extends AbstractClient -{ +class CatalogClient extends AbstractClient { /** * @param \DCarbone\PHPConsulAPI\Catalog\CatalogRegistration $catalogRegistration * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions * @return array( - * @type \DCarbone\PHPConsulAPI\WriteMeta write meta data - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\WriteMeta write meta data + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function register(CatalogRegistration $catalogRegistration, WriteOptions $writeOptions = null) - { + public function register(CatalogRegistration $catalogRegistration, WriteOptions $writeOptions = null) { $r = new Request('put', 'v1/catalog/register', $this->c, $catalogRegistration); $r->setWriteOptions($writeOptions); list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -51,37 +50,37 @@ public function register(CatalogRegistration $catalogRegistration, WriteOptions * @param \DCarbone\PHPConsulAPI\Catalog\CatalogDeregistration $catalogDeregistration * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions * @return array( - * @type \DCarbone\PHPConsulAPI\WriteMeta write meta data - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\WriteMeta write meta data + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function deregister(CatalogDeregistration $catalogDeregistration, WriteOptions $writeOptions = null) - { + public function deregister(CatalogDeregistration $catalogDeregistration, WriteOptions $writeOptions = null) { $r = new Request('put', 'v1/catalog/deregister', $this->c, $catalogDeregistration); $r->setWriteOptions($writeOptions); list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } /** * @return array( - * @type string[]|null list of datacenters or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type string[]|null list of datacenters or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function datacenters() - { + public function datacenters() { $r = new Request('get', 'v1/catalog/datacenters', $this->c); /** @var \Psr\Http\Message\ResponseInterface $response */ list($_, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return $this->decodeBody($response->getBody()); } @@ -89,31 +88,31 @@ public function datacenters() /** * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type CatalogNode[]|null array of catalog nodes or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type CatalogNode[]|null array of catalog nodes or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function nodes(QueryOptions $queryOptions = null) - { + public function nodes(QueryOptions $queryOptions = null) { $r = new Request('get', 'v1/catalog/nodes', $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $nodes = []; - foreach($data as $v) - { + foreach ($data as $v) { $node = new CatalogNode($v); $nodes[$node->Node] = $node; } @@ -129,15 +128,15 @@ public function nodes(QueryOptions $queryOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function services(QueryOptions $queryOptions = null) - { + public function services(QueryOptions $queryOptions = null) { $r = new Request('get', 'v1/catalog/services', $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); @@ -151,33 +150,34 @@ public function services(QueryOptions $queryOptions = null) * @param string $tag * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type CatalogService[]|null array of services or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type CatalogService[]|null array of services or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function service($service, $tag = '', QueryOptions $queryOptions = null) - { + public function service($service, $tag = '', QueryOptions $queryOptions = null) { $r = new Request('get', sprintf('v1/catalog/service/%s', $service), $this->c); $r->setQueryOptions($queryOptions); - if ('' !== $tag) + if ('' !== $tag) { $r->params->set('tag', $tag); + } /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - - if (null !== $err) + + if (null !== $err) { return [null, $qm, $err]; - + } + $services = []; - foreach($data as $v) - { + foreach ($data as $v) { $service = new CatalogService($v); $services[$service->ServiceID] = $service; } @@ -189,27 +189,28 @@ public function service($service, $tag = '', QueryOptions $queryOptions = null) * @param string $node * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type CatalogNode node or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type CatalogNode node or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function node($node, QueryOptions $queryOptions = null) - { + public function node($node, QueryOptions $queryOptions = null) { $r = new Request('get', sprintf('v1/catalog/node/%s', $node), $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } return [new CatalogNode($data), $qm, null]; } diff --git a/src/Catalog/CatalogDeregistration.php b/src/Catalog/CatalogDeregistration.php index e4c0fc2..52baa20 100644 --- a/src/Catalog/CatalogDeregistration.php +++ b/src/Catalog/CatalogDeregistration.php @@ -22,8 +22,7 @@ * Class CatalogDeregistration * @package DCarbone\PHPConsulAPI\Catalog */ -class CatalogDeregistration extends AbstractModel -{ +class CatalogDeregistration extends AbstractModel { /** @var string */ public $Node = ''; /** @var string */ @@ -38,8 +37,7 @@ class CatalogDeregistration extends AbstractModel /** * @return string */ - public function getNode() - { + public function getNode() { return $this->Node; } @@ -47,8 +45,7 @@ public function getNode() * @param string $Node * @return \DCarbone\PHPConsulAPI\Catalog\CatalogDeregistration */ - public function setNode($Node) - { + public function setNode($Node) { $this->Node = $Node; return $this; } @@ -56,8 +53,7 @@ public function setNode($Node) /** * @return string */ - public function getAddress() - { + public function getAddress() { return $this->Address; } @@ -65,8 +61,7 @@ public function getAddress() * @param string $Address * @return \DCarbone\PHPConsulAPI\Catalog\CatalogDeregistration */ - public function setAddress($Address) - { + public function setAddress($Address) { $this->Address = $Address; return $this; } @@ -74,8 +69,7 @@ public function setAddress($Address) /** * @return string */ - public function getDatacenter() - { + public function getDatacenter() { return $this->Datacenter; } @@ -83,8 +77,7 @@ public function getDatacenter() * @param string $Datacenter * @return \DCarbone\PHPConsulAPI\Catalog\CatalogDeregistration */ - public function setDatacenter($Datacenter) - { + public function setDatacenter($Datacenter) { $this->Datacenter = $Datacenter; return $this; } @@ -92,8 +85,7 @@ public function setDatacenter($Datacenter) /** * @return string */ - public function getServiceID() - { + public function getServiceID() { return $this->ServiceID; } @@ -101,8 +93,7 @@ public function getServiceID() * @param string $ServiceID * @return \DCarbone\PHPConsulAPI\Catalog\CatalogDeregistration */ - public function setServiceID($ServiceID) - { + public function setServiceID($ServiceID) { $this->ServiceID = $ServiceID; return $this; } @@ -110,8 +101,7 @@ public function setServiceID($ServiceID) /** * @return string */ - public function getCheckID() - { + public function getCheckID() { return $this->CheckID; } @@ -119,8 +109,7 @@ public function getCheckID() * @param string $CheckID * @return \DCarbone\PHPConsulAPI\Catalog\CatalogDeregistration */ - public function setCheckID($CheckID) - { + public function setCheckID($CheckID) { $this->CheckID = $CheckID; return $this; } diff --git a/src/Catalog/CatalogNode.php b/src/Catalog/CatalogNode.php index bddcb4d..8e9ec89 100644 --- a/src/Catalog/CatalogNode.php +++ b/src/Catalog/CatalogNode.php @@ -21,8 +21,7 @@ * Class CatalogNode * @package DCarbone\PHPConsulAPI\Catalog */ -class CatalogNode extends AbstractModel -{ +class CatalogNode extends AbstractModel { /** @var string */ public $Node = ''; /** @var string */ @@ -31,16 +30,14 @@ class CatalogNode extends AbstractModel /** * @return string */ - public function getNode() - { + public function getNode() { return $this->Node; } /** * @return string */ - public function getAddress() - { + public function getAddress() { return $this->Address; } } \ No newline at end of file diff --git a/src/Catalog/CatalogRegistration.php b/src/Catalog/CatalogRegistration.php index 2387571..93987fb 100644 --- a/src/Catalog/CatalogRegistration.php +++ b/src/Catalog/CatalogRegistration.php @@ -24,8 +24,7 @@ * Class CatalogRegistration * @package DCarbone\PHPConsulAPI\Catalog */ -class CatalogRegistration extends AbstractModel -{ +class CatalogRegistration extends AbstractModel { /** @var string */ public $Node = ''; /** @var string */ @@ -42,20 +41,20 @@ class CatalogRegistration extends AbstractModel * * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); - if (null !== $this->Service && !($this->Service instanceof AgentService)) + if (null !== $this->Service && !($this->Service instanceof AgentService)) { $this->Service = new AgentService((array)$this->Service); - if (null !== $this->Check && !($this->Check instanceof AgentCheck)) + } + if (null !== $this->Check && !($this->Check instanceof AgentCheck)) { $this->Check = new AgentCheck((array)$this->Check); + } } /** * @return string */ - public function getNode() - { + public function getNode() { return $this->Node; } @@ -63,8 +62,7 @@ public function getNode() * @param string $Node * @return \DCarbone\PHPConsulAPI\Catalog\CatalogRegistration */ - public function setNode($Node) - { + public function setNode($Node) { $this->Node = $Node; return $this; } @@ -72,8 +70,7 @@ public function setNode($Node) /** * @return string */ - public function getAddress() - { + public function getAddress() { return $this->Address; } @@ -81,8 +78,7 @@ public function getAddress() * @param string $Address * @return \DCarbone\PHPConsulAPI\Catalog\CatalogRegistration */ - public function setAddress($Address) - { + public function setAddress($Address) { $this->Address = $Address; return $this; } @@ -90,8 +86,7 @@ public function setAddress($Address) /** * @return string */ - public function getDatacenter() - { + public function getDatacenter() { return $this->Datacenter; } @@ -99,8 +94,7 @@ public function getDatacenter() * @param string $Datacenter * @return \DCarbone\PHPConsulAPI\Catalog\CatalogRegistration */ - public function setDatacenter($Datacenter) - { + public function setDatacenter($Datacenter) { $this->Datacenter = $Datacenter; return $this; } @@ -108,8 +102,7 @@ public function setDatacenter($Datacenter) /** * @return \DCarbone\PHPConsulAPI\Agent\AgentService */ - public function getService() - { + public function getService() { return $this->Service; } @@ -117,8 +110,7 @@ public function getService() * @param \DCarbone\PHPConsulAPI\Agent\AgentService $Service * @return \DCarbone\PHPConsulAPI\Catalog\CatalogRegistration */ - public function setService(AgentService $Service) - { + public function setService(AgentService $Service) { $this->Service = $Service; return $this; } @@ -126,8 +118,7 @@ public function setService(AgentService $Service) /** * @return \DCarbone\PHPConsulAPI\Agent\AgentCheck */ - public function getCheck() - { + public function getCheck() { return $this->Check; } @@ -135,8 +126,7 @@ public function getCheck() * @param \DCarbone\PHPConsulAPI\Agent\AgentCheck $Check * @return \DCarbone\PHPConsulAPI\Catalog\CatalogRegistration */ - public function setCheck(AgentCheck $Check) - { + public function setCheck(AgentCheck $Check) { $this->Check = $Check; return $this; } diff --git a/src/Catalog/CatalogService.php b/src/Catalog/CatalogService.php index 1b7fdee..ca0b6cd 100644 --- a/src/Catalog/CatalogService.php +++ b/src/Catalog/CatalogService.php @@ -20,8 +20,7 @@ * Class CatalogService * @package DCarbone\PHPConsulAPI\Catalog */ -class CatalogService extends CatalogNode -{ +class CatalogService extends CatalogNode { /** @var string */ public $ServiceID = ''; /** @var string */ @@ -38,48 +37,42 @@ class CatalogService extends CatalogNode /** * @return string */ - public function getServiceID() - { + public function getServiceID() { return $this->ServiceID; } /** * @return string */ - public function getServiceName() - { + public function getServiceName() { return $this->ServiceName; } /** * @return string */ - public function getServiceAddress() - { + public function getServiceAddress() { return $this->ServiceAddress; } /** * @return string[] */ - public function getServiceTags() - { + public function getServiceTags() { return $this->ServiceTags; } /** * @return int */ - public function getServicePort() - { + public function getServicePort() { return $this->ServicePort; } /** * @return boolean */ - public function isServiceEnableTagOverride() - { + public function isServiceEnableTagOverride() { return $this->ServiceEnableTagOverride; } } \ No newline at end of file diff --git a/src/Config.php b/src/Config.php index 3ebaeaf..82d9eb5 100644 --- a/src/Config.php +++ b/src/Config.php @@ -22,8 +22,7 @@ * Class Config * @package DCarbone\PHPConsulAPI */ -class Config -{ +class Config { /** * The address, including port, of your Consul Agent * @@ -93,15 +92,14 @@ class Config * Config constructor. * @param array $config */ - public function __construct(array $config = []) - { - foreach($config as $k => $v) - { + public function __construct(array $config = []) { + foreach ($config as $k => $v) { $this->{"set{$k}"}($v); } - if (null !== $this->HttpAuth && !isset($this->HttpAuth)) + if (null !== $this->HttpAuth && !isset($this->HttpAuth)) { $this->HttpAuth = new HttpAuth(); + } } /** @@ -110,8 +108,7 @@ public function __construct(array $config = []) * @param \Http\Client\HttpClient $client * @return \DCarbone\PHPConsulAPI\Config */ - public static function newDefaultConfigWithClient(HttpClient $client) - { + public static function newDefaultConfigWithClient(HttpClient $client) { $conf = new static([ 'Address' => '127.0.0.1:8500', 'Scheme' => 'http', @@ -119,20 +116,25 @@ public static function newDefaultConfigWithClient(HttpClient $client) ]); $envParams = static::getEnvironmentConfig(); - if (isset($envParams[Consul::HTTPAddrEnvName])) + if (isset($envParams[Consul::HTTPAddrEnvName])) { $conf->setAddress($envParams[Consul::HTTPAddrEnvName]); + } - if (isset($envParams[Consul::HTTPTokenEnvName])) + if (isset($envParams[Consul::HTTPTokenEnvName])) { $conf->setToken($envParams[Consul::HTTPTokenEnvName]); + } - if (isset($envParams[Consul::HTTPAuthEnvName])) + if (isset($envParams[Consul::HTTPAuthEnvName])) { $conf->setHttpAuth($envParams[Consul::HTTPAuthEnvName]); + } - if (isset($envParams[Consul::HTTPSSLEnvName]) && $envParams[Consul::HTTPSSLEnvName]) + if (isset($envParams[Consul::HTTPSSLEnvName]) && $envParams[Consul::HTTPSSLEnvName]) { $conf->setScheme('https'); + } - if (isset($envParams[Consul::HTTPSSLVerifyEnvName]) && !$envParams[Consul::HTTPSSLVerifyEnvName]) + if (isset($envParams[Consul::HTTPSSLVerifyEnvName]) && !$envParams[Consul::HTTPSSLVerifyEnvName]) { $conf->setInsecureSkipVerify(false); + } return $conf; } @@ -142,8 +144,7 @@ public static function newDefaultConfigWithClient(HttpClient $client) * * @return \DCarbone\PHPConsulAPI\Config */ - public static function newDefaultConfig() - { + public static function newDefaultConfig() { static $knownClients = array( '\\Http\\Client\\Curl\\Client', '\\Http\\Adapter\\Guzzle6\\Client', @@ -151,10 +152,10 @@ public static function newDefaultConfig() '\\Http\\Adapter\\Buzz\\Client' ); - foreach($knownClients as $clientClass) - { - if (class_exists($clientClass, true)) + foreach ($knownClients as $clientClass) { + if (class_exists($clientClass, true)) { return static::newDefaultConfigWithClient(new $clientClass); + } } throw new \RuntimeException(sprintf( @@ -166,8 +167,7 @@ public static function newDefaultConfig() /** * @return string */ - public function getAddress() - { + public function getAddress() { return $this->Address; } @@ -175,8 +175,7 @@ public function getAddress() * @param string $Address * @return \DCarbone\PHPConsulAPI\Config */ - public function setAddress($Address) - { + public function setAddress($Address) { $this->Address = $Address; return $this; } @@ -184,8 +183,7 @@ public function setAddress($Address) /** * @return string */ - public function getScheme() - { + public function getScheme() { return $this->Scheme; } @@ -193,8 +191,7 @@ public function getScheme() * @param string $Scheme * @return \DCarbone\PHPConsulAPI\Config */ - public function setScheme($Scheme) - { + public function setScheme($Scheme) { $this->Scheme = $Scheme; return $this; } @@ -202,8 +199,7 @@ public function setScheme($Scheme) /** * @return string */ - public function getDatacenter() - { + public function getDatacenter() { return $this->Datacenter; } @@ -211,8 +207,7 @@ public function getDatacenter() * @param string $Datacenter * @return \DCarbone\PHPConsulAPI\Config */ - public function setDatacenter($Datacenter) - { + public function setDatacenter($Datacenter) { $this->Datacenter = $Datacenter; return $this; } @@ -220,8 +215,7 @@ public function setDatacenter($Datacenter) /** * @return int */ - public function getWaitTime() - { + public function getWaitTime() { return $this->WaitTime; } @@ -229,8 +223,7 @@ public function getWaitTime() * @param int $WaitTime * @return \DCarbone\PHPConsulAPI\Config */ - public function setWaitTime($WaitTime) - { + public function setWaitTime($WaitTime) { $this->WaitTime = $WaitTime; return $this; } @@ -238,8 +231,7 @@ public function setWaitTime($WaitTime) /** * @return string */ - public function getToken() - { + public function getToken() { return $this->Token; } @@ -247,8 +239,7 @@ public function getToken() * @param string $Token * @return \DCarbone\PHPConsulAPI\Config */ - public function setToken($Token) - { + public function setToken($Token) { $this->Token = $Token; return $this; } @@ -256,8 +247,7 @@ public function setToken($Token) /** * @return boolean */ - public function isInsecureSkipVerify() - { + public function isInsecureSkipVerify() { return $this->InsecureSkipVerify; } @@ -265,8 +255,7 @@ public function isInsecureSkipVerify() * @param boolean $InsecureSkipVerify * @return \DCarbone\PHPConsulAPI\Config */ - public function setInsecureSkipVerify($InsecureSkipVerify) - { + public function setInsecureSkipVerify($InsecureSkipVerify) { $this->InsecureSkipVerify = $InsecureSkipVerify; return $this; } @@ -274,8 +263,7 @@ public function setInsecureSkipVerify($InsecureSkipVerify) /** * @return \DCarbone\PHPConsulAPI\HttpAuth */ - public function getHttpAuth() - { + public function getHttpAuth() { return $this->HttpAuth; } @@ -283,26 +271,20 @@ public function getHttpAuth() * @param string|HttpAuth $HttpAuth * @return \DCarbone\PHPConsulAPI\Config */ - public function setHttpAuth($HttpAuth) - { - if (is_string($HttpAuth)) - { + public function setHttpAuth($HttpAuth) { + if (is_string($HttpAuth)) { $colon = strpos($HttpAuth, ':'); - if (false === $colon) - { + if (false === $colon) { $username = $HttpAuth; $password = null; - } - else - { + } else { $username = substr($HttpAuth, 0, $colon); $password = substr($HttpAuth, $colon + 1); } $HttpAuth = new HttpAuth($username, $password); } - if ($HttpAuth instanceof HttpAuth) - { + if ($HttpAuth instanceof HttpAuth) { $this->HttpAuth = $HttpAuth; return $this; } @@ -317,8 +299,7 @@ public function setHttpAuth($HttpAuth) /** * @return \Http\Client\HttpClient */ - public function getHttpClient() - { + public function getHttpClient() { return $this->HttpClient; } @@ -326,8 +307,7 @@ public function getHttpClient() * @param \Http\Client\HttpClient $HttpClient * @return \DCarbone\PHPConsulAPI\Config */ - public function setHttpClient(HttpClient $HttpClient) - { + public function setHttpClient(HttpClient $HttpClient) { $this->HttpClient = $HttpClient; return $this; } @@ -335,8 +315,7 @@ public function setHttpClient(HttpClient $HttpClient) /** * @return boolean */ - public function isTokenInHeader() - { + public function isTokenInHeader() { return $this->TokenInHeader; } @@ -344,36 +323,56 @@ public function isTokenInHeader() * @param boolean $TokenInHeader * @return \DCarbone\PHPConsulAPI\Config */ - public function setTokenInHeader($TokenInHeader) - { + public function setTokenInHeader($TokenInHeader) { $this->TokenInHeader = (bool)$TokenInHeader; return $this; } + /** + * @param int $in + * @return string + */ + public function intToMillisecond($in) { + if (!is_int($in)) { + throw new \InvalidArgumentException(sprintf('$in must be integer, saw "%s".', gettype($in))); + } + + $ms = intval($in / 1000000, 10); + + if (0 < $in && 0 === $ms) { + $ms = 1; + } + + return sprintf('%dms', $ms); + } + /** * @return array */ - public static function getEnvironmentConfig() - { + public static function getEnvironmentConfig() { return array_filter([ 'CONSUL_HTTP_ADDR' => static::_tryGetEnvParam('CONSUL_HTTP_ADDR'), 'CONSUL_HTTP_AUTH' => static::_tryGetEnvParam('CONSUL_HTTP_AUTH'), 'CONSUL_HTTP_SSL' => static::_tryGetEnvParam('CONSUL_HTTP_SSL'), 'CONSUL_HTTP_SSL_VERIFY' => static::_tryGetEnvParam('CONSUL_HTTP_SSL_VERIFY') - ], function($val) { return null !== $val; }); + ], + function ($val) { + return null !== $val; + }); } /** * @param string $param * @return string|null */ - protected static function _tryGetEnvParam($param) - { - if (false !== ($value = getenv($param))) + protected static function _tryGetEnvParam($param) { + if (false !== ($value = getenv($param))) { return $value; + } - if (isset($_SERVER[$param])) + if (isset($_SERVER[$param])) { return $_SERVER[$param]; + } return null; } diff --git a/src/Consul.php b/src/Consul.php index ae15bf6..324c730 100644 --- a/src/Consul.php +++ b/src/Consul.php @@ -32,8 +32,7 @@ * Class Consul * @package DCarbone\PHPConsulAPI */ -class Consul -{ +class Consul { const HTTPAddrEnvName = 'CONSUL_HTTP_ADDR'; const HTTPTokenEnvName = 'CONSUL_HTTP_TOKEN'; const HTTPAuthEnvName = 'CONSUL_HTTP_AUTH'; @@ -67,18 +66,19 @@ class Consul * Client constructor. * @param Config $config */ - public function __construct(Config $config = null) - { + public function __construct(Config $config = null) { if (null === $config) { $config = Config::newDefaultConfig(); } else { $def = Config::newDefaultConfig(); - if ('' === $config->getAddress()) + if ('' === $config->getAddress()) { $config->setAddress($def->getAddress()); + } - if ('' === $config->getScheme()) + if ('' === $config->getScheme()) { $config->setScheme($def->getScheme()); + } } $this->KV = new KVClient($config); @@ -97,88 +97,77 @@ public function __construct(Config $config = null) /** * @return \DCarbone\PHPConsulAPI\KV\KVClient */ - public function KV() - { + public function KV() { return $this->KV; } /** * @return \DCarbone\PHPConsulAPI\Agent\AgentClient */ - public function Agent() - { + public function Agent() { return $this->Agent; } /** * @return \DCarbone\PHPConsulAPI\Catalog\CatalogClient */ - public function Catalog() - { + public function Catalog() { return $this->Catalog; } /** * @return \DCarbone\PHPConsulAPI\Status\StatusClient */ - public function Status() - { + public function Status() { return $this->Status; } /** * @return \DCarbone\PHPConsulAPI\Event\EventClient */ - public function Event() - { + public function Event() { return $this->Event; } /** * @return \DCarbone\PHPConsulAPI\Health\HealthClient */ - public function Health() - { + public function Health() { return $this->Health; } /** * @return \DCarbone\PHPConsulAPI\Coordinate\CoordinateClient */ - public function Coordinate() - { + public function Coordinate() { return $this->Coordinate; } /** * @return \DCarbone\PHPConsulAPI\Session\SessionClient */ - public function Session() - { + public function Session() { return $this->Session; } /** * @return \DCarbone\PHPConsulAPI\Operator\OperatorClient */ - public function Operator() - { + public function Operator() { return $this->Operator; } /** * @return \DCarbone\PHPConsulAPI\ACL\ACLClient */ - public function ACL() - { + public function ACL() { return $this->ACL; } /** * @return \DCarbone\PHPConsulAPI\PreparedQuery\PreparedQueryClient */ - public function PreparedQuery() - { + public function PreparedQuery() { return $this->PreparedQuery; } } diff --git a/src/ConsulHttpParamContainerTrait.php b/src/ConsulHttpParamContainerTrait.php deleted file mode 100644 index e193fd9..0000000 --- a/src/ConsulHttpParamContainerTrait.php +++ /dev/null @@ -1,67 +0,0 @@ - $v) - { - if (null !== $v) - { - if ('Datacenter' === $k) - $params['dc'] = $v; - else if ('AllowStale' === $k) - $params['stale'] = $v; - else if ('RequireConsistent' === $k) - $params['consistent'] = $v; - else if ('WaitIndex' === $k) - $params['index'] = $v; - else if ('WaitTime' === $k) - $params['wait'] = $v; - else - $params[strtolower($k)] = $v; - } - } - return $params; - } - - /** - * @return string - */ - public function buildHttpQueryString() - { - return (string)$this; - } - - /** - * @return string - */ - public function __toString() - { - return http_build_query($this->buildHttpQueryArray()); - } -} diff --git a/src/Coordinate/Coordinate.php b/src/Coordinate/Coordinate.php index 6436228..893a0e3 100644 --- a/src/Coordinate/Coordinate.php +++ b/src/Coordinate/Coordinate.php @@ -22,8 +22,7 @@ * Class Coordinate * @package DCarbone\PHPConsulAPI\Coordinate */ -class Coordinate extends AbstractModel -{ +class Coordinate extends AbstractModel { /** @var int[] */ public $Vec = []; /** @var float */ @@ -36,32 +35,28 @@ class Coordinate extends AbstractModel /** * @return int[] */ - public function getVec() - { + public function getVec() { return $this->Vec; } /** * @return float */ - public function getError() - { + public function getError() { return $this->Error; } /** * @return float */ - public function getAdjustment() - { + public function getAdjustment() { return $this->Adjustment; } /** * @return float */ - public function getHeight() - { + public function getHeight() { return $this->Height; } } \ No newline at end of file diff --git a/src/Coordinate/CoordinateClient.php b/src/Coordinate/CoordinateClient.php index 178de0b..5644f3f 100644 --- a/src/Coordinate/CoordinateClient.php +++ b/src/Coordinate/CoordinateClient.php @@ -24,32 +24,31 @@ * Class CoordinateClient * @package DCarbone\PHPConsulAPI\Coordinate */ -class CoordinateClient extends AbstractClient -{ +class CoordinateClient extends AbstractClient { /** * @return array( - * @type \DCarbone\PHPConsulAPI\Coordinate\CoordinateDatacenterMap[]|null datacenter map or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if an + * @type \DCarbone\PHPConsulAPI\Coordinate\CoordinateDatacenterMap[]|null datacenter map or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if an * ) */ - public function datacenters() - { + public function datacenters() { $r = new Request('get', 'v1/coordinate/datacenters', $this->c); /** @var \Psr\Http\Message\ResponseInterface $response */ list($_, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } $datacenters = []; - foreach($data as $v) - { + foreach ($data as $v) { $datacenters[] = new CoordinateDatacenterMap($v); } @@ -59,30 +58,30 @@ public function datacenters() /** * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type \DCarbone\PHPConsulAPI\Coordinate\CoordinateEntry[]|null coordinate list or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\Coordinate\CoordinateEntry[]|null coordinate list or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function nodes(QueryOptions $queryOptions = null) - { + public function nodes(QueryOptions $queryOptions = null) { $r = new Request('get', 'v1/coordinate/nodes', $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list ($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $coordinates = []; - foreach($data as $coord) - { + foreach ($data as $coord) { $coordinates[] = new CoordinateEntry($coord); } diff --git a/src/Coordinate/CoordinateDatacenterMap.php b/src/Coordinate/CoordinateDatacenterMap.php index 4894367..721ac55 100644 --- a/src/Coordinate/CoordinateDatacenterMap.php +++ b/src/Coordinate/CoordinateDatacenterMap.php @@ -22,8 +22,7 @@ * Class CoordinateDatacenterMap * @package DCarbone\PHPConsulAPI\Coordinate */ -class CoordinateDatacenterMap extends AbstractModel -{ +class CoordinateDatacenterMap extends AbstractModel { /** @var string */ public $Datacenter = ''; /** @var \DCarbone\PHPConsulAPI\Coordinate\Coordinate[] */ @@ -34,24 +33,21 @@ class CoordinateDatacenterMap extends AbstractModel * * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); // If we have data... - if (0 < count($this->Coordinates)) - { + if (0 < count($this->Coordinates)) { // ...remove null $this->Coordinates = array_filter($this->Coordinates); // ...and if we still have data - if (0 < ($cnt = count($this->Coordinates))) - { + if (0 < ($cnt = count($this->Coordinates))) { // ensure we have objects. - for ($i = 0; $i < $cnt; $i++) - { - if (!($this->Coordinates[$i] instanceof Coordinate)) + for ($i = 0; $i < $cnt; $i++) { + if (!($this->Coordinates[$i] instanceof Coordinate)) { $this->Coordinates[$i] = new Coordinate((array)$this->Coordinates[$i]); + } } } } @@ -60,16 +56,14 @@ public function __construct(array $data = []) /** * @return string */ - public function getDatacenter() - { + public function getDatacenter() { return $this->Datacenter; } /** * @return \DCarbone\PHPConsulAPI\Coordinate\Coordinate[] */ - public function getCoordinates() - { + public function getCoordinates() { return $this->Coordinates; } } \ No newline at end of file diff --git a/src/Coordinate/CoordinateEntry.php b/src/Coordinate/CoordinateEntry.php index 6c078d2..d765846 100644 --- a/src/Coordinate/CoordinateEntry.php +++ b/src/Coordinate/CoordinateEntry.php @@ -22,8 +22,7 @@ * Class CoordinateEntry * @package DCarbone\PHPConsulAPI\Coordinate */ -class CoordinateEntry extends AbstractModel -{ +class CoordinateEntry extends AbstractModel { /** @var string */ public $Node = ''; /** @var \DCarbone\PHPConsulAPI\Coordinate\Coordinate */ @@ -33,27 +32,25 @@ class CoordinateEntry extends AbstractModel * CoordinateEntry constructor. * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); - if (null !== $this->Coord && !($this->Coord instanceof Coordinate)) + if (null !== $this->Coord && !($this->Coord instanceof Coordinate)) { $this->Coord = new Coordinate((array)$this->Coord); + } } /** * @return string */ - public function getNode() - { + public function getNode() { return $this->Node; } /** * @return \DCarbone\PHPConsulAPI\Coordinate\Coordinate */ - public function getCoord() - { + public function getCoord() { return $this->Coord; } } \ No newline at end of file diff --git a/src/DateTime.php b/src/DateTime.php index f4898a2..bf58355 100644 --- a/src/DateTime.php +++ b/src/DateTime.php @@ -20,34 +20,29 @@ * Class DateTime * @package DCarbone\PHPConsulAPI */ -class DateTime extends \DateTime implements \JsonSerializable -{ +class DateTime extends \DateTime implements \JsonSerializable { /** @var string */ private static $_defaultFormat = 'Y-m-d\TH:i:s.uO'; /** * @return string */ - public static function getDefaultFormat() - { + public static function getDefaultFormat() { return self::$_defaultFormat; } /** * @param string $format */ - public static function setDefaultFormat($format) - { - if (!is_string($format)) - { + public static function setDefaultFormat($format) { + if (!is_string($format)) { throw new \InvalidArgumentException(sprintf( 'PHPConsulAPI - DateTime::setDefaultFormat expects argument 1 to be string, %s seen.', gettype($format) )); } - if (false === @date($format)) - { + if (false === @date($format)) { throw new \InvalidArgumentException(sprintf( 'PHPConsulAPI - DateTime::setDefaultFormat specified invalid format "%s". Please see http://php.net/manual/en/function.date.php.', $format @@ -60,32 +55,28 @@ public static function setDefaultFormat($format) /** * @return string */ - public function formatDefault() - { + public function formatDefault() { return $this->format(self::$_defaultFormat); } /** * @return string */ - public static function now() - { + public static function now() { return date(self::$_defaultFormat); } /** * @return string */ - public function jsonSerialize() - { + public function jsonSerialize() { return (string)$this; } /** * @return string */ - public function __toString() - { + public function __toString() { return $this->format(self::$_defaultFormat); } } \ No newline at end of file diff --git a/src/Error.php b/src/Error.php index 5802f4d..e82873c 100644 --- a/src/Error.php +++ b/src/Error.php @@ -18,12 +18,11 @@ /** * TODO: Make this better... - * + * * Class Error * @package DCarbone\PHPConsulAPI */ -class Error implements \JsonSerializable -{ +class Error implements \JsonSerializable { /** @var DateTime */ private $_timestamp; @@ -34,8 +33,7 @@ class Error implements \JsonSerializable * Error constructor. * @param string $message */ - public function __construct($message) - { + public function __construct($message) { $this->_timestamp = new DateTime(); $this->_message = $message; } @@ -43,16 +41,14 @@ public function __construct($message) /** * @return DateTime */ - public function getTimestamp() - { + public function getTimestamp() { return $this->_timestamp; } /** * @return string */ - public function getMessage() - { + public function getMessage() { return $this->_message; } @@ -61,8 +57,7 @@ public function getMessage() * @link http://php.net/manual/en/jsonserializable.jsonserialize.php * @return mixed data which can be serialized by json_encode, which is a value of any type other than a resource. */ - public function jsonSerialize() - { + public function jsonSerialize() { return array( 'message' => $this->_message, 'timestamp' => $this->_timestamp @@ -72,8 +67,7 @@ public function jsonSerialize() /** * @return string */ - public function __toString() - { + public function __toString() { return sprintf( '[error] - %s - %s', $this->_timestamp, diff --git a/src/Event/EventClient.php b/src/Event/EventClient.php index a15dbf8..cc609b1 100644 --- a/src/Event/EventClient.php +++ b/src/Event/EventClient.php @@ -25,79 +25,83 @@ * Class EventClient * @package DCarbone\PHPConsulAPI\Event */ -class EventClient extends AbstractClient -{ +class EventClient extends AbstractClient { /** * @param \DCarbone\PHPConsulAPI\Event\UserEvent $event * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions * @return array( - * @type UserEvent|null user event that was fired or null on error - * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata - * @type \DCarbone\PHPConsulAPI\Error error, if any + * @type UserEvent|null user event that was fired or null on error + * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata + * @type \DCarbone\PHPConsulAPI\Error error, if any * ) */ - public function fire(UserEvent $event, WriteOptions $writeOptions = null) - { + public function fire(UserEvent $event, WriteOptions $writeOptions = null) { $r = new Request( 'put', - sprintf('v1/event/fire/%s', $event->Name), - $this->c, - '' !== $event->Payload ? $event->Payload : null); + sprintf('v1/event/fire/%s', $event->Name), + $this->c, + '' !== $event->Payload ? $event->Payload : null); $r->setWriteOptions($writeOptions); - if ('' !== ($nf = $event->NodeFilter)) + if ('' !== ($nf = $event->NodeFilter)) { $r->params->set('node', $nf); - if ('' !== ($sf = $event->ServiceFilter)) + } + if ('' !== ($sf = $event->ServiceFilter)) { $r->params->set('service', $sf); - if ('' !== ($tf = $event->TagFilter)) + } + if ('' !== ($tf = $event->TagFilter)) { $r->params->set('tag', $tf); + } /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $wm = $this->buildWriteMeta($duration); list($data, $err) = $this->decodeBody($response->getBody()); - if ($err !== null) + if ($err !== null) { return [null, $wm, $err]; + } return [new UserEvent($data), $wm, null]; } - + /** * @param string $name * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type UserEvent[] list of user events or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata - * @type \DCarbone\PHPConsulAPI\Error error, if any + * @type UserEvent[] list of user events or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata + * @type \DCarbone\PHPConsulAPI\Error error, if any * ) */ - public function eventList($name = '', QueryOptions $queryOptions = null) - { + public function eventList($name = '', QueryOptions $queryOptions = null) { $r = new Request('get', 'v1/event/list', $this->c); - if ('' !== (string)$name) + if ('' !== (string)$name) { $r->params->set('name', $name); + } $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $events = []; - foreach($data as $event) - { + foreach ($data as $event) { $events[] = new UserEvent($event); } diff --git a/src/Event/UserEvent.php b/src/Event/UserEvent.php index 23a6086..80921f2 100644 --- a/src/Event/UserEvent.php +++ b/src/Event/UserEvent.php @@ -23,8 +23,7 @@ * Class UserEvent * @package DCarbone\PHPConsulAPI\Event */ -class UserEvent extends AbstractModel -{ +class UserEvent extends AbstractModel { /** @var string */ public $ID = ''; /** @var string */ @@ -48,74 +47,66 @@ class UserEvent extends AbstractModel * @param array $data * @param bool $_decodeValue */ - public function __construct(array $data = [], $_decodeValue = false) - { + public function __construct(array $data = [], $_decodeValue = false) { parent::__construct($data); - if ((bool)$_decodeValue && isset($this->Payload)) + if ((bool)$_decodeValue && isset($this->Payload)) { $this->Payload = base64_decode($this->Payload); + } } /** * @return string */ - public function getID() - { + public function getID() { return $this->ID; } /** * @return string */ - public function getName() - { + public function getName() { return $this->Name; } /** * @return string */ - public function getPayload() - { + public function getPayload() { return $this->Payload; } /** * @return string */ - public function getNodeFilter() - { + public function getNodeFilter() { return $this->NodeFilter; } /** * @return string */ - public function getServiceFilter() - { + public function getServiceFilter() { return $this->ServiceFilter; } /** * @return string */ - public function getTagFilter() - { + public function getTagFilter() { return $this->TagFilter; } /** * @return int */ - public function getVersion() - { + public function getVersion() { return $this->Version; } /** * @return int */ - public function getLTime() - { + public function getLTime() { return $this->LTime; } } \ No newline at end of file diff --git a/src/FileDebugLogger.php b/src/FileDebugLogger.php deleted file mode 100644 index 47de892..0000000 --- a/src/FileDebugLogger.php +++ /dev/null @@ -1,96 +0,0 @@ -_exceptionOnError = (bool)$exceptionOnError; - - $file = __DIR__ . '/../var/logs/php-consul-api.log'; - - if (false === @file_exists($file) && false === (bool)@file_put_contents($file, "\n")) - { - throw new \InvalidArgumentException(sprintf( - 'FileLogger - Unable to create file at path "%s"', - $file - )); - } - - $this->_file = $file; - } - - /** - * Logs with an arbitrary level. - * - * @param mixed $level - * @param string $message - * @param array $context - * - * @throws \Exception - * - * @return null - */ - public function log($level, $message, array $context = []) - { - if (file_exists($this->_file) && is_writable($this->_file)) - { - // Try to write out line - file_put_contents( - $this->_file, - sprintf( - "[%s] %s - %s\n", - $level, - DateTime::now(), - $message - ), - FILE_APPEND - ); - } - else - { - $msg = sprintf( - 'FileLogger - Specified file "%s" could not be opened for writing.', - $this->_file - ); - - if ($this->_exceptionOnError) - throw new \Exception($msg); - - trigger_error($msg, E_USER_ERROR); - } - - return null; - } -} \ No newline at end of file diff --git a/src/HasSettableStringTags.php b/src/HasSettableStringTags.php index 1fd9e05..6fc0755 100644 --- a/src/HasSettableStringTags.php +++ b/src/HasSettableStringTags.php @@ -20,14 +20,12 @@ * Class HasSettableStringTags * @package DCarbone\PHPConsulAPI */ -trait HasSettableStringTags -{ +trait HasSettableStringTags { /** * @param string[] $Tags * @return $this */ - public function setTags(array $Tags) - { + public function setTags(array $Tags) { $this->Tags = $Tags; return $this; } @@ -36,8 +34,7 @@ public function setTags(array $Tags) * @param string $Tag * @return $this */ - public function addTag($Tag) - { + public function addTag($Tag) { $this->Tags[] = $Tag; return $this; } diff --git a/src/HasStringTags.php b/src/HasStringTags.php index 10c51e2..743af9a 100644 --- a/src/HasStringTags.php +++ b/src/HasStringTags.php @@ -20,16 +20,14 @@ * Class HasStringTags * @package DCarbone\PHPConsulAPI */ -trait HasStringTags -{ +trait HasStringTags { /** @var string[] */ public $Tags = []; /** * @return string[] */ - public function getTags() - { + public function getTags() { return $this->Tags; } } \ No newline at end of file diff --git a/src/Health/HealthCheck.php b/src/Health/HealthCheck.php index 34c785e..8688dc5 100644 --- a/src/Health/HealthCheck.php +++ b/src/Health/HealthCheck.php @@ -22,8 +22,7 @@ * Class HealthCheck * @package DCarbone\PHPConsulAPI\Health */ -class HealthCheck extends AbstractModel -{ +class HealthCheck extends AbstractModel { /** @var string */ public $Node = ''; /** @var string */ @@ -44,64 +43,56 @@ class HealthCheck extends AbstractModel /** * @return string */ - public function getNode() - { + public function getNode() { return $this->Node; } /** * @return string */ - public function getCheckID() - { + public function getCheckID() { return $this->CheckID; } /** * @return string */ - public function getName() - { + public function getName() { return $this->Name; } /** * @return string */ - public function getStatus() - { + public function getStatus() { return $this->Status; } /** * @return string */ - public function getNotes() - { + public function getNotes() { return $this->Notes; } /** * @return string */ - public function getOutput() - { + public function getOutput() { return $this->Output; } /** * @return string */ - public function getServiceID() - { + public function getServiceID() { return $this->ServiceID; } /** * @return string */ - public function getServiceName() - { + public function getServiceName() { return $this->ServiceName; } } \ No newline at end of file diff --git a/src/Health/HealthClient.php b/src/Health/HealthClient.php index 67c70db..38bf8aa 100644 --- a/src/Health/HealthClient.php +++ b/src/Health/HealthClient.php @@ -25,26 +25,25 @@ * Class HealthClient * @package DCarbone\PHPConsulAPI\Health */ -class HealthClient extends AbstractClient -{ +class HealthClient extends AbstractClient { /** * @param string $node * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type HealthCheck[]|null list of health checks or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta query meta - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type HealthCheck[]|null list of health checks or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta query meta + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function node($node, QueryOptions $queryOptions = null) - { - if (!is_string($node)) - { - return [null, null, new Error(sprintf( - '%s::node - $node must be string, %s seen.', - get_class($this), - gettype($node) - ))]; + public function node($node, QueryOptions $queryOptions = null) { + if (!is_string($node)) { + return [null, + null, + new Error(sprintf( + '%s::node - $node must be string, %s seen.', + get_class($this), + gettype($node) + ))]; } $r = new Request('get', sprintf('v1/health/node/%s', $node), $this->c); @@ -52,19 +51,20 @@ public function node($node, QueryOptions $queryOptions = null) /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - - if (null !== $err) + + if (null !== $err) { return [null, $qm, $err]; - + } + $checks = []; - foreach($data as $check) - { + foreach ($data as $check) { $checks[] = new HealthCheck($check); } @@ -80,15 +80,15 @@ public function node($node, QueryOptions $queryOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function checks($service, QueryOptions $queryOptions = null) - { - if (!is_string($service)) - { - return [null, null, new Error(sprintf( - '%s::checks - $service must be string, %s seen.', - get_class($this), - gettype($service) - ))]; + public function checks($service, QueryOptions $queryOptions = null) { + if (!is_string($service)) { + return [null, + null, + new Error(sprintf( + '%s::checks - $service must be string, %s seen.', + get_class($this), + gettype($service) + ))]; } /** @var \Psr\Http\Message\ResponseInterface $response */ @@ -96,19 +96,20 @@ public function checks($service, QueryOptions $queryOptions = null) $r->setQueryOptions($queryOptions); list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $checks = []; - foreach($data as $check) - { + foreach ($data as $check) { $checks[] = new HealthCheck($check); } @@ -126,39 +127,42 @@ public function checks($service, QueryOptions $queryOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function service($service, $tag = '', $passingOnly = false, QueryOptions $queryOptions = null) - { - if (!is_string($service)) - { - return [null, null, new Error(sprintf( - '%s::service - $service must be string, %s seen.', - get_class($this), - gettype($service) - ))]; + public function service($service, $tag = '', $passingOnly = false, QueryOptions $queryOptions = null) { + if (!is_string($service)) { + return [null, + null, + new Error(sprintf( + '%s::service - $service must be string, %s seen.', + get_class($this), + gettype($service) + ))]; } $r = new Request('get', sprintf('v1/health/service/%s', $service), $this->c); $r->setQueryOptions($queryOptions); - if ('' !== $tag) + if ('' !== $tag) { $r->params->set('tag', $tag); - if ($passingOnly) + } + if ($passingOnly) { $r->params->set('passing', '1'); + } /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $services = []; - foreach($data as $service) - { + foreach ($data as $service) { $services[] = new ServiceEntry($service); } @@ -169,23 +173,23 @@ public function service($service, $tag = '', $passingOnly = false, QueryOptions * @param string $state * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type HealthCheck[]|null array of heath checks or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type HealthCheck[]|null array of heath checks or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function state($state, QueryOptions $queryOptions = null) - { + public function state($state, QueryOptions $queryOptions = null) { static $validStates = array('any', 'warning', 'critical', 'passing', 'unknown'); - if (!is_string($state) || !in_array($state, $validStates, true)) - { - return [null, null, new Error(sprintf( - '%s::state - "$state" must be string with value of ["%s"]. %s seen.', - get_class($this), - implode('", "', $validStates), - is_string($state) ? $state : gettype($state) - ))]; + if (!is_string($state) || !in_array($state, $validStates, true)) { + return [null, + null, + new Error(sprintf( + '%s::state - "$state" must be string with value of ["%s"]. %s seen.', + get_class($this), + implode('", "', $validStates), + is_string($state) ? $state : gettype($state) + ))]; } $r = new Request('get', sprintf('v1/health/state/%s', $state), $this->c); @@ -193,19 +197,20 @@ public function state($state, QueryOptions $queryOptions = null) /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $checks = []; - foreach($data as $check) - { + foreach ($data as $check) { $checks[] = new HealthCheck($check); } diff --git a/src/Health/ServiceEntry.php b/src/Health/ServiceEntry.php index 77ecc32..2ff7128 100644 --- a/src/Health/ServiceEntry.php +++ b/src/Health/ServiceEntry.php @@ -24,8 +24,7 @@ * Class ServiceEntry * @package DCarbone\PHPConsulAPI\Health */ -class ServiceEntry extends AbstractModel -{ +class ServiceEntry extends AbstractModel { /** @var string */ public $Node = ''; /** @var \DCarbone\PHPConsulAPI\Agent\AgentService */ @@ -37,27 +36,25 @@ class ServiceEntry extends AbstractModel * ServiceEntry constructor. * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); - if (null !== $this->Service && !($this->Service instanceof AgentService)) + if (null !== $this->Service && !($this->Service instanceof AgentService)) { $this->Service = new AgentService((array)$this->Service); + } // If we have data... - if (0 < count($this->Checks)) - { + if (0 < count($this->Checks)) { // ...remove null $this->Checks = array_filter($this->Checks); // ...and if we still have data - if (0 < ($cnt = count($this->Checks))) - { + if (0 < ($cnt = count($this->Checks))) { // ensure we have objects. - for ($i = 0; $i < $cnt; $i++) - { - if (!($this->Checks[$i]) instanceof AgentCheck) + for ($i = 0; $i < $cnt; $i++) { + if (!($this->Checks[$i]) instanceof AgentCheck) { $this->Checks[$i] = new AgentCheck((array)$this->Checks[$i]); + } } } } @@ -66,24 +63,21 @@ public function __construct(array $data = []) /** * @return string */ - public function getNode() - { + public function getNode() { return $this->Node; } /** * @return \DCarbone\PHPConsulAPI\Agent\AgentService */ - public function getService() - { + public function getService() { return $this->Service; } /** * @return \DCarbone\PHPConsulAPI\Health\HealthCheck[] */ - public function getChecks() - { + public function getChecks() { return $this->Checks; } } \ No newline at end of file diff --git a/src/HttpAuth.php b/src/HttpAuth.php index 219d0f7..303727d 100644 --- a/src/HttpAuth.php +++ b/src/HttpAuth.php @@ -20,8 +20,7 @@ * Class HttpAuth * @package DCarbone\PHPConsulAPI */ -class HttpAuth implements \JsonSerializable -{ +class HttpAuth implements \JsonSerializable { /** @var string */ private $_username = ''; /** @var string */ @@ -32,8 +31,7 @@ class HttpAuth implements \JsonSerializable * @param string $username * @param string $password */ - public function __construct($username = '', $password = '') - { + public function __construct($username = '', $password = '') { $this->_username = (string)$username; $this->_password = (string)$password; } @@ -41,56 +39,49 @@ public function __construct($username = '', $password = '') /** * @return array */ - public function __sleep() - { + public function __sleep() { return ['_username']; } /** * @return array */ - public function __debugInfo() - { + public function __debugInfo() { return ['username' => $this->_username]; } /** * @return string */ - public function getUsername() - { + public function getUsername() { return $this->_username; } /** * @return string */ - public function getPassword() - { + public function getPassword() { return $this->_password; } /** * @return string */ - public function compileAuthString() - { + public function compileAuthString() { return (string)$this; } /** * @return array */ - public function jsonSerialize() - { + public function jsonSerialize() { return array('username' => $this->_username, 'password' => $this->_password); } /** * @return string */ - public function __toString() - { + public function __toString() { return trim(sprintf('%s:%s', $this->_username, $this->_password), ":"); } } \ No newline at end of file diff --git a/src/KV/KVClient.php b/src/KV/KVClient.php index 0895d42..6774a04 100644 --- a/src/KV/KVClient.php +++ b/src/KV/KVClient.php @@ -26,26 +26,25 @@ * Class KVClient * @package DCarbone\PHPConsulAPI\KV */ -class KVClient extends AbstractClient -{ +class KVClient extends AbstractClient { /** * @param string $key Name of key to retrieve value for * @param \DCarbone\PHPConsulAPI\QueryOptions $queryOptions * @return array( - * @type KVPair|null kv object or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata object or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type KVPair|null kv object or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata object or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function get($key, QueryOptions $queryOptions = null) - { - if (!is_string($key)) - { - return [null, null, new Error(sprintf( - '%s::get - Key expected to be string, %s seen.', - get_class($this), - gettype($key) - ))]; + public function get($key, QueryOptions $queryOptions = null) { + if (!is_string($key)) { + return [null, + null, + new Error(sprintf( + '%s::get - Key expected to be string, %s seen.', + get_class($this), + gettype($key) + ))]; } $r = new Request('get', sprintf('v1/kv/%s', $key), $this->c); @@ -53,25 +52,27 @@ public function get($key, QueryOptions $queryOptions = null) /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->doRequest($r); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); $code = $response->getStatusCode(); - if (200 === $code) - { + if (200 === $code) { list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } return [new KVPair($data[0], true), $qm, null]; } - if (404 === $code) + if (404 === $code) { return [null, $qm, null]; + } return [null, $qm, new Error(sprintf('%s: %s', $response->getStatusCode(), $response->getReasonPhrase()))]; } @@ -80,45 +81,47 @@ public function get($key, QueryOptions $queryOptions = null) * @param string $prefix * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type KVPair[]|null array of KVPair objects under specified prefix - * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type KVPair[]|null array of KVPair objects under specified prefix + * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function valueList($prefix = '', QueryOptions $queryOptions = null) - { - if (!is_string($prefix)) - { - return [null, null, new Error(sprintf( - '%s::valueList - Prefix expected to be string, "%s" seen.', - get_class($this), - gettype($prefix) - ))]; + public function valueList($prefix = '', QueryOptions $queryOptions = null) { + if (!is_string($prefix)) { + return [null, + null, + new Error(sprintf( + '%s::valueList - Prefix expected to be string, "%s" seen.', + get_class($this), + gettype($prefix) + ))]; } - if ('' === $prefix) + if ('' === $prefix) { $r = new Request('get', 'v1/kv/', $this->c); - else + } else { $r = new Request('get', sprintf('v1/kv/%s', $prefix), $this->c); + } $r->setQueryOptions($queryOptions); $r->params->set('recurse', ''); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $kvPairs = []; - foreach($data as $v) - { + foreach ($data as $v) { $kvp = new KVPair($v, true); $kvPairs[$kvp->Key] = $kvp; } @@ -130,34 +133,36 @@ public function valueList($prefix = '', QueryOptions $queryOptions = null) * @param string $prefix Prefix to search for. Null returns all keys. * @param \DCarbone\PHPConsulAPI\QueryOptions $queryOptions * @return array( - * @type string[]|null list of keys - * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type string[]|null list of keys + * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function keys($prefix = null, QueryOptions $queryOptions = null) - { - if (null !== $prefix && !is_string($prefix)) - { - return [null, null, new Error(sprintf( - '%s::keys - Prefix expected to be empty or string, %s seen.', - get_class($this), - gettype($prefix) - ))]; + public function keys($prefix = null, QueryOptions $queryOptions = null) { + if (null !== $prefix && !is_string($prefix)) { + return [null, + null, + new Error(sprintf( + '%s::keys - Prefix expected to be empty or string, %s seen.', + get_class($this), + gettype($prefix) + ))]; } - if (null === $prefix) + if (null === $prefix) { $r = new Request('get', 'v1/kv/', $this->c); - else + } else { $r = new Request('get', sprintf('v1/kv/%s', $prefix), $this->c); + } $r->setQueryOptions($queryOptions); - $r->params->set('keys', true); + $r->params->set('keys', 'true'); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); @@ -170,20 +175,21 @@ public function keys($prefix = null, QueryOptions $queryOptions = null) * @param KVPair $p * @param \DCarbone\PHPConsulAPI\WriteOptions $writeOptions * @return array( - * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function put(KVPair $p, WriteOptions $writeOptions = null) - { + public function put(KVPair $p, WriteOptions $writeOptions = null) { $r = new Request('put', sprintf('v1/kv/%s', $p->Key), $this->c, $p->Value); $r->setWriteOptions($writeOptions); - if (0 !== $p->Flags) - $r->params->set('flags', $p->Flags); + if (0 !== $p->Flags) { + $r->params->set('flags', (string)$p->Flags); + } list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -192,18 +198,18 @@ public function put(KVPair $p, WriteOptions $writeOptions = null) * @param string $key * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions * @return array( - * @type \DCarbone\PHPConsulAPI\WriteMeta metadata about write - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\WriteMeta metadata about write + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function delete($key, WriteOptions $writeOptions = null) - { + public function delete($key, WriteOptions $writeOptions = null) { $r = new Request('delete', sprintf('v1/kv/%s', $key), $this->c); $r->setWriteOptions($writeOptions); list ($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -212,21 +218,22 @@ public function delete($key, WriteOptions $writeOptions = null) * @param KVPair $p * @param \DCarbone\PHPConsulAPI\WriteOptions $writeOptions * @return array( - * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function cas(KVPair $p, WriteOptions $writeOptions = null) - { + public function cas(KVPair $p, WriteOptions $writeOptions = null) { $r = new Request('put', sprintf('v1/kv/%s', $p->Key), $this->c); $r->setWriteOptions($writeOptions); - $r->params->set('cas', $p->ModifyIndex); - if (0 !== $p->Flags) - $r->params->set('flags', $p->Flags); + $r->params->set('cas', (string)$p->ModifyIndex); + if (0 !== $p->Flags) { + $r->params->set('flags', (string)$p->Flags); + } list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -235,21 +242,22 @@ public function cas(KVPair $p, WriteOptions $writeOptions = null) * @param KVPair $p * @param \DCarbone\PHPConsulAPI\WriteOptions $writeOptions * @return array( - * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function acquire(KVPair $p, WriteOptions $writeOptions = null) - { + public function acquire(KVPair $p, WriteOptions $writeOptions = null) { $r = new Request('put', sprintf('v1/kv/%s', $p->Key), $this->c); $r->setWriteOptions($writeOptions); $r->params->set('acquire', $p->Session); - if (0 !== $p->Flags) - $r->params->set('flags', $p->Flags); + if (0 !== $p->Flags) { + $r->params->set('flags', (string)$p->Flags); + } list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -258,21 +266,22 @@ public function acquire(KVPair $p, WriteOptions $writeOptions = null) * @param KVPair $p * @param \DCarbone\PHPConsulAPI\WriteOptions $writeOptions * @return array( - * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function release(KVPair $p, WriteOptions $writeOptions = null) - { + public function release(KVPair $p, WriteOptions $writeOptions = null) { $r = new Request('put', sprintf('v1/kv/%s', $p->Key), $this->c); $r->setWriteOptions($writeOptions); $r->params->set('release', $p->Session); - if (0 !== $p->Flags) - $r->params->set('flags', $p->Flags); + if (0 !== $p->Flags) { + $r->params->set('flags', (string)$p->Flags); + } list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -281,65 +290,65 @@ public function release(KVPair $p, WriteOptions $writeOptions = null) * @param null|string $prefix * @param \DCarbone\PHPConsulAPI\QueryOptions $queryOptions * @return array( - * @type KVPair[]|KVTree[]|null array of trees, values, or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type KVPair[]|KVTree[]|null array of trees, values, or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function tree($prefix = '', QueryOptions $queryOptions = null) - { + public function tree($prefix = '', QueryOptions $queryOptions = null) { list($valueList, $_, $err) = $this->valueList($prefix, $queryOptions); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } $treeHierarchy = []; - foreach($valueList as $path=>$kvp) - { + foreach ($valueList as $path => $kvp) { $slashPos = strpos($path, '/'); - if (false === $slashPos) - { + if (false === $slashPos) { $treeHierarchy[$path] = $kvp; continue; } $root = substr($path, 0, $slashPos + 1); - if (!isset($treeHierarchy[$root])) + if (!isset($treeHierarchy[$root])) { $treeHierarchy[$root] = new KVTree($root); + } - if ('/' === substr($path, -1)) - { + if ('/' === substr($path, -1)) { $_path = ''; - foreach(explode('/', $prefix) as $part) - { - if ('' === $part) + foreach (explode('/', $prefix) as $part) { + if ('' === $part) { continue; + } $_path .= "{$part}/"; - if ($root === $_path) + if ($root === $_path) { continue; + } - if (!isset($treeHierarchy[$root][$_path])) + if (!isset($treeHierarchy[$root][$_path])) { $treeHierarchy[$root][$_path] = new KVTree($_path); + } } - } - else - { + } else { $kvPrefix = substr($path, 0, strrpos($path, '/') + 1); $_path = ''; - foreach(explode('/', $kvPrefix) as $part) - { - if ('' === $part) + foreach (explode('/', $kvPrefix) as $part) { + if ('' === $part) { continue; + } $_path .= "{$part}/"; - if ($root === $_path) + if ($root === $_path) { continue; + } - if (!isset($treeHierarchy[$root][$_path])) + if (!isset($treeHierarchy[$root][$_path])) { $treeHierarchy[$root][$_path] = new KVTree($_path); + } } $treeHierarchy[$root][$path] = $kvp; diff --git a/src/KV/KVPair.php b/src/KV/KVPair.php index 46cb62a..af2e912 100644 --- a/src/KV/KVPair.php +++ b/src/KV/KVPair.php @@ -22,8 +22,7 @@ * Class KVPair * @package DCarbone\PHPConsulAPI\KV */ -class KVPair extends AbstractModel -{ +class KVPair extends AbstractModel { /** @var string */ public $Key = ''; /** @var int */ @@ -44,18 +43,17 @@ class KVPair extends AbstractModel * @param array $data * @param bool $_decodeValue */ - public function __construct(array $data = [], $_decodeValue = false) - { + public function __construct(array $data = [], $_decodeValue = false) { parent::__construct($data); - if ((bool)$_decodeValue && isset($this->Value)) + if ((bool)$_decodeValue && isset($this->Value)) { $this->Value = base64_decode($this->Value); + } } /** * @return string */ - public function getKey() - { + public function getKey() { return $this->Key; } @@ -63,8 +61,7 @@ public function getKey() * @param string $Key * @return \DCarbone\PHPConsulAPI\KV\KVPair */ - public function setKey($Key) - { + public function setKey($Key) { $this->Key = $Key; return $this; } @@ -72,8 +69,7 @@ public function setKey($Key) /** * @return int */ - public function getCreateIndex() - { + public function getCreateIndex() { return $this->CreateIndex; } @@ -81,8 +77,7 @@ public function getCreateIndex() * @param int $CreateIndex * @return \DCarbone\PHPConsulAPI\KV\KVPair */ - public function setCreateIndex($CreateIndex) - { + public function setCreateIndex($CreateIndex) { $this->CreateIndex = $CreateIndex; return $this; } @@ -90,8 +85,7 @@ public function setCreateIndex($CreateIndex) /** * @return int */ - public function getModifyIndex() - { + public function getModifyIndex() { return $this->ModifyIndex; } @@ -99,8 +93,7 @@ public function getModifyIndex() * @param int $ModifyIndex * @return \DCarbone\PHPConsulAPI\KV\KVPair */ - public function setModifyIndex($ModifyIndex) - { + public function setModifyIndex($ModifyIndex) { $this->ModifyIndex = $ModifyIndex; return $this; } @@ -108,8 +101,7 @@ public function setModifyIndex($ModifyIndex) /** * @return int */ - public function getLockIndex() - { + public function getLockIndex() { return $this->LockIndex; } @@ -117,8 +109,7 @@ public function getLockIndex() * @param int $LockIndex * @return \DCarbone\PHPConsulAPI\KV\KVPair */ - public function setLockIndex($LockIndex) - { + public function setLockIndex($LockIndex) { $this->LockIndex = $LockIndex; return $this; } @@ -126,8 +117,7 @@ public function setLockIndex($LockIndex) /** * @return int */ - public function getFlags() - { + public function getFlags() { return $this->Flags; } @@ -135,8 +125,7 @@ public function getFlags() * @param int $Flags * @return \DCarbone\PHPConsulAPI\KV\KVPair */ - public function setFlags($Flags) - { + public function setFlags($Flags) { $this->Flags = $Flags; return $this; } @@ -144,8 +133,7 @@ public function setFlags($Flags) /** * @return null|string */ - public function getValue() - { + public function getValue() { return $this->Value; } @@ -153,8 +141,7 @@ public function getValue() * @param null|string $Value * @return \DCarbone\PHPConsulAPI\KV\KVPair */ - public function setValue($Value) - { + public function setValue($Value) { $this->Value = $Value; return $this; } @@ -162,8 +149,7 @@ public function setValue($Value) /** * @return string */ - public function getSession() - { + public function getSession() { return $this->Session; } @@ -171,8 +157,7 @@ public function getSession() * @param string $Session * @return \DCarbone\PHPConsulAPI\KV\KVPair */ - public function setSession($Session) - { + public function setSession($Session) { $this->Session = $Session; return $this; } @@ -180,8 +165,7 @@ public function setSession($Session) /** * @return string */ - public function __toString() - { + public function __toString() { return (string)$this->Value; } } \ No newline at end of file diff --git a/src/KV/KVTree.php b/src/KV/KVTree.php index c3fb3bb..8e4f8df 100644 --- a/src/KV/KVTree.php +++ b/src/KV/KVTree.php @@ -20,8 +20,7 @@ * Class KVTree * @package DCarbone\PHPConsulAPI\KV */ -class KVTree implements \RecursiveIterator, \Countable, \JsonSerializable, \ArrayAccess, \Serializable -{ +class KVTree implements \RecursiveIterator, \Countable, \JsonSerializable, \ArrayAccess, \Serializable { /** @var string */ private $_prefix; @@ -32,40 +31,35 @@ class KVTree implements \RecursiveIterator, \Countable, \JsonSerializable, \Arra * KVTree constructor. * @param string $prefix */ - public function __construct($prefix) - { + public function __construct($prefix) { $this->_prefix = $prefix; } /** * @return string */ - public function getPrefix() - { + public function getPrefix() { return $this->_prefix; } /** * @return \DCarbone\PHPConsulAPI\KV\KVTree|\DCarbone\PHPConsulAPI\KV\KVPair Can return any type. */ - public function current() - { + public function current() { return current($this->_children); } /** * @return void Any returned value is ignored. */ - public function next() - { + public function next() { next($this->_children); } /** * @return string scalar on success, or null on failure. */ - public function key() - { + public function key() { return key($this->_children); } @@ -73,40 +67,35 @@ public function key() * @return boolean The return value will be casted to boolean and then evaluated. * Returns true on success or false on failure. */ - public function valid() - { + public function valid() { return null !== key($this->_children); } /** * @return void Any returned value is ignored. */ - public function rewind() - { + public function rewind() { reset($this->_children); } /** * @return bool true if the current entry can be iterated over, otherwise returns false. */ - public function hasChildren() - { + public function hasChildren() { return $this->current() instanceof KVTree; } /** * @return \RecursiveIterator An iterator for the current entry. */ - public function getChildren() - { + public function getChildren() { return $this->current(); } /** * @return int The custom count as an integer. */ - public function count() - { + public function count() { return count($this->_children); } @@ -114,18 +103,16 @@ public function count() * @param mixed $offset An offset to check for. * @return boolean true on success or false on failure. */ - public function offsetExists($offset) - { - if (is_string($offset)) - { + public function offsetExists($offset) { + if (is_string($offset)) { $subPath = str_replace($this->_prefix, '', $offset); $cnt = substr_count($subPath, '/'); - if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1))) - { - $childKey = $this->_prefix.substr($subPath, 0, strpos($subPath, '/') + 1); - if (isset($this->_children[$childKey])) + if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1))) { + $childKey = $this->_prefix . substr($subPath, 0, strpos($subPath, '/') + 1); + if (isset($this->_children[$childKey])) { return isset($this->_children[$childKey][$offset]); + } } } @@ -136,22 +123,21 @@ public function offsetExists($offset) * @param mixed $offset The offset to retrieve. * @return \DCarbone\PHPConsulAPI\KV\KVTree|\DCarbone\PHPConsulAPI\KV\KVPair */ - public function offsetGet($offset) - { - if (is_string($offset)) - { + public function offsetGet($offset) { + if (is_string($offset)) { $subPath = str_replace($this->_prefix, '', $offset); $cnt = substr_count($subPath, '/'); - if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1))) - { - $childKey = $this->_prefix.substr($subPath, 0, strpos($subPath, '/') + 1); - if (isset($this[$childKey])) + if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1))) { + $childKey = $this->_prefix . substr($subPath, 0, strpos($subPath, '/') + 1); + if (isset($this[$childKey])) { return $this->_children[$childKey][$offset]; + } } } - if (isset($this[$offset])) + if (isset($this[$offset])) { return $this->_children[$offset]; + } trigger_error(sprintf( '%s - Requested offset %s does not exist in tree with prefix "%s".', @@ -168,29 +154,20 @@ public function offsetGet($offset) * @param mixed $value The value to set. * @return void */ - public function offsetSet($offset, $value) - { - if ('string' === gettype($offset)) - { + public function offsetSet($offset, $value) { + if ('string' === gettype($offset)) { $subPath = str_replace($this->_prefix, '', $offset); $cnt = substr_count($subPath, '/'); - if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1))) - { - $childKey = $this->_prefix.substr($subPath, 0, strpos($subPath, '/') + 1); + if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1))) { + $childKey = $this->_prefix . substr($subPath, 0, strpos($subPath, '/') + 1); $this->_children[$childKey][$offset] = $value; - } - else - { + } else { $this->_children[$offset] = $value; } - } - else if (null === $offset) - { + } else if (null === $offset) { $this->_children[] = $value; - } - else - { + } else { $this->_children[$offset] = $value; } } @@ -199,16 +176,14 @@ public function offsetSet($offset, $value) * @param mixed $offset The offset to unset. * @return void */ - public function offsetUnset($offset) - { + public function offsetUnset($offset) { // do nothing, yo... } /** * @return string the string representation of the object or null */ - public function serialize() - { + public function serialize() { return serialize([$this->_prefix, $this->_children]); } @@ -216,8 +191,7 @@ public function serialize() * @param string $serialized The string representation of the object. * @return void */ - public function unserialize($serialized) - { + public function unserialize($serialized) { $data = unserialize($serialized); $this->_prefix = $data[0]; $this->_children = $data[1]; @@ -226,15 +200,14 @@ public function unserialize($serialized) /** * @return array data which can be serialized by json_encode,which is a value of any type other than a resource. */ - public function jsonSerialize() - { + public function jsonSerialize() { $json = array($this->_prefix => []); - foreach($this->_children as $k=>$child) - { - if ($child instanceof KVTree) + foreach ($this->_children as $k => $child) { + if ($child instanceof KVTree) { $json[$this->_prefix] = $child; - else if ($child instanceof KVPair) + } else if ($child instanceof KVPair) { $json[$this->_prefix][$child->Key] = $child; + } } return $json; } @@ -242,16 +215,14 @@ public function jsonSerialize() /** * @return string */ - public function __toString() - { + public function __toString() { return $this->_prefix; } /** * @return array */ - public function __debugInfo() - { + public function __debugInfo() { return array( 'prefix' => $this->_prefix, 'children' => $this->_children diff --git a/src/Logger.php b/src/Logger.php deleted file mode 100644 index dc6a069..0000000 --- a/src/Logger.php +++ /dev/null @@ -1,281 +0,0 @@ - 0, - LogLevel::INFO => 1, - LogLevel::NOTICE => 2, - LogLevel::WARNING => 3, - LogLevel::ERROR => 4, - LogLevel::CRITICAL => 5, - LogLevel::ALERT => 6, - LogLevel::EMERGENCY => 7, - ); - - /** - * Set up a file logger that outputs log data to "../var/logs/php-consul-api.log" - */ - public static function addDebugLogger() - { - if (!isset(self::$_debugLogger)) - self::$_debugLogger = new FileDebugLogger(__DIR__ . '/../var/logs/php-consul-api.log'); - } - - /** - * Destroy the default logger - */ - public static function removeDebugLogger() - { - self::$_debugLogger = null; - } - - /** - * @param string $logLevel - */ - public static function setLogLevel($logLevel) - { - self::$_logLevel = self::sanitizeLevel($logLevel); - } - - /** - * @param LoggerInterface[] $loggers - */ - public static function setLoggers(array $loggers) - { - self::$_loggers = []; - - foreach($loggers as $i => $logger) - { - self::addLogger($logger); - } - } - - /** - * @param LoggerInterface $logger - */ - public static function addLogger($logger) - { - if ($logger instanceof LoggerInterface) - { - self::$_loggers[] = $logger; - } - else - { - throw new \InvalidArgumentException(sprintf( - '%s - %s is not a valid logger implementation', - get_called_class(), - is_object($logger) ? get_class($logger) : gettype($logger) - )); - } - } - - /** - * Clear out all loggers - */ - public static function resetLoggers() - { - self::$_loggers = []; - } - - /** - * @param string $logLevel - * @param string $message - * @param array $context - */ - public static function log($logLevel, $message, array $context = []) - { - $logLevel = self::sanitizeLevel($logLevel); - - if (self::$_logLevels[self::$_logLevel] <= self::$_logLevels[$logLevel]) - { - if ($message instanceof Error) - $message = $message->getMessage(); - - // Log to default logger, if set - if (isset(self::$_debugLogger)) - self::$_debugLogger->{$logLevel}($message, $context); - - // Log to each user-defined logger - foreach(self::$_loggers as $logger) - { - $logger->{$logLevel}($message, $context); - } - } - } - - /** - * System is unusable. - * - * @param string $message - * @param array $context - */ - public static function emergency($message, array $context = []) - { - static::log(LogLevel::EMERGENCY, $message, $context); - } - - /** - * Action must be taken immediately. - * - * Example: Entire website down, database unavailable, etc. This should - * trigger the SMS alerts and wake you up. - * - * @param string $message - * @param array $context - */ - public static function alert($message, array $context = []) - { - static::log(LogLevel::ALERT, $message, $context); - } - - /** - * Critical conditions. - * - * Example: Application component unavailable, unexpected exception. - * - * @param string $message - * @param array $context - */ - public static function critical($message, array $context = []) - { - static::log(LogLevel::CRITICAL, $message, $context); - } - - /** - * Runtime errors that do not require immediate action but should typically - * be logged and monitored. - * - * @param string $message - * @param array $context - */ - public static function error($message, array $context = []) - { - static::log(LogLevel::ERROR, $message, $context); - } - - /** - * Exceptional occurrences that are not errors. - * - * Example: Use of deprecated APIs, poor use of an API, undesirable things - * that are not necessarily wrong. - * - * @param string $message - * @param array $context - */ - public static function warning($message, array $context = []) - { - static::log(LogLevel::WARNING, $message, $context); - } - - /** - * Normal but significant events. - * - * @param string $message - * @param array $context - */ - public static function notice($message, array $context = []) - { - static::log(LogLevel::NOTICE, $message, $context); - } - - /** - * Interesting events. - * - * Example: User logs in, SQL logs. - * - * @param string $message - * @param array $context - */ - public static function info($message, array $context = []) - { - static::log(LogLevel::INFO, $message, $context); - } - - /** - * Detailed debug information. - * - * @param string $message - * @param array $context - */ - public static function debug($message, array $context = []) - { - static::log(LogLevel::DEBUG, $message, $context); - } - - /** - * @param string $message - * @param array $context - */ - public static function warn($message, array $context = []) - { - self::log(LogLevel::WARNING, $message, $context); - } - - /** - * @param string $logLevel - * @return string - */ - private static function sanitizeLevel($logLevel) - { - if (!is_string($logLevel)) - { - throw new \InvalidArgumentException(sprintf( - '%s - Log level must be string, %s seen.', - get_called_class(), - gettype($logLevel) - )); - } - - $level = strtolower(trim($logLevel)); - - // Some backwards compatibility - if ('warn' === $level) - $level = LogLevel::WARNING; - - if (!isset(self::$_logLevels[$level])) - { - throw new \InvalidArgumentException(sprintf( - '%s - Log level must be one of the following values: ["%s"]. %s seen.', - get_called_class(), - implode('", "', array_keys(self::$_logLevels)), - is_string($logLevel) ? $logLevel : gettype($logLevel) - )); - } - - return $level; - } -} \ No newline at end of file diff --git a/src/Operator/OperatorClient.php b/src/Operator/OperatorClient.php index 0f333fe..8939c3c 100644 --- a/src/Operator/OperatorClient.php +++ b/src/Operator/OperatorClient.php @@ -25,8 +25,7 @@ * Class OperatorClient * @package DCarbone\PHPConsulAPI\Operator */ -class OperatorClient extends AbstractClient -{ +class OperatorClient extends AbstractClient { /** * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( @@ -35,22 +34,23 @@ class OperatorClient extends AbstractClient * @type \DCarbone\PHPConsulAPI\Error error, if any * ) */ - public function raftGetConfiguration(QueryOptions $queryOptions = null) - { + public function raftGetConfiguration(QueryOptions $queryOptions = null) { $r = new Request('get', 'v1/operator/raft/configuration', $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } return [new RaftConfiguration($data), $qm, null]; } @@ -60,8 +60,7 @@ public function raftGetConfiguration(QueryOptions $queryOptions = null) * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions * @return \DCarbone\PHPConsulAPI\Error|null error, if any */ - public function raftRemovePeerByAddress($address, WriteOptions $writeOptions = null) - { + public function raftRemovePeerByAddress($address, WriteOptions $writeOptions = null) { $r = new Request('delete', 'v1/operator/raft/peer', $this->c); $r->setWriteOptions($writeOptions); $r->params->set('address', (string)$address); diff --git a/src/Operator/RaftConfiguration.php b/src/Operator/RaftConfiguration.php index 0898318..fae2b25 100644 --- a/src/Operator/RaftConfiguration.php +++ b/src/Operator/RaftConfiguration.php @@ -22,8 +22,7 @@ * Class RaftConfiguration * @package DCarbone\PHPConsulAPI\Operator */ -class RaftConfiguration extends AbstractModel -{ +class RaftConfiguration extends AbstractModel { /** @var \DCarbone\PHPConsulAPI\Operator\RaftServer[] */ public $Servers = []; /** @var int */ @@ -34,20 +33,17 @@ class RaftConfiguration extends AbstractModel * * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); - if (0 < count($this->Servers)) - { + if (0 < count($this->Servers)) { $this->Servers = array_filter($this->Servers); - if (0 < ($cnt = count($this->Servers))) - { - for ($i = 0; $i < $cnt; $i++) - { - if (!($this->Servers[$i] instanceof RaftServer)) + if (0 < ($cnt = count($this->Servers))) { + for ($i = 0; $i < $cnt; $i++) { + if (!($this->Servers[$i] instanceof RaftServer)) { $this->Servers[$i] = new RaftServer((array)$this->Servers[$i]); + } } } } @@ -56,8 +52,7 @@ public function __construct(array $data = []) /** * @return \DCarbone\PHPConsulAPI\Operator\RaftServer[] */ - public function getServers() - { + public function getServers() { return $this->Servers; } @@ -65,11 +60,9 @@ public function getServers() * @param \DCarbone\PHPConsulAPI\Operator\RaftServer[] $Servers * @return \DCarbone\PHPConsulAPI\Operator\RaftConfiguration */ - public function setServers(array $Servers) - { + public function setServers(array $Servers) { $this->Servers = []; - foreach($Servers as $Server) - { + foreach ($Servers as $Server) { $this->addServer($Server); } return $this; @@ -79,8 +72,7 @@ public function setServers(array $Servers) * @param \DCarbone\PHPConsulAPI\Operator\RaftServer $Server * @return \DCarbone\PHPConsulAPI\Operator\RaftConfiguration */ - public function addServer(RaftServer $Server) - { + public function addServer(RaftServer $Server) { $this->Servers[] = $Server; return $this; } @@ -88,8 +80,7 @@ public function addServer(RaftServer $Server) /** * @return int */ - public function getIndex() - { + public function getIndex() { return $this->Index; } @@ -97,8 +88,7 @@ public function getIndex() * @param int $Index * @return \DCarbone\PHPConsulAPI\Operator\RaftConfiguration */ - public function setIndex($Index) - { + public function setIndex($Index) { $this->Index = $Index; return $this; } diff --git a/src/Operator/RaftServer.php b/src/Operator/RaftServer.php index bf21451..e71fe5c 100644 --- a/src/Operator/RaftServer.php +++ b/src/Operator/RaftServer.php @@ -22,8 +22,7 @@ * Class RaftServer * @package DCarbone\PHPConsulAPI\Operator */ -class RaftServer extends AbstractModel -{ +class RaftServer extends AbstractModel { /** @var string */ public $ID = ''; /** @var string */ @@ -38,8 +37,7 @@ class RaftServer extends AbstractModel /** * @return string */ - public function getID() - { + public function getID() { return $this->ID; } @@ -47,8 +45,7 @@ public function getID() * @param string $ID * @return RaftServer */ - public function setID($ID) - { + public function setID($ID) { $this->ID = $ID; return $this; } @@ -56,8 +53,7 @@ public function setID($ID) /** * @return string */ - public function getNode() - { + public function getNode() { return $this->Node; } @@ -65,8 +61,7 @@ public function getNode() * @param string $Node * @return RaftServer */ - public function setNode($Node) - { + public function setNode($Node) { $this->Node = $Node; return $this; } @@ -74,8 +69,7 @@ public function setNode($Node) /** * @return string */ - public function getAddress() - { + public function getAddress() { return $this->Address; } @@ -83,8 +77,7 @@ public function getAddress() * @param string $Address * @return RaftServer */ - public function setAddress($Address) - { + public function setAddress($Address) { $this->Address = $Address; return $this; } @@ -92,8 +85,7 @@ public function setAddress($Address) /** * @return boolean */ - public function isLeader() - { + public function isLeader() { return $this->Leader; } @@ -101,8 +93,7 @@ public function isLeader() * @param boolean $Leader * @return RaftServer */ - public function setLeader($Leader) - { + public function setLeader($Leader) { $this->Leader = $Leader; return $this; } @@ -110,8 +101,7 @@ public function setLeader($Leader) /** * @return boolean */ - public function isVoter() - { + public function isVoter() { return $this->Voter; } @@ -119,8 +109,7 @@ public function isVoter() * @param boolean $Voter * @return RaftServer */ - public function setVoter($Voter) - { + public function setVoter($Voter) { $this->Voter = $Voter; return $this; } diff --git a/src/Params.php b/src/Params.php index 089c887..b8994d4 100644 --- a/src/Params.php +++ b/src/Params.php @@ -20,61 +20,12 @@ * Class Params * @package DCarbone\PHPConsulAPI */ -class Params extends AbstractCollection -{ - use ConsulHttpParamContainerTrait; - - /** - * @param mixed $param - * @param mixed $value - * @return $this - */ - public function set($param, $value) - { - $this[$param] = $value; - return $this; - } - +class Params extends Values { /** - * Offset to set - * @link http://php.net/manual/en/arrayaccess.offsetset.php - * @param mixed $offset The offset to assign the value to. - * @param mixed $value The value to set. - * @return void + * @param string $v + * @return string */ - public function offsetSet($offset, $value) - { - if (is_string($offset)) - { - switch(strtolower($offset)) - { - case 'datacenter': - case 'dc': - $offset = 'Datacenter'; - break; - - case 'allowstale': - case 'stale': - $offset = 'AllowStale'; - break; - - case 'requireconsistent': - case 'consistent': - $offset = 'RequireConsistent'; - break; - - case 'waitindex': - case 'index': - $offset = 'WaitIndex'; - break; - - case 'waittime': - case 'wait': - $offset = 'WaitTime'; - break; - } - } - - parent::offsetSet($offset, $value); + protected function encode($v) { + return urlencode($v); } } \ No newline at end of file diff --git a/src/PreparedQuery/PreparedQueryClient.php b/src/PreparedQuery/PreparedQueryClient.php index c4ae473..bb20c07 100644 --- a/src/PreparedQuery/PreparedQueryClient.php +++ b/src/PreparedQuery/PreparedQueryClient.php @@ -25,8 +25,7 @@ * Class PreparedQueryClient * @package DCarbone\PHPConsulAPI\PreparedQuery */ -class PreparedQueryClient extends AbstractClient -{ +class PreparedQueryClient extends AbstractClient { /** * @param PreparedQueryDefinition $query * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions @@ -36,15 +35,15 @@ class PreparedQueryClient extends AbstractClient * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function create(PreparedQueryDefinition $query, WriteOptions $writeOptions = null) - { + public function create(PreparedQueryDefinition $query, WriteOptions $writeOptions = null) { $r = new Request('GET', 'v1/query', $this->c, $query); $r->setWriteOptions($writeOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return ['', null, $err]; + } return [(string)$response->getBody(), $this->buildWriteMeta($duration), null]; } @@ -57,15 +56,15 @@ public function create(PreparedQueryDefinition $query, WriteOptions $writeOption * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function update(PreparedQueryDefinition $query, WriteOptions $writeOptions = null) - { + public function update(PreparedQueryDefinition $query, WriteOptions $writeOptions = null) { $r = new Request('PUT', 'v1/query', $this->c, $query); $r->setWriteOptions($writeOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -78,25 +77,25 @@ public function update(PreparedQueryDefinition $query, WriteOptions $writeOption * @type \DCarbone\PHPConsulAPI\Error|null * ) */ - public function listQueries(QueryOptions $queryOptions = null) - { + public function listQueries(QueryOptions $queryOptions = null) { $r = new Request('GET', 'v1/query', $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($body, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $list = []; - foreach($body as $d) - { + foreach ($body as $d) { $list[] = new PreparedQueryDefinition($d); } return [$list, $qm, null]; @@ -111,25 +110,25 @@ public function listQueries(QueryOptions $queryOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null * ) */ - public function get($queryID, QueryOptions $queryOptions = null) - { + public function get($queryID, QueryOptions $queryOptions = null) { $r = new Request('GET', sprintf('v1/query/%s', $queryID), $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($body, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } $queryDefinitions = []; - foreach($body as $d) - { + foreach ($body as $d) { $queryDefinitions[] = new PreparedQueryDefinition($d); } return [$queryDefinitions, $qm, null]; @@ -143,15 +142,15 @@ public function get($queryID, QueryOptions $queryOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function delete($queryID, WriteOptions $writeOptions = null) - { + public function delete($queryID, WriteOptions $writeOptions = null) { $r = new Request('DELETE', sprintf('v1/query/%s', $queryID), $this->c); $r->setWriteOptions($writeOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } list($_, $err) = $this->decodeBody($response->getBody()); @@ -167,21 +166,22 @@ public function delete($queryID, WriteOptions $writeOptions = null) * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function execute($queryIDOrName, QueryOptions $queryOptions = null) - { + public function execute($queryIDOrName, QueryOptions $queryOptions = null) { $r = new Request('GET', sprintf('v1/query/%s', $queryIDOrName), $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($body, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } return [new PreparedQueryExecuteResponse($body), $qm, null]; } diff --git a/src/PreparedQuery/PreparedQueryDefinition.php b/src/PreparedQuery/PreparedQueryDefinition.php index d754bf7..921b70b 100644 --- a/src/PreparedQuery/PreparedQueryDefinition.php +++ b/src/PreparedQuery/PreparedQueryDefinition.php @@ -22,8 +22,7 @@ * Class PreparedQueryDefinition * @package DCarbone\PHPConsulAPI\PreparedQuery */ -class PreparedQueryDefinition extends AbstractModel -{ +class PreparedQueryDefinition extends AbstractModel { /** @var string */ public $ID = ''; /** @var string */ @@ -43,22 +42,23 @@ class PreparedQueryDefinition extends AbstractModel * PreparedQueryDefinition constructor. * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); - if (null !== $this->Service && !($this->Service instanceof ServiceQuery)) + if (null !== $this->Service && !($this->Service instanceof ServiceQuery)) { $this->Service = new ServiceQuery((array)$this->Service); - if (null !== $this->DNS && !($this->DNS instanceof QueryDNSOptions)) + } + if (null !== $this->DNS && !($this->DNS instanceof QueryDNSOptions)) { $this->DNS = new QueryDNSOptions((array)$this->DNS); - if (null !== $this->Template && !($this->Template instanceof QueryTemplate)) + } + if (null !== $this->Template && !($this->Template instanceof QueryTemplate)) { $this->Template = new QueryTemplate((array)$this->Template); + } } /** * @return string */ - public function getID() - { + public function getID() { return $this->ID; } @@ -66,8 +66,7 @@ public function getID() * @param string $ID * @return \DCarbone\PHPConsulAPI\PreparedQuery\PreparedQueryDefinition */ - public function setID($ID) - { + public function setID($ID) { $this->ID = $ID; return $this; } @@ -75,8 +74,7 @@ public function setID($ID) /** * @return string */ - public function getName() - { + public function getName() { return $this->Name; } @@ -84,8 +82,7 @@ public function getName() * @param string $Name * @return \DCarbone\PHPConsulAPI\PreparedQuery\PreparedQueryDefinition */ - public function setName($Name) - { + public function setName($Name) { $this->Name = $Name; return $this; } @@ -93,8 +90,7 @@ public function setName($Name) /** * @return string */ - public function getSession() - { + public function getSession() { return $this->Session; } @@ -102,8 +98,7 @@ public function getSession() * @param string $Session * @return \DCarbone\PHPConsulAPI\PreparedQuery\PreparedQueryDefinition */ - public function setSession($Session) - { + public function setSession($Session) { $this->Session = $Session; return $this; } @@ -111,8 +106,7 @@ public function setSession($Session) /** * @return string */ - public function getToken() - { + public function getToken() { return $this->Token; } @@ -120,8 +114,7 @@ public function getToken() * @param string $Token * @return \DCarbone\PHPConsulAPI\PreparedQuery\PreparedQueryDefinition */ - public function setToken($Token) - { + public function setToken($Token) { $this->Token = $Token; return $this; } @@ -129,8 +122,7 @@ public function setToken($Token) /** * @return \DCarbone\PHPConsulAPI\PreparedQuery\ServiceQuery */ - public function getService() - { + public function getService() { return $this->Service; } @@ -138,8 +130,7 @@ public function getService() * @param \DCarbone\PHPConsulAPI\PreparedQuery\ServiceQuery $Service * @return \DCarbone\PHPConsulAPI\PreparedQuery\PreparedQueryDefinition */ - public function setService(ServiceQuery $Service) - { + public function setService(ServiceQuery $Service) { $this->Service = $Service; return $this; } @@ -147,8 +138,7 @@ public function setService(ServiceQuery $Service) /** * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryDNSOptions */ - public function getDNS() - { + public function getDNS() { return $this->DNS; } @@ -156,8 +146,7 @@ public function getDNS() * @param \DCarbone\PHPConsulAPI\PreparedQuery\QueryDNSOptions $DNS * @return \DCarbone\PHPConsulAPI\PreparedQuery\PreparedQueryDefinition */ - public function setDNS(QueryDNSOptions $DNS) - { + public function setDNS(QueryDNSOptions $DNS) { $this->DNS = $DNS; return $this; } @@ -165,8 +154,7 @@ public function setDNS(QueryDNSOptions $DNS) /** * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryTemplate */ - public function getTemplate() - { + public function getTemplate() { return $this->Template; } @@ -174,8 +162,7 @@ public function getTemplate() * @param \DCarbone\PHPConsulAPI\PreparedQuery\QueryTemplate $Template * @return \DCarbone\PHPConsulAPI\PreparedQuery\PreparedQueryDefinition */ - public function setTemplate(QueryTemplate $Template) - { + public function setTemplate(QueryTemplate $Template) { $this->Template = $Template; return $this; } diff --git a/src/PreparedQuery/PreparedQueryExecuteResponse.php b/src/PreparedQuery/PreparedQueryExecuteResponse.php index 75879d2..c969604 100644 --- a/src/PreparedQuery/PreparedQueryExecuteResponse.php +++ b/src/PreparedQuery/PreparedQueryExecuteResponse.php @@ -23,8 +23,7 @@ * Class PreparedQueryExecuteResponse * @package DCarbone\PHPConsulAPI\PreparedQuery */ -class PreparedQueryExecuteResponse extends AbstractModel -{ +class PreparedQueryExecuteResponse extends AbstractModel { /** @var string */ public $Service = ''; /** @var \DCarbone\PHPConsulAPI\Health\ServiceEntry[] */ @@ -40,22 +39,20 @@ class PreparedQueryExecuteResponse extends AbstractModel * PreparedQueryExecuteResponse constructor. * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); - if (null !== $this->DNS && !($this->DNS instanceof QueryDNSOptions)) + if (null !== $this->DNS && !($this->DNS instanceof QueryDNSOptions)) { $this->DNS = new QueryDNSOptions((array)$this->DNS); + } - if (0 < count($this->Nodes)) - { + if (0 < count($this->Nodes)) { $this->Nodes = array_filter($this->Nodes); - if (0 < ($cnt = count($this->Nodes))) - { - for ($i = 0; $i < $cnt; $i++) - { - if (!($this->Nodes[$i] instanceof ServiceEntry)) + if (0 < ($cnt = count($this->Nodes))) { + for ($i = 0; $i < $cnt; $i++) { + if (!($this->Nodes[$i] instanceof ServiceEntry)) { $this->Nodes[$i] = new ServiceEntry((array)$this->Nodes[$i]); + } } } @@ -65,40 +62,35 @@ public function __construct(array $data = []) /** * @return string */ - public function getService() - { + public function getService() { return $this->Service; } /** * @return \DCarbone\PHPConsulAPI\Health\ServiceEntry[] */ - public function getNodes() - { + public function getNodes() { return $this->Nodes; } /** * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryDNSOptions */ - public function getDNS() - { + public function getDNS() { return $this->DNS; } /** * @return string */ - public function getDatacenter() - { + public function getDatacenter() { return $this->Datacenter; } /** * @return int */ - public function getFailovers() - { + public function getFailovers() { return $this->Failovers; } } \ No newline at end of file diff --git a/src/PreparedQuery/QueryDNSOptions.php b/src/PreparedQuery/QueryDNSOptions.php index 521a1e4..394e45c 100644 --- a/src/PreparedQuery/QueryDNSOptions.php +++ b/src/PreparedQuery/QueryDNSOptions.php @@ -22,16 +22,14 @@ * Class QueryDNSOptions * @package DCarbone\PHPConsulAPI\PreparedQuery */ -class QueryDNSOptions extends AbstractModel -{ +class QueryDNSOptions extends AbstractModel { /** @var string */ public $TTL = ''; /** * @return string */ - public function getTTL() - { + public function getTTL() { return (string)$this->TTL; } @@ -39,8 +37,7 @@ public function getTTL() * @param string $TTL * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryDNSOptions */ - public function setTTL($TTL) - { + public function setTTL($TTL) { $this->TTL = $TTL; return $this; } @@ -48,8 +45,7 @@ public function setTTL($TTL) /** * @return string */ - public function __toString() - { + public function __toString() { return (string)$this->TTL; } } \ No newline at end of file diff --git a/src/PreparedQuery/QueryDatacenterOptions.php b/src/PreparedQuery/QueryDatacenterOptions.php index 1dd066f..18d1f10 100644 --- a/src/PreparedQuery/QueryDatacenterOptions.php +++ b/src/PreparedQuery/QueryDatacenterOptions.php @@ -22,8 +22,7 @@ * Class QueryDatacenterOptions * @package DCarbone\PHPConsulAPI\PreparedQuery */ -class QueryDatacenterOptions extends AbstractModel -{ +class QueryDatacenterOptions extends AbstractModel { /** @var int */ public $NearestN = 0; /** @var string[] */ @@ -32,8 +31,7 @@ class QueryDatacenterOptions extends AbstractModel /** * @return int */ - public function getNearestN() - { + public function getNearestN() { return $this->NearestN; } @@ -41,8 +39,7 @@ public function getNearestN() * @param int $NearestN * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryDatacenterOptions */ - public function setNearestN($NearestN) - { + public function setNearestN($NearestN) { $this->NearestN = $NearestN; return $this; } @@ -50,8 +47,7 @@ public function setNearestN($NearestN) /** * @return string[] */ - public function getDatacenters() - { + public function getDatacenters() { return $this->Datacenters; } @@ -59,11 +55,9 @@ public function getDatacenters() * @param string[] $Datacenters * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryDatacenterOptions */ - public function setDatacenters(array $Datacenters) - { + public function setDatacenters(array $Datacenters) { $this->Datacenters = []; - foreach($Datacenters as $datacenter) - { + foreach ($Datacenters as $datacenter) { $this->addDatacenter($datacenter); } return $this; @@ -73,8 +67,7 @@ public function setDatacenters(array $Datacenters) * @param string $datacenter * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryDatacenterOptions */ - public function addDatacenter($datacenter) - { + public function addDatacenter($datacenter) { $this->Datacenters[] = $datacenter; return $this; } diff --git a/src/PreparedQuery/QueryTemplate.php b/src/PreparedQuery/QueryTemplate.php index 13844b3..74e22a3 100644 --- a/src/PreparedQuery/QueryTemplate.php +++ b/src/PreparedQuery/QueryTemplate.php @@ -22,8 +22,7 @@ * Class QueryTemplate * @package DCarbone\PHPConsulAPI\PreparedQuery */ -class QueryTemplate extends AbstractModel -{ +class QueryTemplate extends AbstractModel { /** @var string */ public $Type = ''; /** @var string */ @@ -32,8 +31,7 @@ class QueryTemplate extends AbstractModel /** * @return string */ - public function getType() - { + public function getType() { return $this->Type; } @@ -41,8 +39,7 @@ public function getType() * @param string $Type * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryTemplate */ - public function setType($Type) - { + public function setType($Type) { $this->Type = $Type; return $this; } @@ -50,8 +47,7 @@ public function setType($Type) /** * @return string */ - public function getRegexp() - { + public function getRegexp() { return $this->Regexp; } @@ -59,8 +55,7 @@ public function getRegexp() * @param string $Regexp * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryTemplate */ - public function setRegexp($Regexp) - { + public function setRegexp($Regexp) { $this->Regexp = $Regexp; return $this; } diff --git a/src/PreparedQuery/ServiceQuery.php b/src/PreparedQuery/ServiceQuery.php index 0e2104b..e53d2a0 100644 --- a/src/PreparedQuery/ServiceQuery.php +++ b/src/PreparedQuery/ServiceQuery.php @@ -24,8 +24,7 @@ * Class ServiceQuery * @package DCarbone\PHPConsulAPI\PreparedQuery */ -class ServiceQuery extends AbstractModel -{ +class ServiceQuery extends AbstractModel { use HasStringTags, HasSettableStringTags; /** @var string */ @@ -41,18 +40,17 @@ class ServiceQuery extends AbstractModel * ServiceQuery constructor. * @param array $data */ - public function __construct(array $data = []) - { + public function __construct(array $data = []) { parent::__construct($data); - if (null !== $this->Failover && !($this->Failover instanceof QueryDatacenterOptions)) + if (null !== $this->Failover && !($this->Failover instanceof QueryDatacenterOptions)) { $this->Failover = new QueryDatacenterOptions((array)$this->Failover); + } } /** * @return string */ - public function getService() - { + public function getService() { return $this->Service; } @@ -60,8 +58,7 @@ public function getService() * @param string $Service * @return \DCarbone\PHPConsulAPI\PreparedQuery\ServiceQuery */ - public function setService($Service) - { + public function setService($Service) { $this->Service = $Service; return $this; } @@ -69,8 +66,7 @@ public function setService($Service) /** * @return string */ - public function getNear() - { + public function getNear() { return $this->Near; } @@ -78,8 +74,7 @@ public function getNear() * @param string $Near * @return \DCarbone\PHPConsulAPI\PreparedQuery\ServiceQuery */ - public function setNear($Near) - { + public function setNear($Near) { $this->Near = $Near; return $this; } @@ -87,8 +82,7 @@ public function setNear($Near) /** * @return \DCarbone\PHPConsulAPI\PreparedQuery\QueryDatacenterOptions */ - public function getFailover() - { + public function getFailover() { return $this->Failover; } @@ -96,8 +90,7 @@ public function getFailover() * @param \DCarbone\PHPConsulAPI\PreparedQuery\QueryDatacenterOptions $Failover * @return \DCarbone\PHPConsulAPI\PreparedQuery\ServiceQuery */ - public function setFailover(QueryDatacenterOptions $Failover) - { + public function setFailover(QueryDatacenterOptions $Failover) { $this->Failover = $Failover; return $this; } @@ -105,8 +98,7 @@ public function setFailover(QueryDatacenterOptions $Failover) /** * @return boolean */ - public function isOnlyPassing() - { + public function isOnlyPassing() { return $this->OnlyPassing; } @@ -114,8 +106,7 @@ public function isOnlyPassing() * @param boolean $OnlyPassing * @return \DCarbone\PHPConsulAPI\PreparedQuery\ServiceQuery */ - public function setOnlyPassing($OnlyPassing) - { + public function setOnlyPassing($OnlyPassing) { $this->OnlyPassing = $OnlyPassing; return $this; } diff --git a/src/QueryMeta.php b/src/QueryMeta.php index f670cd7..0077ebe 100644 --- a/src/QueryMeta.php +++ b/src/QueryMeta.php @@ -20,56 +20,50 @@ * Class QueryMeta * @package DCarbone\PHPConsulAPI */ -class QueryMeta -{ +class QueryMeta { /** @var string */ - public $requestUrl = ''; + public $RequestUrl = ''; /** @var int */ - public $lastIndex = 0; + public $LastIndex = 0; /** @var int */ - public $lastContact = 0; + public $LastContact = 0; /** @var bool */ - public $knownLeader = false; + public $KnownLeader = false; /** @var int */ - public $requestTime = 0; + public $RequestTime = 0; /** * @return string */ - public function getRequestUrl() - { - return $this->requestUrl; + public function getRequestUrl() { + return $this->RequestUrl; } /** * @return int */ - public function getLastIndex() - { - return $this->lastIndex; + public function getLastIndex() { + return $this->LastIndex; } /** * @return int */ - public function getLastContact() - { - return $this->lastContact; + public function getLastContact() { + return $this->LastContact; } /** * @return boolean */ - public function isKnownLeader() - { - return $this->knownLeader; + public function isKnownLeader() { + return $this->KnownLeader; } /** * @return int */ - public function getRequestTime() - { - return $this->requestTime; + public function getRequestTime() { + return $this->RequestTime; } } \ No newline at end of file diff --git a/src/QueryOptions.php b/src/QueryOptions.php index 26ac2c7..a9a3f51 100644 --- a/src/QueryOptions.php +++ b/src/QueryOptions.php @@ -20,167 +20,165 @@ * Class QueryOptions * @package DCarbone\PHPConsulAPI */ -class QueryOptions extends AbstractOptions -{ - use ConsulHttpParamContainerTrait; +class QueryOptions extends AbstractModel { + /** @var string */ + public $Datacenter = ''; + /** @var bool */ + public $AllowStale = false; + /** @var bool */ + public $RequireConsistent = false; + /** @var int */ + public $WaitIndex = 0; + /** @var int */ + public $WaitTime = 0; + /** @var string */ + public $Token = ''; + /** @var string */ + public $Near = ''; + /** @var array */ + public $NodeMeta = []; + /** @var int */ + public $RelayFactor = 0; + /** @var bool */ + public $Pretty = false; /** - * @return array + * @return string */ - protected function getDefinition() - { - return array( - 'Datacenter' => null, - 'AllowStale' => null, - 'RequireConsistent' => null, - 'WaitIndex' => null, - 'WaitTime' => null, - 'Token' => null, - 'Near' => null, - 'Pretty' => null, - ); + public function getDatacenter() { + return $this->Datacenter; } /** - * @return string + * @param string $Datacenter + */ + public function setDatacenter($Datacenter) { + $this->Datacenter = $Datacenter; + } + + /** + * @return bool */ - public function getDatacenter() - { - return (string)$this->_storage['Datacenter']; + public function isAllowStale() { + return $this->AllowStale; } /** - * @param string $datacenter - * @return QueryOptions + * @param bool $AllowStale */ - public function setDatacenter($datacenter) - { - $this->_storage['Datacenter'] = $datacenter; - return $this; + public function setAllowStale($AllowStale) { + $this->AllowStale = $AllowStale; } /** * @return bool */ - public function getAllowStale() - { - return (bool)$this->_storage['AllowStale']; + public function isRequireConsistent() { + return $this->RequireConsistent; } /** - * @param bool $allowStale - * @return QueryOptions + * @param bool $RequireConsistent */ - public function setAllowStale($allowStale) - { - $this->_storage['AllowStale'] = $allowStale; - return $this; + public function setRequireConsistent($RequireConsistent) { + $this->RequireConsistent = $RequireConsistent; } /** * @return int */ - public function getWaitIndex() - { - return (int)$this->_storage['WaitIndex']; + public function getWaitIndex() { + return $this->WaitIndex; } /** - * @param int $waitIndex - * @return QueryOptions + * @param int $WaitIndex */ - public function setWaitIndex($waitIndex) - { - $this->_storage['WaitIndex'] = $waitIndex; - return $this; + public function setWaitIndex($WaitIndex) { + $this->WaitIndex = $WaitIndex; } /** * @return int */ - public function getWaitTime() - { - return (int)$this->_storage['WaitTime']; + public function getWaitTime() { + return $this->WaitTime; } /** - * @param int $waitTime - * @return QueryOptions + * @param int $WaitTime */ - public function setWaitTime($waitTime) - { - $this->_storage['WaitTime'] = $waitTime; - return $this; + public function setWaitTime($WaitTime) { + $this->WaitTime = $WaitTime; } /** * @return string */ - public function getToken() - { - return (string)$this->_storage['Token']; + public function getToken() { + return $this->Token; } /** - * @param string $token - * @return QueryOptions + * @param string $Token */ - public function setToken($token) - { - $this->_storage['Token'] = $token; - return $this; + public function setToken($Token) { + $this->Token = $Token; } /** * @return string */ - public function getNear() - { - return (string)$this->_storage['Near']; + public function getNear() { + return $this->Near; } /** - * @param string $near - * @return QueryOptions + * @param string $Near */ - public function setNear($near) - { - $this->_storage['Near'] = $near; - return $this; + public function setNear($Near) { + $this->Near = $Near; } /** - * @return bool + * @return array */ - public function getRequireConsistent() - { - return (bool)$this->_storage['RequireConsistent']; + public function getNodeMeta() { + return $this->NodeMeta; } /** - * @param string $requireConsistent - * @return QueryOptions + * @param array $NodeMeta */ - public function setRequireConsistent($requireConsistent) - { - $this->_storage['RequireConsistent'] = $requireConsistent; - return $this; + public function setNodeMeta($NodeMeta) { + $this->NodeMeta = $NodeMeta; } - - public function isPretty() { - return isset($this->_storage['Pretty']); + + /** + * @return int + */ + public function getRelayFactor() { + return $this->RelayFactor; + } + + /** + * @param int $RelayFactor + */ + public function setRelayFactor($RelayFactor) { + $this->RelayFactor = $RelayFactor; } /** - * @param bool $pretty - * @return QueryOptions + * @return bool */ - public function setPretty($pretty) { - if ($pretty) - $this->_storage['Pretty'] = true; - else - unset($this->_storage['Pretty']); + public function isPretty() { + return $this->Pretty; + } - return $this; + /** + * @param bool $Pretty + */ + public function setPretty($Pretty) { + $this->Pretty = $Pretty; } } \ No newline at end of file diff --git a/src/Request.php b/src/Request.php index e999627..ea408d6 100644 --- a/src/Request.php +++ b/src/Request.php @@ -16,19 +16,22 @@ limitations under the License. */ - use GuzzleHttp\Psr7\Request as Psr7Request; use GuzzleHttp\Psr7\Stream as Psr7Stream; /** * Class Request - * @package DCarbone\PHPConsulAPI\Http + * @package DCarbone\PHPConsulAPI */ class Request { - /** @var \DCarbone\PHPConsulAPI\Config */ - private $config; + /** @var \DCarbone\PHPConsulAPI\Values */ + public $headers; /** @var \DCarbone\PHPConsulAPI\Params */ public $params; + + /** @var \DCarbone\PHPConsulAPI\Config */ + private $config; + /** @var string */ private $path; @@ -36,13 +39,10 @@ class Request { private $uri; /** @var string */ - private $method = 'POST'; - - /** @var array */ - private $headers = ['Accept' => ['application/json'], 'Content-Type' => ['application/json']]; + private $method; /** @var null|resource */ - private $body = null; + private $body; /** * Request constructor. @@ -54,23 +54,25 @@ class Request { public function __construct($method, $path, Config $config, $body = null) { $this->config = $config; - $this->params = new Params(); $this->method = strtoupper($method); $this->path = $path; - if ('' !== ($dc = $config->getDatacenter())) { - $this->params['dc'] = $dc; + $this->headers = new Values(); + $this->params = new Params(); + + if ('' !== $config->Datacenter) { + $this->params->set('dc', $config->Datacenter); } - if (0 !== ($wait = $config->getWaitTime())) { - $this->params['wait'] = $wait; + if (0 !== $config->WaitTime) { + $this->params->set('wait', $config->intToMillisecond($config->WaitTime)); } - if ('' !== ($token = $config->getToken())) { - if ($config->isTokenInHeader()) { - $this->headers['X-Consul-Token'] = $token; + if ('' !== $config->Token) { + if ($config->TokenInHeader) { + $this->headers->set('X-Consul-Token', $config->Token); } else { - $this->params['token'] = $token; + $this->params->set('token', $config->Token); } } @@ -110,60 +112,68 @@ public function __destruct() { } /** - * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions + * @param \DCarbone\PHPConsulAPI\QueryOptions|null $options */ - public function setQueryOptions(QueryOptions $queryOptions = null) { - if (null === $queryOptions) { + public function setQueryOptions(QueryOptions $options = null) { + if (null === $options) { return; } - if ('' !== ($dc = $queryOptions->getDatacenter())) { - $this->params['dc'] = $dc; + if ('' !== $options->Datacenter) { + $this->params->set('dc', $options->Datacenter); } - - if ($queryOptions->getAllowStale()) { - $this->params['stale'] = ''; + if ($options->AllowStale) { + $this->params->set('stale', ''); } - - if ($queryOptions->getRequireConsistent()) { - $this->params['consistent'] = ''; + if ($options->RequireConsistent) { + $this->params->set('consistent', ''); } - - if (0 !== ($waitIndex = $queryOptions->getWaitIndex())) { - $this->params['index'] = $waitIndex; + if (0 !== $options->WaitIndex) { + $this->params->set('index', (string)$options->WaitIndex); } - - if (0 !== ($waitTime = $queryOptions->getWaitTime())) { - $this->params['wait'] = $waitTime; + if (0 !== $options->WaitTime) { + $this->params->set('wait', $this->config->intToMillisecond($options->WaitTime)); } - - if ('' !== ($token = $queryOptions->getToken())) { - if ($this->config->isTokenInHeader()) { - $this->headers['X-Consul-Token'] = $token; + if ('' !== $options->Token) { + if ($this->config->TokenInHeader) { + $this->headers->set('X-Consul-Token', $options->Token); } else { - $this->params['token'] = $token; + $this->params->set('token', $options->Token); } } - - if ('' !== ($near = $queryOptions->getNear())) { - $this->params['near'] = $near; + if ('' !== $options->Near) { + $this->params->set('near', $options->Near); + } + if (isset($options->NodeMeta) && 0 < count($options->NodeMeta)) { + foreach ($options->NodeMeta as $k => $v) { + $this->params->add('node-meta', "{$k}:{$v}"); + } + } + if ('' !== $options->RelayFactor) { + $this->params->set('relay-factor', (string)$options->RelayFactor); } } /** - * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions + * @param \DCarbone\PHPConsulAPI\WriteOptions|null $options */ - public function setWriteOptions(WriteOptions $writeOptions = null) { - if (null === $writeOptions) { + public function setWriteOptions(WriteOptions $options = null) { + if (null === $options) { return; } - if ('' !== ($dc = $writeOptions->getDatacenter())) { - $this->params['dc'] = $dc; + if ('' !== $options->Datacenter) { + $this->params->set('dc', $options->Datacenter); } - - if ('' !== ($token = $writeOptions->getToken())) { - $this->params['token'] = $token; + if ('' !== $options->Token) { + if ($this->config->TokenInHeader) { + $this->headers->set('X-Consul-Token', $options->Token); + } else { + $this->headers->set('token', $options->Token); + } + } + if (0 !== $options->RelayFactor) { + $this->params->set('relay-factor', (string)$options->RelayFactor); } } @@ -187,7 +197,7 @@ public function toPsrRequest() { return new Psr7Request( $this->method, $this->getUri(), - $this->headers, + $this->headers->toPsr7Array(), isset($this->body) ? new Psr7Stream($this->body) : null ); } diff --git a/src/Session/SessionClient.php b/src/Session/SessionClient.php index 996e758..8da6233 100644 --- a/src/Session/SessionClient.php +++ b/src/Session/SessionClient.php @@ -26,8 +26,7 @@ * Class SessionClient * @package DCarbone\PHPConsulAPI\Session */ -class SessionClient extends AbstractClient -{ +class SessionClient extends AbstractClient { const SessionBehaviorRelease = 'release'; const SessionBehaviorDelete = 'delete'; @@ -35,31 +34,33 @@ class SessionClient extends AbstractClient * @param SessionEntry|null $sessionEntry * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions * @return array( - * @type string - * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type string + * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function createNoChecks(SessionEntry $sessionEntry = null, WriteOptions $writeOptions = null) - { - if (null === $sessionEntry) + public function createNoChecks(SessionEntry $sessionEntry = null, WriteOptions $writeOptions = null) { + if (null === $sessionEntry) { $sessionEntry = new SessionEntry; - else + } else { $sessionEntry->Checks = []; + } $r = new Request('put', 'v1/session/create', $this->c, $sessionEntry); $r->setWriteOptions($writeOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return ['', null, $err]; + } $wm = $this->buildWriteMeta($duration); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return ['', $wm, $err]; + } return [$data['ID'], $wm, null]; } @@ -68,27 +69,28 @@ public function createNoChecks(SessionEntry $sessionEntry = null, WriteOptions $ * @param SessionEntry|null $sessionEntry * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions * @return array( - * @type string - * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type string + * @type \DCarbone\PHPConsulAPI\WriteMeta write metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function create(SessionEntry $sessionEntry = null, WriteOptions $writeOptions = null) - { + public function create(SessionEntry $sessionEntry = null, WriteOptions $writeOptions = null) { $r = new Request('put', 'v1/session/create', $this->c, $sessionEntry); $r->setWriteOptions($writeOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return ['', null, $err]; + } $wm = $this->buildWriteMeta($duration); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return ['', $wm, $err]; + } return [$data['ID'], $wm, null]; } @@ -97,27 +99,27 @@ public function create(SessionEntry $sessionEntry = null, WriteOptions $writeOpt * @param string $id * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions * @return array( - * @type \DCarbone\PHPConsulAPI\WriteMeta|null write metadata or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\WriteMeta|null write metadata or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function destroy($id, WriteOptions $writeOptions = null) - { - if (!is_string($id)) - { - return [null, new Error(sprintf( - '%s::destroy - "$id" must be string, %s seen.', - get_class($this), - gettype($id) - ))]; + public function destroy($id, WriteOptions $writeOptions = null) { + if (!is_string($id)) { + return [null, + new Error(sprintf( + '%s::destroy - "$id" must be string, %s seen.', + get_class($this), + gettype($id) + ))]; } $r = new Request('put', sprintf('v1/session/destroy/%s', $id), $this->c); $r->setWriteOptions($writeOptions); list($duration, $_, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return [$this->buildWriteMeta($duration), null]; } @@ -126,20 +128,20 @@ public function destroy($id, WriteOptions $writeOptions = null) * @param string $id * @param \DCarbone\PHPConsulAPI\WriteOptions|null $writeOptions * @return array( - * @type \DCarbone\PHPConsulAPI\Session\SessionEntry[]|null list of session entries or null on error - * @type \DCarbone\PHPConsulAPI\WriteMeta|null write metadata or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\Session\SessionEntry[]|null list of session entries or null on error + * @type \DCarbone\PHPConsulAPI\WriteMeta|null write metadata or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function renew($id, WriteOptions $writeOptions = null) - { - if (!is_string($id)) - { - return [null, null, new Error(sprintf( - '%s::renew - "$id" must be string, %s seen.', - get_class($this), - gettype($id) - ))]; + public function renew($id, WriteOptions $writeOptions = null) { + if (!is_string($id)) { + return [null, + null, + new Error(sprintf( + '%s::renew - "$id" must be string, %s seen.', + get_class($this), + gettype($id) + ))]; } $r = new Request('put', sprintf('v1/session/renew/%s', $id), $this->c); @@ -147,30 +149,34 @@ public function renew($id, WriteOptions $writeOptions = null) /** @var \Psr\Http\Message\ResponseInterface $response */ list ($duration, $response, $err) = $this->doRequest($r); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $wm = $this->buildWriteMeta($duration); $code = $response->getStatusCode(); - if (404 === $code) + if (404 === $code) { return [null, $wm, null]; + } - if (200 !== $code) - { - return [null, $wm, new Error(sprintf( - '%s::renew - Unexpected response code %d. Reason: %s', - get_class($this), - $code, - $response->getReasonPhrase() - ))]; + if (200 !== $code) { + return [null, + $wm, + new Error(sprintf( + '%s::renew - Unexpected response code %d. Reason: %s', + get_class($this), + $code, + $response->getReasonPhrase() + ))]; } list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $wm, $err]; + } return [$this->_hydrateEntries($data), $wm, null]; } @@ -179,20 +185,20 @@ public function renew($id, WriteOptions $writeOptions = null) * @param string $id * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type \DCarbone\PHPConsulAPI\Session\SessionEntry[]|null list of session entries or null on error / empty response - * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\Session\SessionEntry[]|null list of session entries or null on error / empty response + * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function info($id, QueryOptions $queryOptions = null) - { - if (!is_string($id)) - { - return [null, null, new Error(sprintf( - '%s::info - "$id" must be string, %s seen.', - get_class($this), - gettype($id) - ))]; + public function info($id, QueryOptions $queryOptions = null) { + if (!is_string($id)) { + return [null, + null, + new Error(sprintf( + '%s::info - "$id" must be string, %s seen.', + get_class($this), + gettype($id) + ))]; } $r = new Request('get', sprintf('v1/session/info/%s', $id), $this->c); @@ -200,18 +206,21 @@ public function info($id, QueryOptions $queryOptions = null) /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } - if (!is_array($data) || 0 === count($data)) + if (!is_array($data) || 0 === count($data)) { return [null, $qm, null]; + } return [$this->_hydrateEntries($data), $qm, null]; } @@ -220,20 +229,20 @@ public function info($id, QueryOptions $queryOptions = null) * @param string $node * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type \DCarbone\PHPConsulAPI\Session\SessionEntry[]|null list of session entries or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata or null on error - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\Session\SessionEntry[]|null list of session entries or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta|null query metadata or null on error + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function node($node, QueryOptions $queryOptions = null) - { - if (!is_string($node)) - { - return [null, null, new Error(sprintf( - '%s::node - "$node" must be string, %s seen.', - get_class($this), - gettype($node) - ))]; + public function node($node, QueryOptions $queryOptions = null) { + if (!is_string($node)) { + return [null, + null, + new Error(sprintf( + '%s::node - "$node" must be string, %s seen.', + get_class($this), + gettype($node) + ))]; } $r = new Request('get', sprintf('v1/session/node/%s', $node), $this->c); @@ -241,15 +250,17 @@ public function node($node, QueryOptions $queryOptions = null) /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } return [$this->_hydrateEntries($data), $qm, null]; } @@ -257,27 +268,28 @@ public function node($node, QueryOptions $queryOptions = null) /** * @param \DCarbone\PHPConsulAPI\QueryOptions|null $queryOptions * @return array( - * @type \DCarbone\PHPConsulAPI\Session\SessionEntry[]|null list of session entries or null on error - * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type \DCarbone\PHPConsulAPI\Session\SessionEntry[]|null list of session entries or null on error + * @type \DCarbone\PHPConsulAPI\QueryMeta query metadata + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function listSessions(QueryOptions $queryOptions = null) - { + public function listSessions(QueryOptions $queryOptions = null) { $r = new Request('get', 'v1/session/list', $this->c); $r->setQueryOptions($queryOptions); /** @var \Psr\Http\Message\ResponseInterface $response */ list($duration, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, null, $err]; + } $qm = $this->buildQueryMeta($duration, $response, $r->getUri()); list($data, $err) = $this->decodeBody($response->getBody()); - if (null !== $err) + if (null !== $err) { return [null, $qm, $err]; + } return [$this->_hydrateEntries($data), $qm, null]; } @@ -286,11 +298,9 @@ public function listSessions(QueryOptions $queryOptions = null) * @param array $data * @return array */ - private function _hydrateEntries(array $data) - { + private function _hydrateEntries(array $data) { $entries = []; - foreach($data as $entry) - { + foreach ($data as $entry) { $entries[] = new SessionEntry($entry); } diff --git a/src/Session/SessionEntry.php b/src/Session/SessionEntry.php index e9e0bd1..8667a29 100644 --- a/src/Session/SessionEntry.php +++ b/src/Session/SessionEntry.php @@ -22,8 +22,7 @@ * Class SessionEntry * @package DCarbone\PHPConsulAPI\Session */ -class SessionEntry extends AbstractModel -{ +class SessionEntry extends AbstractModel { /** @var int */ public $CreateIndex = 0; /** @var string */ @@ -44,64 +43,56 @@ class SessionEntry extends AbstractModel /** * @return int */ - public function getCreateIndex() - { + public function getCreateIndex() { return $this->CreateIndex; } /** * @return string */ - public function getID() - { + public function getID() { return $this->ID; } /** * @return string */ - public function getName() - { + public function getName() { return $this->Name; } /** * @return string */ - public function getNode() - { + public function getNode() { return $this->Node; } /** * @return string[] */ - public function getChecks() - { + public function getChecks() { return $this->Checks; } /** * @return int */ - public function getLockDelay() - { + public function getLockDelay() { return $this->LockDelay; } /** * @return string */ - public function getBehavior() - { + public function getBehavior() { return $this->Behavior; } /** * @return string */ - public function getTTL() - { + public function getTTL() { return $this->TTL; } } \ No newline at end of file diff --git a/src/Status/StatusClient.php b/src/Status/StatusClient.php index 426c9fa..b84634d 100644 --- a/src/Status/StatusClient.php +++ b/src/Status/StatusClient.php @@ -23,23 +23,22 @@ * Class StatusClient * @package DCarbone\PHPConsulAPI\Status */ -class StatusClient extends AbstractClient -{ +class StatusClient extends AbstractClient { /** * @return array( - * @type string - * @type \DCarbone\PHPConsulAPI\Error|null error, if any + * @type string + * @type \DCarbone\PHPConsulAPI\Error|null error, if any * ) */ - public function leader() - { + public function leader() { $r = new Request('get', 'v1/status/leader', $this->c); /** @var \Psr\Http\Message\ResponseInterface $response */ list($_, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return ['', $err]; + } return $this->decodeBody($response->getBody()); } @@ -47,15 +46,15 @@ public function leader() /** * @return array|null */ - public function peers() - { + public function peers() { $r = new Request('get', 'v1/status/peers', $this->c); /** @var \Psr\Http\Message\ResponseInterface $response */ list($_, $response, $err) = $this->requireOK($this->doRequest($r)); - if (null !== $err) + if (null !== $err) { return [null, $err]; + } return $this->decodeBody($response->getBody()); } diff --git a/src/Uri.php b/src/Uri.php index 5071ea5..1028ac7 100644 --- a/src/Uri.php +++ b/src/Uri.php @@ -45,9 +45,9 @@ class Uri implements UriInterface { * Uri constructor. * @param string $path * @param Config $config - * @param Params $params + * @param Values $query */ - public function __construct($path, Config $config, Params $params) { + public function __construct($path, Config $config, Values $query) { $this->scheme = $config->Scheme; $this->userInfo = $config->HttpAuth; $this->path = $path; @@ -61,7 +61,7 @@ public function __construct($path, Config $config, Params $params) { $this->port = (int)substr($a, $pos + 1); } - $this->query = (string)$params; + $this->query = (string)$query; } public function __clone() { diff --git a/src/Values.php b/src/Values.php new file mode 100644 index 0000000..2fb66e9 --- /dev/null +++ b/src/Values.php @@ -0,0 +1,147 @@ +values[$key])) { + return $this->values[$key][0]; + } + + return ''; + } + + /** + * @param string $key + * @return string[] + */ + public function getAll($key) { + if (!is_string($key)) { + throw new \InvalidArgumentException(sprintf('$key must be string, saw "%s".', gettype($key))); + } + + if (isset($this->values[$key])) { + return $this->values[$key]; + } + + return []; + } + + /** + * @param string $key + * @param string $value + */ + public function set($key, $value) { + if (!is_string($key)) { + throw new \InvalidArgumentException(sprintf('$key must be string, saw "%s".', gettype($key))); + } + + if (!is_string($value)) { + throw new \InvalidArgumentException(sprintf('$value must be string, saw "%s".', gettype($value))); + } + + $this->values[$key] = [$value]; + } + + /** + * @param string $key + * @param string $value + */ + public function add($key, $value) { + if (!is_string($key)) { + throw new \InvalidArgumentException(sprintf('$key must be string, saw "%s".', gettype($key))); + } + + if (!is_string($value)) { + throw new \InvalidArgumentException(sprintf('$value must be string, saw "%s".', gettype($value))); + } + + if (isset($this->values[$key])) { + $this->values[$key][] = $value; + } else { + $this->values[$key] = [$value]; + } + } + + /** + * @param string $key + */ + public function delete($key) { + if (!is_string($key)) { + throw new \InvalidArgumentException(sprintf('$key must be string, saw "%s".', gettype($key))); + } + + unset($this->values[$key]); + } + + /** + * @return array + */ + public function toPsr7Array() { + return $this->values; + } + + /** + * @return array + */ + public function jsonSerialize() { + return $this->values; + } + + /** + * @return string + */ + public function __toString() { + $str = ''; + foreach ($this->values as $k => $vs) { + foreach ($vs as $v) { + if ('' !== $str) { + $str .= '&'; + } + if ('' === $v) { + $str .= $k; + } else { + $str .= sprintf('%s=%s', $k, $this->encode($v)); + } + } + } + return $str; + } + + /** + * @param string $v + * @return string mixed + */ + protected function encode($v) { + return $v; + } +} \ No newline at end of file diff --git a/src/WriteMeta.php b/src/WriteMeta.php index 3a0ea80..a4d9b23 100644 --- a/src/WriteMeta.php +++ b/src/WriteMeta.php @@ -20,16 +20,14 @@ * Class WriteMeta * @package DCarbone\PHPConsulAPI */ -class WriteMeta -{ +class WriteMeta { /** @var int */ - public $requestTime = 0; + public $RequestTime = 0; /** * @return int */ - public function getRequestTime() - { - return $this->requestTime; + public function getRequestTime() { + return $this->RequestTime; } } \ No newline at end of file diff --git a/src/WriteOptions.php b/src/WriteOptions.php index d1cebdf..434e8de 100644 --- a/src/WriteOptions.php +++ b/src/WriteOptions.php @@ -20,54 +20,53 @@ * Class WriteOptions * @package DCarbone\PHPConsulAPI */ -class WriteOptions extends AbstractOptions -{ - use ConsulHttpParamContainerTrait; +class WriteOptions extends AbstractModel { + /** @var string */ + public $Datacenter = ''; + /** @var string */ + public $Token = ''; + /** @var int */ + public $RelayFactor = 0; /** - * @return array + * @return string + */ + public function getDatacenter() { + return $this->Datacenter; + } + + /** + * @param string $Datacenter */ - protected function getDefinition() - { - return array( - 'Datacenter' => null, - 'Token' => null - ); + public function setDatacenter($Datacenter) { + $this->Datacenter = $Datacenter; } /** * @return string */ - public function getDatacenter() - { - return (string)$this->_storage['Datacenter']; + public function getToken() { + return $this->Token; } /** - * @param string $dc - * @return $this + * @param string $Token */ - public function setDatacenter($dc) - { - $this->_storage['Datacenter'] = $dc; - return $this; + public function setToken($Token) { + $this->Token = $Token; } /** - * @return string + * @return int */ - public function getToken() - { - return (string)$this->_storage['Token']; + public function getRelayFactor() { + return $this->RelayFactor; } /** - * @param string $token - * @return $this + * @param int $RelayFactor */ - public function setToken($token) - { - $this->_storage['Token'] = $token; - return $this; + public function setRelayFactor($RelayFactor) { + $this->RelayFactor = $RelayFactor; } } \ No newline at end of file