PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
← All changes | src/Framework/Models/Factories/ModelFactory.php +29 -2 2.30.04.16.9 View file →
@@ -1,12 +1,12 @@
1 1 <?php
2 2
3 3 namespace Give\Framework\Models\Factories;
4 4
5 +use Closure;
5 6 use Exception;
6 7 use Faker\Generator;
7 8 use Give\Framework\Database\DB;
8 -use Give\Framework\Models\Contracts\ModelCrud;
9 9
10 10 /**
11 11 * @template M
12 12 */
@@ -64,8 +64,20 @@
64 64 return $this->count === 1 ? $results[0] : $results;
65 65 }
66 66
67 67 /**
68 + * @since 4.0.0
69 + */
70 + public function makeAndResolveTo($property): Closure
71 + {
72 + return function() use ($property) {
73 + return is_array($results = $this->make())
74 + ? array_column($results, $property)
75 + : $results->$property;
76 + };
77 + }
78 +
79 + /**
68 80 * @since 2.20.0
69 81 *
70 82 * @param array $attributes
71 83 *
@@ -86,10 +98,23 @@
86 98 return $this->count === 1 ? $instances[0] : $instances;
87 99 }
88 100
89 101 /**
102 + * @since 4.0.0
103 + */
104 + public function createAndResolveTo($property): Closure
105 + {
106 + return function() use ($property) {
107 + return is_array($results = $this->create())
108 + ? array_column($results, $property)
109 + : $results->$property;
110 + };
111 + }
112 +
113 + /**
90 114 * Creates an instance of the model from the attributes and definition.
91 115 *
116 + * @since 4.0.0 Add support for resolving Closures.
92 117 * @since 2.23.0
93 118 *
94 119 * @return M
95 120 */
@@ -94,9 +119,11 @@
94 119 * @return M
95 120 */
96 121 protected function makeInstance(array $attributes)
97 122 {
98 - return new $this->model(array_merge($this->definition(), $attributes));
123 + return new $this->model(array_map(function($attribute) {
124 + return $attribute instanceof Closure ? $attribute() : $attribute;
125 + }, array_merge($this->definition(), $attributes)));
99 126 }
100 127
101 128 /**
102 129 * Get a new Faker instance.