4 class Command_Update extends Command_AbstractProps
6 public static function opts(\Console_CommandLine $optParser)
8 $cmd = $optParser->addCommand('update');
13 'long_name' => '--add',
14 'description' => 'Property to add',
15 'help_name' => 'PROP=VAL',
16 'action' => 'StoreArray',
24 'long_name' => '--replace',
25 'description' => 'Property to remove',
26 'help_name' => 'PROP=VAL',
27 'action' => 'StoreArray',
35 'long_name' => '--delete',
36 'description' => 'Property to delete',
37 'help_name' => 'PROP=VAL',
38 'action' => 'StoreArray',
47 'description' => 'Post URL',
52 public function run(\Console_CommandLine_Result $cmdRes)
54 $url = Validator::url($cmdRes->args['url'], 'url');
59 $req = new Request($this->cfg->host, $this->cfg);
71 foreach (['add', 'delete', 'replace'] as $part) {
72 if (!count($cmdRes->options[$part])) {
75 foreach ($cmdRes->options[$part] as $kvpair) {
76 list($prop, $val) = explode('=', $kvpair, 2);
77 if (!isset($parts[$part][$prop])) {
78 $parts[$part][$prop] = [];
80 $parts[$part][$prop][] = $val;
83 foreach ($parts as $part => $changes) {
84 if (count($changes)) {
85 $json[$part] = $changes;
89 $req->req->setHeader('Content-Type: application/json');
90 $res = $req->send(json_encode($json));
91 $newPostUrl = $res->getHeader('Location');
92 Log::info('Post updated at server');
94 Log::info('Post has a new URL:');
95 Log::msg($newPostUrl);