Skip to content

Commit

Permalink
Merge branch '2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-gribanov committed Jul 25, 2017
2 parents b7fe7ef + ca3ea71 commit 4bccf11
Show file tree
Hide file tree
Showing 36 changed files with 569 additions and 1,132 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ matrix:
- php: 7.0
- php: 5.6
- php: 5.5
- php: 5.4
- php: hhvm
sudo: required
dist: trusty
Expand Down
106 changes: 101 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,116 @@
[![StyleCI](https://styleci.io/repos/61719557/shield?branch=master)](https://styleci.io/repos/61719557)
[![License](https://img.shields.io/packagist/l/anime-db/smart-sleep.svg?maxAge=3600)](https://github.com/anime-db/smart-sleep)

# SmartSleep util
SmartSleep
==========

## Installation
Installation
------------

Pretty simple with [Composer](http://packagist.org), run:

```sh
composer require anime-db/smart-sleep
```

## How-to
How-to
------

See [functional tests](https://github.com/anime-db/smart-sleep/tree/master/tests/functional) for more details.
First build schedule

## License
```php
use AnimeDb\SmartSleep\Rule\EverydayRule;
use AnimeDb\SmartSleep\Schedule;

$schedule = new Schedule([
new EverydayRule(0, 3, 260), // [00:00, 03:00)
new EverydayRule(3, 9, 900), // [03:00, 09:00)
new EverydayRule(9, 19, 160), // [09:00, 19:00)
new EverydayRule(19, 23, 70), // [19:00, 23:00)
new EverydayRule(23, 24, 60), // [23:00, 24:00)
]);
```

Configure SmartSleep

```php
use AnimeDb\SmartSleep\SmartSleep;

$smart = new SmartSleep($schedule);
```

And now we can sleep

```php
$seconds = $smart->sleepForSeconds(new \DateTime());

sleep($seconds);
```

Rules
-----

### SpecificDayRule

The rule corresponds to specific day in the specified time interval.
Can be used for public holidays.

```php
$rule = new SpecificDayRule(new \DateTime('2017-01-01'), $start_hour, $end_hour, $max_sleep_seconds)
```

### EverydayRule

The rule corresponds to any day in the specified time interval.

```php
$rule = new EverydayRule($start_hour, $end_hour, $max_sleep_seconds)
```

### HolidayRule

The rule corresponds to the holiday at the specified time interval.

```php
$rule = new HolidayRule($start_hour, $end_hour, $max_sleep_seconds)
```

### WeekdayRule

The rule corresponds to the weekday at the specified time interval.

```php
$rule = new WeekdayRule($start_hour, $end_hour, $max_sleep_seconds)
```

### OnceDayRule

The rule always corresponds to the specified time.
Returns the seconds in the next day.

```php
$rule = new OnceDayRule()
```

### OnceWeekRule

The rule always corresponds to the specified time.
Returns the seconds in the next week.

```php
$rule = new OnceWeekRule()
```

### OnceMonthRule

The rule always corresponds to the specified time.
Returns the seconds in the next month.

```php
$rule = new OnceMonthRule()
```

License
-------

This bundle is under the [MIT license](http://opensource.org/licenses/MIT). See the complete license in the file: LICENSE
19 changes: 17 additions & 2 deletions src/Rule/EverydayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,30 @@

namespace AnimeDb\SmartSleep\Rule;

class EverydayRule extends RandMaxSecondsRuleBase
class EverydayRule implements HourIntervalRule
{
use HourlyIntervalRuleTrait;
use RandSecondsRuleTrait;

/**
* @param int $start
* @param int $end
* @param int $seconds
*/
public function __construct($start, $end, $seconds)
{
$this->setStart($start);
$this->setEnd($end);
$this->setSeconds($seconds);
}

/**
* @param \DateTime $time
*
* @return bool
*/
public function isMatched(\DateTime $time)
{
return $this->getStart() <= $time->format('G') && $this->getEnd() > $time->format('G');
return $this->start() <= $time->format('G') && $this->end() > $time->format('G');
}
}
25 changes: 21 additions & 4 deletions src/Rule/HolidayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,34 @@

namespace AnimeDb\SmartSleep\Rule;

class HolidayRule extends RandMaxSecondsRuleBase
class HolidayRule implements HourIntervalRule
{
use HourlyIntervalRuleTrait;
use RandSecondsRuleTrait;

/**
* @param int $start
* @param int $end
* @param int $seconds
*/
public function __construct($start, $end, $seconds)
{
$this->setStart($start);
$this->setEnd($end);
$this->setSeconds($seconds);
}

/**
* @param \DateTime $time
*
* @return bool
*/
public function isMatched(\DateTime $time)
{
return $time->format('N') > 5 &&
$this->getStart() <= $time->format('G') &&
$this->getEnd() > $time->format('G');
return
$time->format('N') > 5 &&
$this->start() <= $time->format('G') &&
$this->end() > $time->format('G')
;
}
}
22 changes: 22 additions & 0 deletions src/Rule/HourIntervalRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* AnimeDb package.
*
* @author Peter Gribanov <info@peter-gribanov.ru>
* @copyright Copyright (c) 2011, Peter Gribanov
*/

namespace AnimeDb\SmartSleep\Rule;

interface HourIntervalRule extends Rule
{
/**
* @return int
*/
public function start();

/**
* @return int
*/
public function end();
}
54 changes: 54 additions & 0 deletions src/Rule/HourlyIntervalRuleTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* AnimeDb package.
*
* @author Peter Gribanov <info@peter-gribanov.ru>
* @copyright Copyright (c) 2011, Peter Gribanov
*/

namespace AnimeDb\SmartSleep\Rule;

trait HourlyIntervalRuleTrait
{
/**
* @var int
*/
private $start = -1;

/**
* @var int
*/
private $end = -1;

/**
* @param int $start
*/
protected function setStart($start)
{
$this->start = $start;
}

/**
* @param int $end
*/
protected function setEnd($end)
{
$this->end = $end;
}

/**
* @return int
*/
public function start()
{
return $this->start;
}

/**
* @return int
*/
public function end()
{
return $this->end;
}
}
8 changes: 4 additions & 4 deletions src/Rule/OnceDayRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

namespace AnimeDb\SmartSleep\Rule;

class OnceDayRule extends RuleBase
class OnceDayRule implements Rule
{
/**
* @var \DateTime
*/
protected $time;
private $time;

public function __construct()
{
Expand All @@ -35,10 +35,10 @@ public function isMatched(\DateTime $time)
/**
* @return int
*/
public function getSeconds()
public function seconds()
{
$offset_time = clone $this->time;
$offset_time->modify('+1 day 00:00:00');
$offset_time->modify('+1 day')->setTime(0, 0, 0);
$offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); // offset to next day

return $offset + rand(0, 86400); // 86400 is a 1 day
Expand Down
14 changes: 6 additions & 8 deletions src/Rule/OnceMonthRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

namespace AnimeDb\SmartSleep\Rule;

class OnceMonthRule extends RuleBase
class OnceMonthRule implements Rule
{
/**
* @var \DateTime
*/
protected $time;
private $time;

public function __construct()
{
Expand All @@ -35,19 +35,17 @@ public function isMatched(\DateTime $time)
/**
* @return int
*/
public function getSeconds()
public function seconds()
{
// interval duration [next month, next month +1)
$offset_time = clone $this->time;
$offset_time->modify('+1 month 00:00:00')->modify('first day of this month');
$offset_time->modify('first day of this month')->modify('+1 month')->setTime(0, 0, 0);

$limit_time = clone $this->time;
$limit_time->modify('+2 month 00:00:00')->modify('first day of this month');
$limit_time = clone $offset_time;
$limit_time->modify('+1 month');
$limit = $limit_time->getTimestamp() - $offset_time->getTimestamp();

// offset to next month
$offset_time = clone $this->time;
$offset_time->modify('+1 month 00:00:00')->modify('first day of this month');
$offset = $offset_time->getTimestamp() - $this->time->getTimestamp();

return $offset + rand(0, $limit);
Expand Down
8 changes: 4 additions & 4 deletions src/Rule/OnceWeekRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

namespace AnimeDb\SmartSleep\Rule;

class OnceWeekRule extends RuleBase
class OnceWeekRule implements Rule
{
/**
* @var \DateTime
*/
protected $time;
private $time;

public function __construct()
{
Expand All @@ -35,10 +35,10 @@ public function isMatched(\DateTime $time)
/**
* @return int
*/
public function getSeconds()
public function seconds()
{
$offset_time = clone $this->time;
$offset_time->modify('+1 week 00:00:00');
$offset_time->modify('+1 week')->setTime(0, 0, 0);
$offset = $offset_time->getTimestamp() - $this->time->getTimestamp(); // offset to next week

return $offset + rand(0, 604800); // 604800 is a 1 week
Expand Down

0 comments on commit 4bccf11

Please sign in to comment.