/
CreateScript.php
75 lines (60 loc) · 1.75 KB
/
CreateScript.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace Charcoal\Admin\Script\Object;
// From PSR-7
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
// From 'charcoal-admin'
use Charcoal\Admin\AdminScript;
/**
*
*/
class CreateScript extends AdminScript
{
/**
* @return array
*/
public function defaultArguments()
{
$arguments = [
'obj-type' => [
'longPrefix' => 'obj-type',
'description' => 'Object type',
'defaultValue' => ''
],
'obj-id' => [
'longPrefiex' => 'obj-id',
'description' => 'Object ID',
'defaultValue' => ''
]
];
$arguments = array_merge(parent::defaultArguments(), $arguments);
return $arguments;
}
/**
* @param RequestInterface $request A PSR-7 compatible Request instance.
* @param ResponseInterface $response A PSR-7 compatible Response instance.
* @return ResponseInterface
*/
public function run(RequestInterface $request, ResponseInterface $response)
{
unset($request);
$climate = $this->climate();
$climate->underline()->out(
'Create a new object'
);
$objType = $this->argOrInput('obj-type');
$obj = $this->modelFactory()->create($objType);
$properties = $obj->properties();
$vals = [];
foreach ($properties as $prop) {
$input = $this->propertyToInput($prop);
$vals[$prop->ident()] = $input->prompt();
}
$obj->setFlatData($vals);
$ret = $obj->save();
$climate->green()->out(
"\n".sprintf('Success! Object "%s" created.', $ret)
);
return $response;
}
}