1 | <?php |
||
15 | trait CollectionTrait |
||
16 | { |
||
17 | use DependsOnManagesItemsTrait; |
||
18 | |||
19 | /* Traits cannot declare constants, so we mimic constants with static properties */ |
||
20 | public static $RETURN_ARRAY = "_return_array"; |
||
21 | public static $RETURN_COLLECTION = "_return_collection"; |
||
22 | public static $MODIFY_MANIFEST = "_modify_manifest"; |
||
23 | |||
24 | /** |
||
25 | * Configuration: do we want to return Collections from get() and getAll()? |
||
26 | * @var bool |
||
27 | */ |
||
28 | public $useCollections = true; |
||
29 | |||
30 | /** |
||
31 | * Converts an array to a collection if value is arrayable and config is set |
||
32 | * @param $value |
||
33 | * @return static |
||
34 | */ |
||
35 | public function toCollection($value) |
||
43 | |||
44 | /** |
||
45 | * Does this instance want collections returned from get() and getAll()? |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function wantsCollections() |
||
52 | |||
53 | /** |
||
54 | * Invokes when calling a method on the Collection API |
||
55 | * |
||
56 | * This method simply decides how to handle the method call. |
||
57 | * 1. The class is using the ChainsNestedItemsTrait and Collection API does NOT contain the method |
||
58 | * Let `ChainsNestedItemsTrait` do its thing |
||
59 | * 2. The Collection API DOES contain the method |
||
60 | * Pass the method call along to the third party Collection API |
||
61 | * 3. The method does not exist on the class or in the Collection API |
||
62 | * Throw an Exception |
||
63 | * @param string $method Name of the method |
||
64 | * @param array $arguments Arguments passed along |
||
65 | * @return mixed |
||
66 | * @throws \BadMethodCallException |
||
67 | */ |
||
68 | public function __call($method, $arguments) |
||
88 | |||
89 | /** |
||
90 | * Checks to see if the Collection API contains a specific method |
||
91 | * @param string $method name |
||
92 | * @return bool |
||
93 | */ |
||
94 | protected function collectionApiHasMethod($method) |
||
98 | |||
99 | /** |
||
100 | * Checks to see if the current Manager class is using `ChainsNestedItemsTrait` |
||
101 | * @return bool |
||
102 | */ |
||
103 | protected function usingNestedItemsTrait() |
||
107 | |||
108 | /** |
||
109 | * Passes the method call along to the Collection API (currently Arrayzy) |
||
110 | * Also checks for any flags that determine how return data should be formatted |
||
111 | * |
||
112 | * @param string $method name |
||
113 | * @param array $arguments to be passed along (including return type flags if exists) |
||
114 | * @return mixed |
||
115 | */ |
||
116 | protected function passToCollectionApi($method, $arguments) |
||
139 | |||
140 | /** |
||
141 | * Calls the actual method on the Collection Instance (currently Arrayzy) |
||
142 | * |
||
143 | * @param string $method name |
||
144 | * @param array $arguments to be passed along |
||
145 | * @param object $instance of the Collection |
||
146 | * @param string $flag corresponding to the properties above |
||
147 | * @param string $subject Alias of data in Manager |
||
148 | * @return mixed |
||
149 | */ |
||
150 | protected function callCollectionMethod($method, $arguments, $instance, $flag, $subject) |
||
167 | } |
||
168 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: