'default' => [],
)
);
+ static::addOptJson($cmd);
+ }
+
+ protected static function addOptJson(\Console_CommandLine_Command $cmd)
+ {
+ $cmd->addOption(
+ 'json',
+ array(
+ 'long_name' => '--json',
+ 'description' => 'Send request data as JSON',
+ 'action' => 'StoreTrue',
+ 'default' => false,
+ )
+ );
}
protected function handleGenericOptions(
\Console_CommandLine_Result $cmdRes, Request $req
) {
+ $this->handleOptJson($cmdRes, $req);
+
if ($cmdRes->options['published'] !== null) {
$req->req->addPostParameter(
'published', $cmdRes->options['published']
$postParams = [];
foreach ($cmdRes->options['x'] as $xproperty) {
list($propkey, $propval) = explode('=', $xproperty, 2);
- if (!isset($postParams[$propkey] )) {
+ if (!isset($postParams[$propkey])) {
$postParams[$propkey] = [];
}
$postParams[$propkey][] = $propval;
}
}
+ protected function handleOptJson(
+ \Console_CommandLine_Result $cmdRes, Request $req
+ ) {
+ $req->setSendAsJson($cmdRes->options['json']);
+ }
+
protected function handleFiles(
\Console_CommandLine_Result $cmdRes, Request $req
) {
public function run(\Console_CommandLine_Result $cmdRes)
{
$req = new Request($this->cfg->host, $this->cfg);
- $req->req->addPostParameter('h', 'entry');
- $req->req->addPostParameter('name', $cmdRes->args['title']);
+ $req->setType('entry');
+ $req->addProperty('name', $cmdRes->args['title']);
if ($cmdRes->options['html']) {
- $req->req->addPostParameter(
- 'content[html]', $cmdRes->args['text']
+ $req->addProperty(
+ 'content', ['html' => $cmdRes->args['text']]
);
} else {
- $req->req->addPostParameter('content', $cmdRes->args['text']);
+ $req->addProperty('content', $cmdRes->args['text']);
}
$this->handleGenericOptions($cmdRes, $req);
}
$req = new Request($this->cfg->host, $this->cfg);
- $req->req->addPostParameter('h', 'entry');
- $req->req->addPostParameter('bookmark-of', $url);
+ $req->setType('entry');
+ $req->addProperty('bookmark-of', $url);
if ($cmdRes->args['text']) {
- $req->req->addPostParameter('content', $cmdRes->args['text']);
+ $req->addProperty('content', $cmdRes->args['text']);
}
public static function opts(\Console_CommandLine $optParser)
{
$cmd = $optParser->addCommand('delete');
+ static::addOptJson($cmd);
$cmd->addArgument(
'url',
[
}
$req = new Request($this->cfg->host, $this->cfg);
- $req->req->addPostParameter('action', 'delete');
- $req->req->addPostParameter('url', $url);
+ $this->handleOptJson($cmdRes, $req);
+ $req->setAction('delete');
+ $req->setUrl($url);
$res = $req->send();
Log::info('Post deleted from server');
}
$req = new Request($this->cfg->host, $this->cfg);
- $req->req->addPostParameter('h', 'entry');
- $req->req->addPostParameter('like-of', $url);
+ $req->setType('entry');
+ $req->addProperty('like-of', $url);
$this->handleGenericOptions($cmdRes, $req);
$res = $req->send();
public function run(\Console_CommandLine_Result $cmdRes)
{
$req = new Request($this->cfg->host, $this->cfg);
- $req->req->addPostParameter('h', 'entry');
- $req->req->addPostParameter('content', $cmdRes->args['text']);
+ $req->setType('entry');
+ $req->addProperty('content', $cmdRes->args['text']);
$this->handleGenericOptions($cmdRes, $req);
$res = $req->send();
}
$req = new Request($this->cfg->host, $this->cfg);
- $req->req->addPostParameter('h', 'entry');
- $req->req->addPostParameter('content', implode(' ', $cmdRes->args['text']));
- $req->req->addPostParameter('in-reply-to', $url);
+ $req->setType('entry');
+ $req->addProperty('content', implode(' ', $cmdRes->args['text']));
+ $req->addProperty('in-reply-to', $url);
$this->handleGenericOptions($cmdRes, $req);
$res = $req->send();
}
$req = new Request($this->cfg->host, $this->cfg);
- $req->req->addPostParameter('h', 'entry');
- $req->req->addPostParameter('repost-of', $url);
+ $req->setType('entry');
+ $req->addProperty('repost-of', $url);
$this->handleGenericOptions($cmdRes, $req);
$res = $req->send();
}
$req = new Request($this->cfg->host, $this->cfg);
- $req->req->addPostParameter('h', 'entry');
- $req->req->addPostParameter('in-reply-to', $url);
- $req->req->addPostParameter('rsvp', $rsvp);
+ $req->setType('h', 'entry');
+ $req->addProperty('in-reply-to', $url);
+ $req->addProperty('rsvp', $rsvp);
if ($cmdRes->args['text'] != '') {
- $req->req->addPostParameter('content', $cmdRes->args['text']);
+ $req->addProperty('content', $cmdRes->args['text']);
}
$this->handleGenericOptions($cmdRes, $req);
public static function opts(\Console_CommandLine $optParser)
{
$cmd = $optParser->addCommand('undelete');
+ static::addOptJson($cmd);
$cmd->addArgument(
'url',
[
}
$req = new Request($this->cfg->host, $this->cfg);
- $req->req->addPostParameter('action', 'undelete');
- $req->req->addPostParameter('url', $url);
+ $this->handleOptJson($cmdRes, $req);
+ $req->setAction('undelete');
+ $req->setUrl($url);
$res = $req->send();
Log::info('Post restored at server');
public $req;
public $cfg;
+ protected $sendAsJson = false;
protected $uploadsInfo = [];
protected $dedicatedBody = false;
+ protected $properties = [];
+ protected $type = null;
+ protected $action = null;
+ protected $url = null;
+
public function __construct($host, $cfg)
{
$this->cfg = $cfg;
public function send($body = null)
{
+ if ($this->sendAsJson) {
+ //application/json
+ if ($body !== null) {
+ throw new \Exception('body already defined');
+ }
+ $this->req->setHeader('Content-Type: application/json');
+ $data = [];
+ if ($this->action !== null) {
+ $data['action'] = $this->action;
+ }
+ if ($this->url !== null) {
+ $data['url'] = $this->url;
+ }
+ if ($this->type !== null) {
+ $data['type'] = 'h-' . $this->type;
+ }
+ if (count($this->properties)) {
+ $data['properties'] = $this->properties;
+ }
+ $body = json_encode($data);
+ } else {
+ //form-encoded
+ if ($this->type !== null) {
+ $this->req->addPostParameter('h', $this->type);
+ }
+ if ($this->action !== null) {
+ $this->req->addPostParameter('action', $this->action);
+ }
+ if ($this->url !== null) {
+ $this->req->addPostParameter('url', $this->url);
+ }
+ foreach ($this->properties as $propkey => $propval) {
+ if (isset($propval['html'])) {
+ //workaround for content[html]
+ $propkey = $propkey . '[html]';
+ $propval = $propval['html'];
+ } else if (count($propval) == 1) {
+ $propval = reset($propval);
+ }
+ $this->req->addPostParameter($propkey, $propval);
+ }
+ }
+
if ($body !== null) {
$this->dedicatedBody = true;
$this->req->setBody($body);
return $res;
}
+ public function setAction($action)
+ {
+ $this->action = $action;
+ }
+
+ public function setType($type)
+ {
+ $this->type = $type;
+ }
+
+ public function setUrl($url)
+ {
+ $this->url = $url;
+ }
+
/**
- * @param string $fieldName name of file-upload field
- * @param string|resource|array $filename full name of local file,
+ * @param string $fieldName name of file-upload field
+ * @param string|resource|array $filename full name of local file,
* pointer to open file or an array of files
*/
public function addUpload($fieldName, $filename)
{
+ if ($this->sendAsJson) {
+ throw new \Exception('File uploads do not work with JSON');
+ }
$this->uploadsInfo[$fieldName] = $filename;
return $this->req->addUpload($fieldName, $filename);
}
/**
- * Add one or multiple POST parameters.
- * Automatically adds them as array or as string.
+ * Adds a micropub property to the request.
*
* @param string $key Parameter name
* @param string|array $values One or multiple values
*/
- public function addPostParameter($key, $values)
+ public function addProperty($key, $values)
{
- if (count($values) == 1) {
- $values = reset($values);
- }
- $this->req->addPostParameter($key, $values);
+ $this->properties[$key] = (array) $values;
}
protected function printCurl()
Log::msg($command);
}
+
+ public function setSendAsJson($json)
+ {
+ $this->sendAsJson = $json;
+ }
}
\ No newline at end of file
if (false === array_search($answer, $allowed)) {
Log::err(
'Invalid RSVP answer given; allowed are: '
- . implode (', ', $allowed)
+ . implode(', ', $allowed)
);
return false;
}