Exemplo n.º 1
0
 /**
  * Generate a unqiue text-identifier for a given table and column from any input string.
  *
  * Will generate a text identifier from the string (lowercase, alphanum-only), search for the value in the given table/column,
  * and keep incrementing a suffixed counter until the value is unique.
  *
  * @param \pdyn\database\DbDriverInterface $DB An active database connection.
  * @param string $input Any input text.
  * @param string $table The table in which to ensure uniqueness.
  * @param string $field The column in which to ensure uniqueness.
  * @param array $restriction_list Array of values the text-identifier cannot match.
  * @return string The generated, unique text identifier.
  */
 public static function generate_slug(\pdyn\database\DbDriverInterface $DB, $input, $table, $field, array $restriction_list = array())
 {
     $i = 0;
     while (true) {
         $slug = \pdyn\datatype\Text::make_slug($input);
         $slug .= $i != 0 ? '_' . $i : '';
         // This is so we don't add the increment on the first loop.
         if (!empty($restriction_list) && in_array($slug, $restriction_list, true)) {
             $i++;
             continue;
         }
         $found = $DB->get_record($table, [$field => $slug]);
         if (empty($found)) {
             break;
         }
         $i++;
     }
     return $slug;
 }
Exemplo n.º 2
0
 /**
  * Tests make_slug function.
  *
  * @dataProvider dataprovider_makeSlug
  */
 public function test_makeSlug($text, $expected)
 {
     $actual = \pdyn\datatype\Text::make_slug($text);
     $this->assertEquals($expected, $actual);
 }