-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobile_number.devel_generate.inc
executable file
·88 lines (75 loc) · 2.36 KB
/
mobile_number.devel_generate.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* @file
* Provides devel generate integration.
*/
use libphonenumber\PhoneNumberUtil;
require_once drupal_get_path('module', 'mobile_number') . '/include/mobile_number.libphonenumber.inc';
/**
* Implements hook_devel_generate().
*/
function mobile_number_devel_generate($object, $field, $instance, $bundle) {
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
return devel_generate_multiple('_mobile_number_devel_generate', $object, $field, $instance, $bundle);
}
else {
return _mobile_number_devel_generate($object, $field, $instance, $bundle);
}
}
/**
* Devel generate callback for phone field types.
*/
function _mobile_number_devel_generate($object, $field, $instance, $bundle) {
static $last_numbers = array();
$util = PhoneNumberUtil::getInstance();
$settings = array(
'verify' => MOBILE_NUMBER_VERIFY_NONE,
'countries' => array(),
);
if ($instance['widget']['type'] == 'mobile_number_default') {
$settings = $instance['widget']['settings'];
}
$country = array_rand(MobileNumber::getCountryOptions($settings['countries']));
$last = !empty($last_numbers[$country]) ? $last_numbers[$country] : array();
$mobile_number = NULL;
if (!$last) {
$last['count'] = 0;
$last['example'] = ($number = $util->getExampleNumberForType($country, 1)) ? $number->getNationalNumber() : NULL;
}
$example = $last['example'];
$count = $last['count'];
if ($example) {
while (strlen($count) <= strlen($example) && !$mobile_number) {
$number_length = strlen($example);
$number = substr($example, 0, $number_length - strlen($count)) . $count;
if (substr($count, 0, 1) != substr($example, strlen($count) - 1, 1)) {
try {
$mobile_number = new MobileNumber($number, $country);
break;
}
catch (Exception $e) {
}
}
$count++;
};
}
$value = array();
if ($mobile_number) {
$value = array(
'value' => $mobile_number->callableNumber,
);
switch ($settings['verify']) {
case MOBILE_NUMBER_VERIFY_NONE:
$value['verified'] = 0;
break;
case MOBILE_NUMBER_VERIFY_OPTIONAL:
$value['verified'] = rand(0, 1);
break;
case MOBILE_NUMBER_VERIFY_REQUIRED:
$value['verified'] = 1;
break;
}
}
$last_numbers[$country] = $last;
return $value;
}