Skip to content

Commit

Permalink
Add default alphabet and add Exception class
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Freytag committed Nov 19, 2014
1 parent da94290 commit e050c50
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Bijective/BijectiveTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Bijective;

use Bijective\Exception\BijectiveException;

/**
* @author Brian Freytag <brian@idltg.in>
*/
Expand All @@ -13,7 +15,7 @@ class BijectiveTranslator implements BijectiveTranslatorInterface
/**
* @param $alphabet
*/
public function __construct($alphabet)
public function __construct($alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$this->alphabet = $alphabet;
}
Expand All @@ -23,13 +25,17 @@ public function __construct($alphabet)
*
* @param integer $int
*
* @throws \InvalidArgumentException
* @throws BijectiveException
* @return string
*/
public function encode($int)
{
if (is_int($int)) {
$int = (string) $int;
}

if (!ctype_digit($int)) {
throw new \InvalidArgumentException('You can only encode a number');
throw new BijectiveException('You can only encode a number');
}

if ($int == 0) {
Expand All @@ -53,13 +59,13 @@ public function encode($int)
*
* @param $str
*
* @throws \InvalidArgumentException
* @throws BijectiveException
* @return int
*/
public function decode($str)
{
if (!is_string($str)) {
throw new \InvalidArgumentException('You can only decode a string');
throw new BijectiveException('You can only decode a string');
}

$int = 0;
Expand Down
12 changes: 12 additions & 0 deletions src/Bijective/Exception/BijectiveException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Bijective\Exception;

/**
* The default exception for the Bijective library
*
* @author Brian Freytag <brian@quizzle.com>
*/
class BijectiveException extends \Exception
{
}

0 comments on commit e050c50

Please sign in to comment.