| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * RuleTransformer.php | 
					
						
							|  |  |  |  * Copyright (c) 2018 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is part of Firefly III. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Transformers; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Models\Rule; | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  | use League\Fractal\Resource\Collection as FractalCollection; | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  | use League\Fractal\Resource\Item; | 
					
						
							|  |  |  | use League\Fractal\TransformerAbstract; | 
					
						
							|  |  |  | use Symfony\Component\HttpFoundation\ParameterBag; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class RuleTransformer | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class RuleTransformer extends TransformerAbstract | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * List of resources possible to include | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $availableIncludes = ['rule_group', 'rule_triggers', 'rule_actions', 'user']; | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * List of resources to automatically include | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @var array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected $defaultIncludes = ['rule_group', 'rule_triggers', 'rule_actions']; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** @var ParameterBag */ | 
					
						
							|  |  |  |     protected $parameters; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * CurrencyTransformer constructor. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param ParameterBag $parameters | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(ParameterBag $parameters) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->parameters = $parameters; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Rule $rule | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return FractalCollection | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |     public function includeRuleActions(Rule $rule): FractalCollection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->collection($rule->ruleActions, new RuleActionTransformer($this->parameters), 'rule_actions'); | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |      * Include the rule group. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |      * @param Rule $rule | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * @return Item | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |     public function includeRuleGroup(Rule $rule): Item | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->item($rule->ruleGroup, new RuleGroupTransformer($this->parameters), 'rule_groups'); | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param Rule $rule | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |      * @return FractalCollection | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |     public function includeRuleTriggers(Rule $rule): FractalCollection | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |         return $this->collection($rule->ruleTriggers, new RuleTriggerTransformer($this->parameters), 'rule_triggers'); | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |      * Include the user. | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @param Rule $rule | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @codeCoverageIgnore | 
					
						
							|  |  |  |      * @return Item | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |     public function includeUser(Rule $rule): Item | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-06-30 05:21:21 +02:00
										 |  |  |         return $this->item($rule->user, new UserTransformer($this->parameters), 'users'); | 
					
						
							| 
									
										
										
										
											2018-06-24 06:51:22 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Transform the rule. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Rule $rule | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return array | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function transform(Rule $rule): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data = [ | 
					
						
							|  |  |  |             'id'              => (int)$rule->id, | 
					
						
							|  |  |  |             'updated_at'      => $rule->updated_at->toAtomString(), | 
					
						
							|  |  |  |             'created_at'      => $rule->created_at->toAtomString(), | 
					
						
							|  |  |  |             'title'           => $rule->title, | 
					
						
							|  |  |  |             'text'            => $rule->text, | 
					
						
							|  |  |  |             'order'           => (int)$rule->order, | 
					
						
							|  |  |  |             'active'          => $rule->active, | 
					
						
							|  |  |  |             'stop_processing' => $rule->stop_processing, | 
					
						
							|  |  |  |             'strict'          => $rule->strict, | 
					
						
							|  |  |  |             'links'           => [ | 
					
						
							|  |  |  |                 [ | 
					
						
							|  |  |  |                     'rel' => 'self', | 
					
						
							|  |  |  |                     'uri' => '/rules/' . $rule->id, | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $data; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-22 20:32:02 +02:00
										 |  |  | } |