diff --git a/composer.json b/composer.json index 4c78f3c..ab078c2 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,7 @@ { "name": "hutattedonmyarm/apnuti", "type": "library", + "version": "0.1.0", "description": "PHP Pnut library", "keywords": ["pnut", "api"], "license": "MIT", diff --git a/src/APnutI.php b/src/APnutI.php index 5a27ff6..6bd0719 100644 --- a/src/APnutI.php +++ b/src/APnutI.php @@ -36,7 +36,6 @@ class APnutI protected array $headers = []; protected ?string $server_token; protected ?string $access_token; - protected LoggerInterface $logger; protected string $token_session_key; protected string $token_redirect_after_auth; protected ?string $server_token_file_path = null; @@ -45,6 +44,7 @@ class APnutI public ?Meta $meta = null; public string $app_name = 'Abstract API'; + public LoggerInterface $logger; /* * Error codes: diff --git a/src/Entities/Poll.php b/src/Entities/Poll.php index 262436d..7301964 100644 --- a/src/Entities/Poll.php +++ b/src/Entities/Poll.php @@ -151,6 +151,45 @@ class Poll return $this-api->voteInPoll($this->id, $options, $this->token); } + /* + * This is inconsistend with most other functions (which are provided directly by the API object. + * I should probably settle on one of the two styles. + * I prefer having the methods in here, but having to pass the API object along is not so great, + * neither is having to make the API's logger public + * TODO for v2 I guess + */ + public static function create( + APnutI $api, + string $prompt, + array $options, + int $max_options, + int $duration_minutes, + bool $is_anonymous, + bool $is_public + ): Poll { + $options = array_filter($options); + $options = array_map( + function ($v) { + return ['text' => $v]; + }, + $options + ); + $params = [ + 'duration' => $duration_minutes, + 'options' => array_filter($options), #filters empty options + 'prompt' => $prompt, + 'type' => 'io.pnut.core.poll', + 'is_anonymous' => $is_anonymous, + 'is_public' => $is_public, + 'max_options' => $max_options + ]; + $api->logger->debug('Creating poll'); + $api->logger->debug(json_encode($params)); + $resp = $api->post('/polls', $params, 'application/json'); + #TODO: Use getPollFromEndpoint + return new Poll($resp, $api); + } + public function __toString(): string { if (!empty($this->user)) { diff --git a/src/Meta.php b/src/Meta.php index aca963f..d6a3032 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -33,7 +33,8 @@ class Meta $this->code = $meta['code']; if ($this->code === 400) { throw new PnutException($meta['error_message']); - }if ($this->code === 401) { + } + if ($this->code === 401) { throw new NotAuthorizedException($meta['error_message']); } if ($this->code === 403) {