public function testCreateCreditCard_Success()
 {
     $expected = new CreditCard();
     $expected->setCreditCardBrand(CreditCardBrandEnum::MASTERCARD);
     $expected->setCreditCardNumber("5555444433332222");
     $expected->setExpMonth(12);
     $expected->setExpYear(30);
     $expected->setHolderName("gateway");
     $expected->setSecurityCode("999");
     $creditCard = CreditCardHelper::createCreditCard(" 5555 4444 3333 2222 ", " gateway ", " 12/30 ", " 999 ");
     $this->assertEquals($expected, $creditCard);
     $expected->setExpYear(2030);
     $creditCard = CreditCardHelper::createCreditCard(" 5555 4444 3333 2222 ", " gateway ", " 12/2030 ", " 999 ");
     $this->assertEquals($expected, $creditCard);
 }
 /**
  * @param $number
  * @param $name
  * @param $expiry
  * @param $cvc
  * @return CreditCard
  * @throws CreditCardError
  */
 public static function createCreditCard($number, $name, $expiry, $cvc)
 {
     if (empty($number)) {
         throw new CreditCardError("Invalid credit card number.", "number");
     }
     if (empty($name)) {
         throw new CreditCardError("Invalid credit card holder name.", "holderName");
     }
     if (empty($cvc)) {
         throw new CreditCardError("Invalid credit card security code.", "securityCode");
     }
     // Verifica se foi enviado uma barra
     if (empty($expiry) || stristr($expiry, '/') === false) {
         throw new CreditCardError("Invalid credit card expiration date.", "expirationDate");
     }
     // Separa mes e ano da data de validade do cartão
     $expiryParts = explode('/', trim($expiry));
     $expMonth = @trim($expiryParts[0]);
     $expYear = @trim($expiryParts[1]);
     // Verifica se o mês é válido
     if ($expMonth < 1 || $expMonth > 12) {
         throw new CreditCardError("Invalid credit card expiration month.", "expirationMonth");
     }
     // Verifica se o ano é válido
     if (!in_array(strlen($expYear), array(2, 4))) {
         throw new CreditCardError("Invalid credit card expiration year.", "expirationYear");
     }
     // Extrai somente números
     $number = str_replace(array('-', '+'), '', filter_var($number, FILTER_SANITIZE_NUMBER_INT));
     $cvc = str_replace(array('-', '+'), '', filter_var($cvc, FILTER_SANITIZE_NUMBER_INT));
     // Valida número do cartão
     if (strlen($number) < 10 || strlen($number) > 24) {
         throw new CreditCardError("Invalid credit card number.", "number");
     }
     // Obtém a bandeira do cartão
     $creditCardBrand = self::getBrandByNumber($number);
     // Valida a bandeira
     if ($creditCardBrand == null) {
         throw new CreditCardError("Invalid credit card brand.", "brand");
     }
     // Sanitiza o nome
     $name = filter_var(trim($name), FILTER_SANITIZE_STRING);
     // Cria um objeto de cartão de crédito
     $creditCard = new CreditCard();
     $creditCard->setCreditCardBrand($creditCardBrand);
     $creditCard->setHolderName($name);
     $creditCard->setCreditCardNumber($number);
     $creditCard->setExpMonth($expMonth);
     $creditCard->setExpYear($expYear);
     $creditCard->setSecurityCode($cvc);
     // Devolve o cartão
     return $creditCard;
 }