Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed May 27, 2018
0 parents commit 46ee8ea
Show file tree
Hide file tree
Showing 9 changed files with 840 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "rennokki/schedule",
"description": "Setting up schedule to Eloquent models with the ability of checking schedule based on time.",
"keywords": [
"schedule",
"scheduler",
"timetable",
"spatie",
"opening-hours",
"schedule",
"opening",
"hours"
],
"homepage": "https://github.com/rennokki/schedule",
"license": "MIT",
"authors": [
{
"name": "Alex Renoki",
"email": "rennokki@gmail.com",
"homepage": "https://twitter.com/rennokki",
"role": "Developer"
}
],
"require": {
"nesbot/carbon": "^1.29.0"
},
"autoload": {
"psr-4": {
"Rennokki\\Schedule\\": "src"
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev"
}
16 changes: 16 additions & 0 deletions config/schedule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [

/**
* The model the app uses.
* Feel free to change it however you like, but do not
* forget to extend the original model.
*
* Or don't extend it from the original model.
* I'm just some text. I can't stop you.
*/

'model' => \Rennokki\Schedule\Models\ScheduleModel::class,

];
40 changes: 40 additions & 0 deletions database/migrations/2018_05_19_135648_schedules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class Schedules extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('schedules', function(Blueprint $table) {

$table->increments('id');

$table->integer('model_id');
$table->string('model_type');

$table->text('schedule')->nullable();
$table->text('exclusions')->nullable();

$table->timestamps();

});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('schedules');
}
}
101 changes: 101 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Schedule
Schedule is a package that brings up features like "timetable" for a certain eloquent model.

To get started, let's use an example.

```php
// Let's get an user.
$user = User::find(1);

// We can set up a schedule for this user, Monday to Friday, between 8-12 and 13-18.
$user->setSchedule([
'monday' => ['08:00-12:00', '13:00-18:00'],
'tuesday' => ['08:00-12:00', '13:00-18:00'],
'wednesday' => ['08:00-12:00', '13:00-18:00'],
'thursday' => ['08:00-12:00', '13:00-18:00'],
'friday' => ['08:00-12:00', '13:00-18:00'],
]);

// Let's also set exclusions, such as the user's birthday: 1st March, each year, the user is working from 8 to 12 only.
// Also, let's mark for the user the first and the second day of Christmas as having no schedule.
$user->setExclusions([
'03-01' => ['08:00-12:00'],
'12-25' => [],
'12-26' => [],
'2018-05-01' => [], // We can also set a specific day, just one.
]);

// We can check against day name, month and day, even year, month and day or Carbon instance.
$user->isAvailableOn('monday'); // true
$user->isAvailableOn('05-28'); // true; The year is the current year (2018); This is monday.
$user->isAvailableOn('2018-05-28'); // true
$user->isAvailableOn(Carbon::create(2018, 5, 28, 0, 0, 0)); // true

// We can do the opposite.
$user->isUnavailableOn('monday'); // false
$user->isUnavailableOn('05-28'); // false
$user->isUnavailableOn('2018-05-28'); // false
$user->isUnavailableOn(Carbon::create(2018, 5, 28, 0, 0, 0)); // false

// Check against exclusions is also provided.
$user->isUnavailableOn('12-25'); // true
$user->isUnavailableOn('03-01'); // false

// We can also check against time. For the sake of this example's length, this works too with the exclusions.
// The first parameter is the same as the first parameter in the isAvailableOn() method.
$user->isAvailableOnAt('monday', '09:00'); // true
$user->isUnavailableOnAt('monday', '09:00'); // false

// We can get the amount of working hours on a certain day.
// The first parameter is the same as the first parameter in the isAvailableOn() method.
$user->getHoursOn('03-01'); // 4
$user->getHoursOn('12-26'); // 0
$user->getHoursOn('05-28'); // 9
$user->getHoursOn('2018-05-28'); // 9

// Alternatively, you can have getMinutesOn() method.
$user->getMinutesOn('03-01'); // 240

// You can delete a schedule.
$user->deleteSchedule();

// You can also check if the user has a schedule set.
$user->hasSchedule(); // false, because we deleted it
```

# Installation
Install the package via Composer CLI:
```bash
$ composer require rennokki/schedule
```

For versions of Laravel that doesn't support package discovery, you should add this to your `config/app.php` file, in the `providers` array:

```php
\Rennokki\Schedule\ScheduleServiceProvider::class,
```

Publish the migration file and the config file.
```bash
$ php artisan vendor:publish
```

Migrate the database.
```bash
$ php artisan migrate
```

Add the trait to your model.
```php
use Rennokki\Schedule\Traits\HasSchedule;

class User extends Model {
use HasSchedule;
...
}
```

# About the package
This package is inspired from [Spatie's Opening Hours](https://github.com/spatie/opening-hours) package, which uses a schedule but only statically, rather than binding it to a model.

Feel free to address the issues to the Issues Board if there are errors. Also, you can fork/pull request it and you can improve it anytime you want. Let's keep the open source alive!
22 changes: 22 additions & 0 deletions src/Models/ScheduleModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rennokki\Schedule\Models;

use Illuminate\Database\Eloquent\Model;

class ScheduleModel extends Model
{
protected $table = 'schedules';
protected $fillable = [
'model_id', 'model_type', 'schedule', 'exclusions',
];
protected $casts = [
'schedule' => 'array',
'exclusions' => 'array',
];

public function model()
{
return $this->morphTo();
}
}
34 changes: 34 additions & 0 deletions src/ScheduleServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Rennokki\Schedule;

use Illuminate\Support\ServiceProvider;

class ScheduleServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/config/schedule.php' => config_path('schedule.php'),
], 'config');

$this->publishes([
__DIR__.'/database/migrations/2018_05_19_135648_schedules.php' => database_path('migrations/2018_05_19_135648_schedules.php'),
], 'migration');
}

/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
//
}
}
Loading

0 comments on commit 46ee8ea

Please sign in to comment.