Skip to content

Commit

Permalink
Merge pull request #1 from marioquartz/dev
Browse files Browse the repository at this point in the history
A better Readme and has a good score in PhpInsigths
  • Loading branch information
marioquartz committed Jun 26, 2021
2 parents c64ad86 + 566fe22 commit 64abb44
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 122 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.idea/
vendor/
composer.lock
phpinsights.php
58 changes: 34 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,48 @@ This library has one goal: **organizing list of events in sessions**. You can ad

The origin of this library is sort the actions when I work, each project is a different type of event. With this library now I can see how much time work in each project.

#Using the library
# Using the library

You start initialising the _Maker_ class and adding the events:

use Marioquartz\MakingSessions\Maker;
use Marioquartz\MakingSessions\Event;

$maker=new Maker();
foreach ($json as $item) {
$event=new Marioquartz\MakingSessions\Event();
$event
->setStart($start) //start can be a timestamp or a DateInmutable
->setDuration(60)
->setType("example");
$maker->add($event);
}
```php
use Marioquartz\MakingSessions\Maker;
use Marioquartz\MakingSessions\Event;

$maker=new Maker();
foreach ($list as $item) {
$event=new Event();
$event
->setStart($start) //start can be a timestamp or a DateInmutable
->setDuration(60) // you can set the duration or use setEnd()
->setType("example");
$maker->add($event);
}
```

Generate the list of sessions with their events is easy:

$sessions=$maker->getSessions();
```php
$sessions=$maker->getSessions();
```

Sessions now have a list of sessions, that can be iterated:
**But, what is a "_session_"?** An aggregate of events that are "near". In the context of this library: the difference between the end of an event and the start of next event is less than the variable "timeMerge". By default is 300 seconds (5 minutes). You can configure this behaviour with _$maker->setTimeMerge($seconds)_.

foreach($sessions as $sessiong) {
echo $session->getStart(); // Start of the session returned as timestamp
echo $session->getEnd(); //End as timestamp
echo $session->getDuration() // Duration in seconds
echo $session->getType(); // Type of the events inside
foreach ($session->getEvents() as $event) {
//events have the same functions: getStart(), getEnd(), getDuration() and getType()
}
_$sessions_ now have a list of sessions, that can be iterated:

```php
foreach($sessions as $session) {
echo $session->getStart(); // Start of the session returned as timestamp
echo $session->getEnd(); //End as timestamp
echo $session->getDuration() // Duration in seconds
echo $session->getType(); // Type of the events inside
foreach ($session->getEvents() as $event) {
//events have the same functions: getStart(), getEnd(), getDuration() and getType()
}
}
```

# Helping

This is my first real library. I don't have much experience, so of course this library have a big room for make better code or usefulness.
If you are a kind person, and you think have an idea or if you have a bug im all ears.
If you are a kind person, and you think have an idea or if you have a bug, im all ears.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"php":">=7.4"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.0"
"squizlabs/php_codesniffer": "^3.0",
"nunomaduro/phpinsights": "^2.0"
},
"scripts": {
"check-style": "phpcs src",
Expand Down
2 changes: 1 addition & 1 deletion src/Event.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

declare(strict_types=1);

namespace Marioquartz\MakingSessions;

class Event extends TimeBucket
{

}
3 changes: 2 additions & 1 deletion src/EventList.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

declare(strict_types=1);

namespace Marioquartz\MakingSessions;

class EventList extends ItemList
{
public function order(): ItemList
{
return self::orderList($this, "Event");
return self::orderList($this, 'Event');
}
}
51 changes: 24 additions & 27 deletions src/ItemList.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
<?php

declare(strict_types=1);

namespace Marioquartz\MakingSessions;

class ItemList implements \Countable, \Iterator
{
protected array $list=array();
private int $cursor=0;
/**
* @var array<TimeBucket> $list
*/
protected array $list = [];
private int $cursor = 0;

public function add(TimeBucket $item)
public function add(TimeBucket $item): void
{
$this->list[]=$item;
$this->cursor=($this->count()-1);
$this->list[] = $item;
$this->cursor = $this->count() - 1;
}

public function count():int
public function count(): int
{
return count($this->list);
}

public function current():TimeBucket
public function current(): TimeBucket
{
return $this->list[$this->cursor];
}

public function next()
public function next(): void
{
++$this->cursor;
}

public function key()
public function key(): int
{
return $this->cursor;
}
Expand All @@ -39,41 +43,34 @@ public function valid(): bool
return isset($this->list[$this->cursor]);
}

public function rewind()
public function rewind(): void
{
$this->cursor=0;
$this->cursor = 0;
}

public function last():TimeBucket
public function last(): TimeBucket
{
$n=$this->count();
$n = $this->count();
--$n;
return $this->list[$n];
}

/**
* @param ItemList $itemList
* @param string $itemType
* @param bool $inverse
*
* @return ItemList
*/
public static function orderList(ItemList $itemList, string $itemType, bool $inverse = false): ItemList
public static function orderList(ItemList $list, string $type, bool $inverse = false): ItemList
{
$position = array();
/** @var Event[] $newRow */
$newRow = array();
foreach (iterator_to_array($itemList) as $key => $item) {
$position = [];
/** @var array<Event> $newRow */
$newRow = [];
foreach (iterator_to_array($list) as $key => $item) {
/** @var TimeBucket $item */
$position[$key] = $item->getStart();
$position[$key] = $item->getStart();
$newRow[$key] = $item;
}
if ($inverse) {
arsort($position);
} else {
asort($position);
}
$nameList="\Marioquartz\MakingSessions\\".$itemType."List";
$nameList = "\Marioquartz\MakingSessions\\" . $type . 'List';
/** @var ItemList $returnList */
$returnList = new $nameList();
foreach ($position as $key => $pos) {
Expand Down
54 changes: 23 additions & 31 deletions src/Maker.php
Original file line number Diff line number Diff line change
@@ -1,75 +1,67 @@
<?php

declare(strict_types=1);

namespace Marioquartz\MakingSessions;

class Maker
{
private ItemList $input;
private ItemList $ordered;
private int $timeMerge=300;
private int $timeMerge = 300;

public function __construct()
{
$this->input=new EventList();
$this->ordered=new EventList();
$this->input = new EventList();
$this->ordered = new EventList();
}

public function add(Event $event)
public function add(Event $event): void
{
$this->input->add($event);
}

/**
* Establish the maximum time for two events being in the same Session
* @param int $timeMerge
*/
public function setTimeMerge(int $timeMerge)
public function setTimeMerge(int $timeMerge): void
{
$this->timeMerge=$timeMerge;
$this->timeMerge = $timeMerge;
}

public function getSessions(): SessionList
{
$this->ordered=$this->input->order();
$sessions=new SessionList();
$this->ordered = $this->input->order();
$sessions = new SessionList();
foreach ($this->ordered as $event) {
if ($this->ordered->key()==0) {
$sessions=$this->addNewSession($sessions, $event);
if ($this->ordered->key() === 0) {
$sessions = $this->addNewSession($sessions, $event);
continue;
}
/**
* @var Session $sesion
* @var Session $session
*/
$sesion=$sessions->current();
$session = $sessions->current();

$last=$sesion->getEvents()->last();
$new=0;
if ($last->getType()!=$event->getType()) {
$new=1;
$last = $session->getEvents()->last();
$new = 0;
if (($last->getType() !== $event->getType()) ||
($event->getStart() - $last->getEnd() > $this->timeMerge)) {
$new = 1;
}
if (($event->getStart() - $last->getEnd())>$this->timeMerge) {
$new=1;
}
if ($new==1) {
$sessions=$this->addNewSession($sessions, $event);
if ($new === 1) {
$sessions = $this->addNewSession($sessions, $event);
continue;
}
$sesion->getEvents()->add($event);
$sesion->setEnd($event->getEnd());
$session->getEvents()->add($event);
$session->setEnd($event->getEnd());
}
return $sessions;
}

/**
* @param SessionList $sessions
* @param Event $event
*
* @return SessionList
*/
public function addNewSession(SessionList $sessions, Event $event): SessionList
{
$tmp=new Session();
$tmp = new Session();
$tmp
->setStart($event->getStart())
->setEnd($event->getEnd())
Expand Down
3 changes: 2 additions & 1 deletion src/Session.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

declare(strict_types=1);

namespace Marioquartz\MakingSessions;

Expand All @@ -9,7 +10,7 @@ class Session extends TimeBucket

public function __construct()
{
$this->events=new EventList();
$this->events = new EventList();
}

public function getEvents(): EventList
Expand Down
3 changes: 2 additions & 1 deletion src/SessionList.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

declare(strict_types=1);

namespace Marioquartz\MakingSessions;

class SessionList extends ItemList
{
public function order(): ItemList
{
return self::orderList($this, "Session");
return self::orderList($this, 'Session');
}
}

0 comments on commit 64abb44

Please sign in to comment.