-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Static Class and Helper Functions #15
base: master
Are you sure you want to change the base?
Changes from all commits
8377840
7d8955a
4812393
e68ce19
e360702
259f317
d999156
e62c30c
9cf300f
7bb705f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/vendor/ | ||
/bin/ | ||
/coverage/ | ||
composer.lock |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
use StephenHill\StaticBase58; | ||
|
||
if (false === function_exists('base58_encode')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need in
Is 100% enough and much more readable :) |
||
{ | ||
function base58_encode($string) | ||
{ | ||
return StaticBase58::encode($string); | ||
} | ||
} | ||
|
||
if (false === function_exists('base58_decode')) | ||
{ | ||
function base58_decode($string) | ||
{ | ||
return StaticBase58::decode($string); | ||
} | ||
} | ||
|
||
if (false === class_exists('Base58', true)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. Just use
|
||
{ | ||
class Base58 | ||
{ | ||
public static function encode($string) | ||
{ | ||
return StaticBase58::encode($string); | ||
} | ||
|
||
public static function decode($string) | ||
{ | ||
return StaticBase58::decode($string); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace StephenHill; | ||
|
||
class StaticBase58 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is singleton, then you should add |
||
{ | ||
protected static $instance; | ||
|
||
public static function encode($string) | ||
{ | ||
return self::getInstance()->encode($string); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
public static function decode($string) | ||
{ | ||
return self::getInstance()->decode($string); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
public static function getInstance() | ||
{ | ||
if (null === static::$instance) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
static::$instance = new Base58(); | ||
} | ||
|
||
return static::$instance; | ||
} | ||
|
||
public static function setInstance(Base58 $base58) | ||
{ | ||
static::$instance = $base58; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
use StephenHill\StaticBase58; | ||
|
||
class StaticTests extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testStaticClass() | ||
{ | ||
$this->assertSame('JxF12TrwUP45BMd', StaticBase58::encode('Hello World')); | ||
$this->assertSame('Hello World', StaticBase58::decode('JxF12TrwUP45BMd')); | ||
} | ||
|
||
public function testNamespacelessClass() | ||
{ | ||
$this->assertTrue(class_exists('Base58', true)); | ||
$this->assertSame('JxF12TrwUP45BMd', Base58::encode('Hello World')); | ||
$this->assertSame('Hello World', Base58::decode('JxF12TrwUP45BMd')); | ||
} | ||
|
||
public function testFunctions() | ||
{ | ||
$this->assertTrue(function_exists('base58_encode')); | ||
$this->assertTrue(function_exists('base58_decode')); | ||
$this->assertSame('JxF12TrwUP45BMd', base58_encode('Hello World')); | ||
$this->assertSame('Hello World', base58_decode('JxF12TrwUP45BMd')); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?php | ||
|
||
require_once 'vendor/autoload.php'; | ||
require_once 'base58.php'; | ||
date_default_timezone_set("UTC"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess you can add
use StephenHill\StaticBase58
to access class nameStaticBase58
without namespace.