/**
 * Checks whether a passed phone number has valid sintax
 *
 * @param v_phone The phone to check format
 * @return %TRUE if it's well formated
 **/
DROP PROCEDURE IF EXISTS pbx.phoneIsValid;
DELIMITER $$
CREATE PROCEDURE pbx.phoneIsValid (vPhone VARCHAR(255))
BEGIN
	DECLARE vIsValid BOOLEAN;

    SET vIsValid =  vPhone IS NULL
		OR (vPhone REGEXP '^[0-9]+$'
		AND vPhone NOT REGEXP '^0+$'
		AND vPhone NOT REGEXP '^0034');

	IF NOT vIsValid
    THEN
		SIGNAL SQLSTATE '45000'
			SET MESSAGE_TEXT = 'PHONE_INVALID_FORMAT';
    END IF;
END$$
DELIMITER ;
