Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
# libphonenumber for PHP (Lite) [![Build Status](https://github.com/giggsey/libphonenumber-for-php-lite/workflows/Tests/badge.svg)](https://github.com/giggsey/libphonenumber-for-php-lite/actions?query=workflow%3A%22Tests%22) [![codecov](https://codecov.io/gh/giggsey/libphonenumber-for-php-lite/branch/main/graph/badge.svg?token=S0TV2TAXOQ)](https://codecov.io/gh/giggsey/libphonenumber-for-php-lite) [![Mutation testing badge](https://img.shields.io/endpoint?style=flat-square&url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fgiggsey%2Flibphonenumber-for-php-lite%2Fmain)](https://dashboard.stryker-mutator.io/reports/github.com/giggsey/libphonenumber-for-php-lite/main)
2
 
3
 
4
[![Total Downloads](https://poser.pugx.org/giggsey/libphonenumber-for-php-lite/downloads?format=flat-square)](https://packagist.org/packages/giggsey/libphonenumber-for-php-lite)
5
[![Downloads per month](https://img.shields.io/packagist/dm/giggsey/libphonenumber-for-php-lite.svg?style=flat-square)](https://packagist.org/packages/giggsey/libphonenumber-for-php-lite)
6
[![Latest Stable Version](https://img.shields.io/packagist/v/giggsey/libphonenumber-for-php-lite.svg?style=flat-square)](https://packagist.org/packages/giggsey/libphonenumber-for-php-lite)
7
[![License](https://img.shields.io/badge/license-Apache%202.0-red.svg?style=flat-square)](https://packagist.org/packages/giggsey/libphonenumber-for-php-lite)
8
 
9
## What is it?
10
A PHP library for parsing, formatting, storing and validating international phone numbers. This library is based on Google's [libphonenumber](https://github.com/google/libphonenumber).
11
 
12
This is a lite version that only includes the core Phone Number Utils. To make full use of the library, including geolocation, carrier information and short number info, use [giggsey/libphonenumber-for-php](https://github.com/giggsey/libphonenumber-for-php)
13
 
14
 - [Installation](#installation)
15
 - [Documentation](#documentation)
16
 - [Highlights of functionality](#highlights-of-functionality)
17
   - [Versioning](#versioning)
18
   - [Quick Examples](#quick-examples)
19
 - [FAQ](#faq)
20
   - [Problems with Invalid Numbers?](#problems-with-invalid-numbers)
21
 - [Generating data](#generating-data)
22
 
23
## Installation
24
 
25
PHP versions 8.1 and above are supported.
26
 
27
The PECL [mbstring](http://php.net/mbstring) extension is required.
28
 
29
It is recommended to use [composer](https://getcomposer.org) to install the library.
30
 
31
```bash
32
$ composer require giggsey/libphonenumber-for-php-lite
33
```
34
 
35
You can also use any other [PSR-4](http://www.php-fig.org/psr/psr-4/) compliant autoloader.
36
 
37
### PHP Version Policy
38
 
39
This library will be updated to use [supported versions of PHP](https://www.php.net/supported-versions.php) only. At the moment, the main [giggsey/libphonenumber-for-php](https://github.com/giggsey/libphonenumber-for-php) library supports older PHP versions.
40
 
41
## Documentation
42
 
43
 - [PhoneNumber Util](docs/PhoneNumberUtil.md)
44
 
45
# Highlights of functionality
46
* Parsing/formatting/validating phone numbers for all countries/regions of the world.
47
* `getNumberType` - gets the type of the number based on the number itself; able to distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible).
48
* `isNumberMatch` - gets a confidence level on whether two numbers could be the same.
49
* `getExampleNumber`/`getExampleNumberByType` - provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed.
50
* `isValidNumber` - full validation of a phone number for a region using length and prefix information.
51
 
52
## Versioning
53
 
54
This library will try to follow the same version numbers as Google. There could be additional releases where needed to fix critical issues that can not wait until the next release from Google.
55
 
56
This does mean that this project may not follow [Semantic Versioning](http://semver.org/), but instead Google's version policy. As a result, jumps in major versions may not actually contain any backwards
57
incompatible changes. Please read the release notes for such releases.
58
 
59
Google try to release their versions according to Semantic Versioning, as laid out of in their [Versioning Guide](https://github.com/google/libphonenumber#versioning-and-announcements).
60
 
61
## Quick Examples
62
Let's say you have a string representing a phone number from Switzerland. This is how you parse/normalize it into a PhoneNumber object:
63
 
64
```php
65
$swissNumberStr = "044 668 18 00";
66
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
67
try {
68
    $swissNumberProto = $phoneUtil->parse($swissNumberStr, "CH");
69
    var_dump($swissNumberProto);
70
} catch (\libphonenumber\NumberParseException $e) {
71
    var_dump($e);
72
}
73
```
74
 
75
At this point, swissNumberProto contains:
76
 
77
    class libphonenumber\PhoneNumber#9 (7) {
78
     private $countryCode =>
79
      int(41)
80
     private $nationalNumber =>
81
      double(446681800)
82
     private $extension =>
83
      NULL
84
     private $italianLeadingZero =>
85
      NULL
86
     private $rawInput =>
87
      NULL
88
     private $countryCodeSource =>
89
      NULL
90
     private $preferredDomesticCarrierCode =>
91
      NULL
92
    }
93
 
94
Now let us validate whether the number is valid:
95
 
96
```php
97
$isValid = $phoneUtil->isValidNumber($swissNumberProto);
98
var_dump($isValid); // true
99
```
100
 
101
There are a few formats supported by the formatting method, as illustrated below:
102
 
103
```php
104
// Produces "+41446681800"
105
echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::E164);
106
 
107
// Produces "044 668 18 00"
108
echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::NATIONAL);
109
 
110
// Produces "+41 44 668 18 00"
111
echo $phoneUtil->format($swissNumberProto, \libphonenumber\PhoneNumberFormat::INTERNATIONAL);
112
```
113
 
114
You could also choose to format the number in the way it is dialled from another country:
115
 
116
```php
117
// Produces "011 41 44 668 1800", the number when it is dialled in the United States.
118
echo $phoneUtil->formatOutOfCountryCallingNumber($swissNumberProto, "US");
119
 
120
// Produces "00 41 44 668 18 00", the number when it is dialled in Great Britain.
121
echo $phoneUtil->formatOutOfCountryCallingNumber($swissNumberProto, "GB");
122
```
123
 
124
## FAQ
125
 
126
#### Problems with Invalid Numbers?
127
 
128
This library uses phone number metadata from Google's [libphonenumber](https://github.com/google/libphonenumber). If this library is working as intended, it should provide the same result as the Java version of Google's project.
129
 
130
If you believe that a phone number is returning an incorrect result, first test it with [libphonenumber](https://github.com/google/libphonenumber) via their [Online Demo](https://libphonenumber.appspot.com/). If that returns the same result as this project, and you feel it is in error, raise it as an Issue with the libphonenumber project.
131
 
132
If Google's [Online Demo](https://libphonenumber.appspot.com/) gives a different result to the [libphonenumber-for-php demo](http://giggsey.com/libphonenumber/), then please raise an Issue on the [giggsey/libphonenumber-for-php](https://github.com/giggsey/libphonenumber-for-php) project.
133
 
134
If [giggsey/libphonenumber-for-php](https://github.com/giggsey/libphonenumber-for-php) differs from this library, please raise an issue here instead.
135
 
136
## Generating data
137
 
138
Generating the data is not normally needed, as this repository will generally always have the up to data metadata.
139
 
140
If you do need to generate the data, the commands are provided by [Phing](https://www.phing.info). Ensure you have all the dev composer dependencies installed, then run
141
 
142
```bash
143
$ vendor/bin/phing compile
144
```
145
 
146
This compile task clones the [libphonenumber](https://github.com/google/libphonenumber) project at the version specified in [METADATA-VERSION.php](METADATA-VERSION.php).