| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * CakeCMS Core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is part of the of the simple cms based on CakePHP 3. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package     Core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license     MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @link        https://github.com/CakeCMS/Core". | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author      Sergey Kalistratov <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace Core\Controller\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Cake\ORM\Table; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use JBZoo\Utils\Arr; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Cake\Controller\Component; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Cake\ORM\Behavior\TreeBehavior; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * Class MoveComponent | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @package     Core\Controller\Component | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @property    FlashComponent $Flash | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | class MoveComponent extends AppComponent | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     const TYPE_UP   = 'moveUp'; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     const TYPE_DOWN = 'moveDown'; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * Other Components this component uses. | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     public $components = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         'Core.Flash' | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * Move down record in tree. | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param   Table $table | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @param   int $id | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * @param   int $step | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @return  \Cake\Http\Response|null | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function down(Table $table, $id, $step = 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return $this->_move($table, $id, $step, self::TYPE_DOWN); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * Sets the config. | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * @param   array|string $key | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @param   null|mixed $value | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @param   bool $merge | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * @return  mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @throws  \Aura\Intl\Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @throws  \Cake\Core\Exception\Exception When trying to set a key that is invalid. | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function setConfig($key, $value = null, $merge = true) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->_defaultConfig = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'messages' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 'success' => __d('core', 'Object has been moved'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                 'error'   => __d('core', 'Object could not been moved') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'action' => 'index', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return parent::setConfig($key, $value, $merge); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * Move up record in tree. | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * @param   Table $table | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @param   int $id | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * @param   int $step | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @return  \Cake\Http\Response|null | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      * @SuppressWarnings(PHPMD.ShortMethodName) | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function up(Table $table, $id, $step = 1) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         return $this->_move($table, $id, $step); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * Move object in tree table. | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * @param   Table $table | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      * @param   string $type | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |      * @param   int $id | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |      * @param   int $step | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |      * @return  \Cake\Http\Response|null | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     protected function _move(Table $table, $id, $step = 1, $type = self::TYPE_UP) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $behaviors = $table->behaviors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         if (!Arr::in('Tree', $behaviors->loaded())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $behaviors->load('Tree'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $entity = $table->get($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         /** @var TreeBehavior $treeBehavior */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $treeBehavior = $behaviors->get('Tree'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $treeBehavior->setConfig('scope', $entity->get('id')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         if ($table->{$type}($entity, $step)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $this->Flash->success($this->_configRead('messages.success')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |             $this->Flash->error($this->_configRead('messages.error')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         return $this->_redirect(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |      * Process redirect. | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |      * @return  \Cake\Http\Response|null | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     protected function _redirect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $request = $this->_controller->request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $this->_controller->redirect([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             'prefix'     => $request->getParam('prefix'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             'plugin'     => $request->getParam('plugin'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             'controller' => $request->getParam('controller'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             'action'     => $this->getConfig('action') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         ]); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 144 |  |  |  | 
            
                        
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.