|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under The MIT License |
|
6
|
|
|
* Redistributions of files must retain the above copyright notice. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com) |
|
9
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CakeDC\Api\Service; |
|
13
|
|
|
|
|
14
|
|
|
use Cake\Utility\Hash; |
|
15
|
|
|
use Cake\Utility\Inflector; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class NestedCrudService |
|
19
|
|
|
* |
|
20
|
|
|
* @package CakeDC\Api\Service |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class NestedCrudService extends CrudService |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $_parentIdName = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* NestedCrudService constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $config Service settings. |
|
34
|
|
|
*/ |
|
35
|
130 |
|
public function __construct(array $config = []) |
|
36
|
|
|
{ |
|
37
|
130 |
|
parent::__construct($config); |
|
38
|
130 |
|
if (isset($config['parentIdName'])) { |
|
39
|
|
|
$this->_parentIdName = $config['parentIdName']; |
|
40
|
|
|
} |
|
41
|
130 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Action constructor options. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $route Action route, |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
55 |
|
protected function _actionOptions($route) |
|
50
|
|
|
{ |
|
51
|
55 |
|
$parent = $this->getParentService(); |
|
52
|
55 |
|
if ($this->_parentIdName === null && $parent instanceof Service) { |
|
53
|
9 |
|
$parentName = $parent->getName(); |
|
54
|
9 |
|
$parentIdName = Inflector::singularize($parentName) . '_id'; |
|
55
|
9 |
|
if (array_key_exists($parentIdName, $route)) { |
|
56
|
9 |
|
$this->_parentIdName = $parentIdName; |
|
57
|
9 |
|
} |
|
58
|
9 |
|
} |
|
59
|
55 |
|
$parentId = null; |
|
60
|
55 |
|
if ($this->_parentIdName !== null && isset($route[$this->_parentIdName])) { |
|
61
|
9 |
|
$parentId = $route[$this->_parentIdName]; |
|
62
|
9 |
|
} |
|
63
|
|
|
$options = [ |
|
64
|
55 |
|
'parentId' => $parentId, |
|
65
|
55 |
|
'parentIdName' => $this->_parentIdName, |
|
66
|
55 |
|
]; |
|
67
|
55 |
|
if ($parentId !== null) { |
|
68
|
9 |
|
$options['Extension'] = ['CakeDC/Api.Nested']; |
|
69
|
9 |
|
} |
|
70
|
|
|
|
|
71
|
55 |
|
return Hash::merge(parent::_actionOptions($route), $options); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|