28a1d150f9320b1403969d72ab243cca39a4b113
[yaffs-website] / vendor / consolidation / robo / src / Common / DynamicParams.php
1 <?php
2 namespace Robo\Common;
3
4 /**
5  * Simplifies generating of configuration chanined methods.
6  * You can only define configuration properties and use magic methods to set them.
7  * Methods will be named the same way as properties.
8  * * Boolean properties are switched on/off if no values is provided.
9  * * Array properties can accept non-array values, in this case value will be appended to array.
10  * You should also define phpdoc for methods.
11  */
12 trait DynamicParams
13 {
14     /**
15      * @param string $property
16      * @param array $args
17      *
18      * @return $this
19      */
20     public function __call($property, $args)
21     {
22         if (!property_exists($this, $property)) {
23             throw new \RuntimeException("Property $property in task ".get_class($this).' does not exists');
24         }
25
26         // toggle boolean values
27         if (!isset($args[0]) and (is_bool($this->$property))) {
28             $this->$property = !$this->$property;
29             return $this;
30         }
31
32         // append item to array
33         if (is_array($this->$property)) {
34             if (is_array($args[0])) {
35                 $this->$property = $args[0];
36             } else {
37                 array_push($this->$property, $args[0]);
38             }
39             return $this;
40         }
41
42         $this->$property = $args[0];
43         return $this;
44     }
45 }