4 class Command_Update extends Command_AbstractProps
6 public static function opts(\Console_CommandLine $optParser)
8 $cmd = $optParser->addCommand('update');
9 $cmd->description = 'Modify an existing post';
14 'long_name' => '--add',
15 'description' => 'Property to add',
16 'help_name' => 'PROP=VAL',
17 'action' => 'StoreArray',
25 'long_name' => '--replace',
26 'description' => 'Property to remove',
27 'help_name' => 'PROP=VAL',
28 'action' => 'StoreArray',
36 'long_name' => '--delete',
37 'description' => 'Property to delete',
38 'help_name' => 'PROP=VAL',
39 'action' => 'StoreArray',
48 'description' => 'Post URL',
53 public function run(\Console_CommandLine_Result $cmdRes)
55 $url = Validator::url($cmdRes->args['url'], 'url');
60 $req = new Request($this->cfg->host, $this->cfg);
72 foreach (['add', 'delete', 'replace'] as $part) {
73 if (!count($cmdRes->options[$part])) {
76 foreach ($cmdRes->options[$part] as $kvpair) {
77 list($prop, $val) = explode('=', $kvpair, 2);
78 if (!isset($parts[$part][$prop])) {
79 $parts[$part][$prop] = [];
81 $parts[$part][$prop][] = $val;
84 foreach ($parts as $part => $changes) {
85 if (count($changes)) {
86 $json[$part] = $changes;
90 $req->req->setHeader('Content-Type: application/json');
91 $res = $req->send(json_encode($json));
92 $newPostUrl = $res->getHeader('Location');
93 Log::info('Post updated at server');
95 Log::info('Post has a new URL:');
96 Log::msg($newPostUrl);