Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Commit

Permalink
Integrate travis #1 (#7)
Browse files Browse the repository at this point in the history
* Integrate travis

* Integrate travis

Integrate travis #1

* Update readme.md

Update readme.md by adding build status

* Apply fixes from StyleCI (#6)
  • Loading branch information
erashdan authored Jan 17, 2019
1 parent cc9bc8d commit 65ec089
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 25 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php

install:
composer install --no-interaction

php:
- 7.1
- 7.2

cache:
directories:
- vendor

script:
- php vendor/bin/phpunit
2 changes: 1 addition & 1 deletion config/hashid.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
*/

'length' => env('HASHID_LENGTH', 6),
]
],
];
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Eloquent Hashid
# Eloquent Hashid [![Build Status](https://travis-ci.org/erashdan/hashid.svg?branch=master)](https://travis-ci.org/erashdan/hashid)

* [Installation](#installation)
* [Usage](#usage)
Expand Down
17 changes: 9 additions & 8 deletions src/HashData.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class HashData
{
/**
* Encode ID
* Encode ID.
*
* @param $key
* @param null $baseKey
Expand All @@ -21,9 +21,8 @@ public static function Encode($key, $baseKey)
return $hashids->encode($key);
}


/**
* Decode ID
* Decode ID.
*
* @param $key
* @param null $baseKey
Expand All @@ -32,29 +31,31 @@ public static function Encode($key, $baseKey)
*/
public static function Decode($key, $baseKey = null)
{
if (!$baseKey)
if (! $baseKey) {
$baseKey = self::getKey();
}

$hashids = new Hashids($baseKey, self::getLength());

return $hashids->decode($key);
}

/**
* Get length
* Get length.
*
* @throws \Exception
* @return integer
* @return int
*/
private static function getLength(): int
{
if (
(config('hashid.hash_data.length') == null) ||
(config('hashid.hash_data.length') == '') ||
!is_int(config('hashid.hash_data.length'))
! is_int(config('hashid.hash_data.length'))
) {
throw new \Exception('Unable to define hashing length');
}

return config('hashid.hash_data.length');
}
}
}
1 change: 0 additions & 1 deletion src/HashidServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ public function boot()
*/
public function register()
{

}
}
14 changes: 7 additions & 7 deletions src/Traits/Hashid.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@
trait Hashid
{
/**
* Encode hashed id
* Encode hashed id.
*
* @return mixed
* @throws \Exception
*/
public function getHashedIdAttribute()
{
$primary = $this->primaryKey;

return HashData::Encode($this->$primary, self::setHashKey(self::class), 20);
}

/**
* Decode hash id
* Decode hash id.
*
* @param $id
* @return |null
Expand All @@ -33,8 +34,6 @@ public static function DecodeId($id)
if (count($decode) > 0) {
return $decode[0];
}

return null;
}

public function scopeFindOrFailHashed($query, $id)
Expand All @@ -49,7 +48,7 @@ public function scopeFindOrFailHashed($query, $id)
public function scopeFindHashed($query, $id)
{
if (empty($decoded = self::DecodeId($id))) {
return null;
return;
}

return $query->find(self::DecodeId($id));
Expand All @@ -61,6 +60,7 @@ public function scopeWhereInHashed($query, $values)
foreach ($values as $value) {
$hash[] = self::DecodeId($value);
}

return $query->whereIn($this->primaryKey, $hash);
}

Expand All @@ -73,6 +73,6 @@ private static function setHashKey($class)
throw new \Exception('Unable to define hashing key');
}

return config('hashid.hash_data.key') . $class;
return config('hashid.hash_data.key').$class;
}
}
}
3 changes: 0 additions & 3 deletions tests/HashingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

class HashingTest extends TestCase
{

/** @test * */
public function hashid_can_hash_primary_key()
{
$this->assertEquals('N5zQE4', $this->testModel->hashed_id);
}


/** @test * */
public function get_correct_element_by_hashed_id()
{
Expand Down Expand Up @@ -61,6 +59,5 @@ public function throw_exception_if_hash_length_not_integer()
config(['hashid.hash_data.length' => 'STRING']);
$this->expectExceptionMessage('Unable to define hashing length');
$this->assertNotNull($this->testModel->hashed_id);

}
}
3 changes: 0 additions & 3 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public function setUp()
config(['hashid.hash_data.length' => 6]);
}


/**
* Set up the environment.
*
Expand All @@ -36,7 +35,6 @@ protected function getEnvironmentSetUp($app)
'database' => ':memory:',
'prefix' => '',
]);

}

/**
Expand All @@ -55,5 +53,4 @@ protected function setUpDatabase($app)

TestModel::create(['another_key' => 2]);
}

}
2 changes: 1 addition & 1 deletion tests/TestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ class TestModel extends Model
protected $fillable = ['another_key'];

public $timestamps = false;
}
}

0 comments on commit 65ec089

Please sign in to comment.