/**
  * Generate the model.
  *
  * @throws LackOfCoffeeException
  * @return Model
  */
 public function make()
 {
     // Make model instance
     $model = $this->getModelInstance(true);
     $filling = TransformPipeline::define()->inline(function ($input) {
         return array_merge($input, $this->overrides);
     })->inline(function ($input) {
         return Std::map(function ($value) {
             return Std::value($value);
         }, $input);
     })->run($this->getMapping());
     Std::each(function ($value, $key) use(&$model) {
         $model->{$key} = $value;
     }, $filling);
     Std::each(function ($relation, $name) use(&$model) {
         if (!$this->isBelongsTo($name)) {
             return;
         }
         $model->{$name}()->associate($this->generateRelation($name));
     }, $this->relations);
     $model->save();
     Std::each(function ($relation, $name) use(&$model) {
         if ($this->isBelongsTo($name)) {
             return;
         }
         $related = $this->generateRelation($name);
         if (is_array($related)) {
             $model->{$name}()->saveMany($related);
         } else {
             $model->{$name}()->save($related);
         }
     }, $this->relations);
     return $model;
 }
 /**
  * Check the given plain value against a hash.
  *
  * @param string $value
  * @param string $hashedValue
  * @param array $options
  *
  * @return bool
  */
 public function check($value, $hashedValue, array $options = [])
 {
     // First, we are optimistic and try to use the target hasher.
     if ($this->targetHasher->check($value, $hashedValue, $options)) {
         return true;
     }
     // Otherwise, we attempt to check if it passes any supported hasher.
     $match = false;
     Std::each(function (Hasher $hasher) use($value, $hashedValue, $options, &$match) {
         if ($hasher->check($value, $hashedValue, $options)) {
             $match = true;
         }
     }, $this->supportedHashers);
     return $match;
 }
 /**
  * Inject routes into the provided router.
  *
  * @param Router $router
  * @return Router
  */
 public function inject(Router $router)
 {
     $router->group(Arr::filterNullValues(['middleware' => $this->middleware, 'prefix' => $this->prefix]), function (Router $router) {
         Std::each(function (ResourceMethod $method) use($router) {
             $handler = vsprintf('%s@%s', [$this->controller, $method->getMethod()]);
             $router->match([$method->getVerb()], $method->getPath(), $handler);
         }, $this->methods);
     });
     return $router;
 }
Example #4
0
 /**
  * Build a mock from an array of CallExpectations.
  *
  * @param string $type
  * @param CallExpectation[] $expectations
  *
  * @return MockInterface
  */
 public static function expectationsToMock($type, $expectations)
 {
     return Mockery::mock($type, function (MockInterface $mock) use($expectations) {
         Std::each(function (CallExpectation $expect) use(&$mock) {
             $mockExpect = $mock->shouldReceive($expect->getMethodName())->times($expect->getTimes())->withArgs($expect->getArguments())->andReturn($expect->getReturn());
             if ($expect instanceof CallAndThrowExpectation) {
                 $mockExpect->andThrow($expect->getExceptionClass(), $expect->getExceptionMessage(), $expect->getExceptionCode());
             }
         }, $expectations);
     });
 }
 /**
  * Validate and discover all the relationships on a model.
  *
  * @return Relation[]
  */
 protected function discoverRelations()
 {
     if ($this->relations === null) {
         $this->relations = [];
         Std::each(function ($name) {
             // Check that the relationship method is there.
             if (!method_exists($this->model, $name)) {
                 throw new LackOfCoffeeException(vsprintf('The model declares a relationship named "%s" but' . ' there is no method with that name.', [$name]));
             }
             $relationReflector = new ReflectionMethod($this->model, $name);
             if (!$relationReflector->isPublic()) {
                 throw new LackOfCoffeeException(vsprintf('The method for the relationship named "%s" should' . ' be public.', [$name]));
             }
             $argumentCount = $relationReflector->getNumberOfParameters();
             if ($argumentCount > 0) {
                 throw new LackOfCoffeeException(vsprintf('The method for the relationship named "%s" should' . ' take 0 arguments. However, it requires %d.', [$name, $argumentCount]));
             }
             $relation = $this->model->{$name}();
             if (!$relation instanceof Relation) {
                 throw new LackOfCoffeeException(vsprintf('The method for the relationship named "%s" should' . ' return an instance of %s. Got %s.', [$name, Relation::class, TypeHound::fetch($relation)]));
             }
             $this->relations[$name] = $relation;
         }, $this->model->getRelated());
     }
     return $this->relations;
 }
Example #6
0
 public function testEach()
 {
     $result = '';
     $concat = function ($x) use(&$result) {
         $result .= $x;
     };
     Std::each($concat, ['h', 'e', 'l', 'l', 'o', ' ']);
     Std::each($concat, ArrayList::of(['w', 'o', 'r', 'l', 'd']));
     Std::each($concat, new ArrayObject([' ', ':', ')']));
     $this->assertEquals('hello world :)', $result);
 }