| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace keeko\core\serializer\base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use keeko\framework\utils\HydrateUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Tobscure\JsonApi\Relationship; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use keeko\core\model\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Tobscure\JsonApi\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use keeko\core\model\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | trait GroupSerializerTrait { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 * @param mixed $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	 * @return Relationship | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	public function actions($model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 		$relationship = new Relationship(new Collection($model->getActions(), Action::getSerializer())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 		return $this->addRelationshipSelfLink($relationship, $model, 'actions'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	 * @param mixed $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * @param array $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	public function getAttributes($model, array $fields = null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			'id' => $model->getId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 			'owner_id' => $model->getOwnerId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			'name' => $model->getName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 			'is_guest' => $model->getIsGuest(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 			'is_default' => $model->getIsDefault(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 			'is_active' => $model->getIsActive(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 			'is_system' => $model->getIsSystem(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	public function getFields() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 		return ['id', 'owner_id', 'name', 'is_guest', 'is_default', 'is_active', 'is_system', 'created_at', 'updated_at']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * @param mixed $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  | 	public function getId($model) { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  | 		return $model->getId(); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	public function getRelationships() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			'users' => User::getSerializer()->getType(null), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 			'actions' => Action::getSerializer()->getType(null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	public function getSortFields() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		return ['id', 'owner_id', 'name', 'is_guest', 'is_default', 'is_active', 'is_system']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * @param mixed $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	public function getType($model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		return 'core/group'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * @param mixed $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * @param mixed $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * @return mixed The model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	public function hydrate($model, $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		// attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$attribs = isset($data['attributes']) ? $data['attributes'] : []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'owner_id', 'name', 'is_guest', 'is_default', 'is_active', 'is_system']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		// relationships | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$this->hydrateRelationships($model, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		return $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @param mixed $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @return Relationship | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	public function users($model) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$relationship = new Relationship(new Collection($model->getUsers(), User::getSerializer())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		return $this->addRelationshipSelfLink($relationship, $model, 'users'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * @param mixed $model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 * @param mixed $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	abstract protected function hydrateRelationships($model, $data); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  | 
            
                        
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.