Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
/bin/
/coverage/
composer.lock
35 changes: 35 additions & 0 deletions base58.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

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 name StaticBase58 without namespace.


use StephenHill\StaticBase58;

if (false === function_exists('base58_encode'))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need in false ===, it looks weird. I think

if (!function_exists('base58_encode'))

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))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. Just use

if (!class_exists('Base58', true))

{
class Base58
{
public static function encode($string)
{
return StaticBase58::encode($string);
}

public static function decode($string)
{
return StaticBase58::decode($string);
}
}
}
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
"psr-4": {
"StephenHill\\": "src/",
"StephenHill\\Benchmarks\\": "benchmarks/"
}
},
"files": [
"base58.php"
]
}
}
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@

<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src</directory>
<directory suffix=".php">.</directory>
<exclude>
<directory suffix=".php">vendor</directory>
<directory suffix=".php">tests</directory>
</exclude>
</whitelist>
</filter>
Expand Down
25 changes: 25 additions & 0 deletions src/Base58.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,29 @@ public function decode($base58)
{
return $this->service->decode($base58);
}

/**
* Return the current service provider.
*
* @since v1.2.0
* @return ServiceInterface Return the current service.
*/
public function getService()
{
return $this->service;
}

/**
* Replaces the current service provider with a new one.
*
* @param ServiceInterface $service The new service instance.
* @since v1.2.0
* @return self
*/
public function replaceService(ServiceInterface $service)
{
$this->service = $service;

return $this;
}
}
33 changes: 33 additions & 0 deletions src/StaticBase58.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace StephenHill;

class StaticBase58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is singleton, then you should add private function __construct() {}.

{
protected static $instance;

public static function encode($string)
{
return self::getInstance()->encode($string);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return static::... is better if somebody decide to override getInstance() to use another Base58 implementation class`

}

public static function decode($string)
{
return self::getInstance()->decode($string);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return static::... is better if somebody decide to override getInstance() to use another Base58 implementation class`

}

public static function getInstance()
{
if (null === static::$instance)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!static::$instance) is enough

{
static::$instance = new Base58();
}

return static::$instance;
}

public static function setInstance(Base58 $base58)
{
static::$instance = $base58;
}
}
13 changes: 13 additions & 0 deletions tests/Base58Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,17 @@ public function testInvalidBase58()
$base58 = new Base58();
$base58->decode("This isn't valid base58");
}

public function testGetAndReplaceService()
{
$bcMath = new BCMathService();
$gmp = new GMPService();
$base58 = new Base58();

$base58->replaceService($bcMath);
$this->assertSame($bcMath, $base58->getService());

$base58->replaceService($gmp);
$this->assertSame($gmp, $base58->getService());
}
}
27 changes: 27 additions & 0 deletions tests/StaticTest.php
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'));
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
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");