/** * Apply the given action to the provided roles. * * @param string $action * @param array $roles * @return \Illuminate\Database\Eloquent\Model */ private function applyToRoles(string $action, array $roles) { if (array_every($roles, 'is_string')) { $roles = Role::whereIn('slug', $roles)->get()->toArray(); } call_user_func_array([$this->roles(), $action], [array_pluck($roles, 'id')]); return $this->fresh(); }
/** * @test */ public function it_tests_array_every() { $odds = [1, 3, 5, 7, 9]; $strings = ['hello', 'j', 'and', 1, 'string', true]; $this->assertTrue(array_every($odds, function ($n) { return $n % 2 == 1; })); $this->assertFalse(array_every($strings, function ($n) { return is_string($n); })); }
/** * Wrapper method around instance {@see calculate()} method * * This wrapper allows the Operator instance to be called as a function to calculate * the result of its operand(s) and also provide type checking for the operands before * being passed to {@see calculate()} */ public function __invoke() { $args = func_get_args(); if (!array_every($args, 'is_numeric')) { throw new InvalidArgumentException(get_class($this) . '::calculate() expects all arguments to be numbers'); } return call_user_func_array([$this, 'calculate'], $args); }
/** * @param string $code * * @return bool */ public function hasPermission($code) { // Collect all the codes $permissionCodes = array_map(function (PermissionInterface $permission) { return $permission->getCode(); }, $this->permissions); if (!is_array($code)) { return in_array($code, $permissionCodes); } return array_every($code, function ($permissionCode) use($permissionCodes) { return in_array($permissionCode, $permissionCodes); }); }