/** * @param string|null $type If specified, return the type value. See {@see User::getAllowedTypes()} to see valid * type names; if is invalid will return en empty array. Otherwise return an array with a * random of types. * * @return array Array of user types. */ public function userTypes($type = null) { $allowedTypes = User::getAllowedTypes(); if (null === $type) { return BaseProvider::randomElements($allowedTypes, BaseProvider::numberBetween(1, count($allowedTypes))); } return isset($allowedTypes[$type]) ? [$allowedTypes[$type]] : []; }
/** * @testdox Test the UserProvider::userTypes * * @covers ::userTypes */ public function testUserTypes() { $allowedTypes = User::getAllowedTypes(); // Test random generation for ($i = 0; $i <= self::N; ++$i) { $types = $this->provider->userTypes(); foreach ($types as $type) { $this->assertTrue(in_array($type, $allowedTypes), 'Expected to generate a valid type'); } } // Test get specified type $this->assertEquals([User::TYPE_CONTRACTOR], $this->provider->userTypes('contractor')); $this->assertEquals([User::TYPE_MEMBER], $this->provider->userTypes('member')); // Test get invalid type $this->assertEquals([], $this->provider->userTypes('unknown')); }
/** * {@inheritdoc} */ public function applyFilter(ResourceInterface $resource, QueryBuilder $queryBuilder, array $queryValues) { // ?filter[where][type]=TYPE_CONTRACTOR // ?filter[where][type]=contractor if (isset($queryValues['type']) && is_string($queryValues['type'])) { if (isset(User::getAllowedTypes()[$queryValues['type']])) { $type = User::getAllowedTypes()[$queryValues['type']]; } elseif (in_array($queryValues['type'], User::getAllowedTypes())) { $type = $queryValues['type']; } else { return; } //TODO: secure parameters $queryBuilder->andWhere('o.types LIKE :user_type')->setParameter('user_type', sprintf('%%%s%%', $type)); } }