Skip to content

Commit 06565c9

Browse files
[Task] Cleanup (#6)
* extract build methods for testability * add missing types * handle nullable properties * phpstan level 9 * update readme
1 parent 306208e commit 06565c9

File tree

4 files changed

+27
-24
lines changed

4 files changed

+27
-24
lines changed

README.md

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Generate FFMpeg easing and tweening strings in Laravel.
2-
32
[![Latest Version on Packagist](https://img.shields.io/packagist/v/projektgopher/laravel-ffmpeg-tween.svg?style=flat-square)](https://packagist.org/packages/projektgopher/laravel-ffmpeg-tween)
43
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/projektgopher/laravel-ffmpeg-tween/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/projektgopher/laravel-ffmpeg-tween/actions?query=workflow%3Arun-tests+branch%3Amain)
54
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/projektgopher/laravel-ffmpeg-tween/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/projektgopher/laravel-ffmpeg-tween/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
@@ -26,22 +25,17 @@ The API is modelled after [The GreenSock Animation Platform (GSAP)](https://gree
2625
and all the math for the easings is ported from [Easings.net](https://easings.net).
2726
The stringification of these math strings is ported from [This Gitlab repo](https://gitlab.com/dak425/easing/-/blob/master/ffmpeg/ffmpeg.go)
2827

29-
3028
## Installation
31-
3229
You can install the package via composer:
33-
3430
```bash
3531
composer require projektgopher/laravel-ffmpeg-tween
3632
```
3733

38-
### Using outside of a Laravel application
39-
40-
For now this package can only be used within a Laravel app, but there are plans to extract the core functionality out into a separate package that can be used without being bound to the framework.
41-
4234
## Usage
35+
### Using outside of a Laravel application
36+
For now this package can only be used within a Laravel app, but there are plans to extract the core functionality into a separate package that can be used without being bound to the framework.
4337

44-
Simple tween with delay and duration
38+
### Simple tween with delay and duration
4539
```php
4640
use ProjektGopher\FFMpegTween\Tween;
4741
use ProjektGopher\FFMpegTween\Timing;
@@ -55,7 +49,7 @@ $x = (new Tween())
5549
->ease(Ease::OutSine);
5650
```
5751

58-
Animation sequences using keyframes
52+
### Animation sequences using keyframes
5953
```php
6054
use ProjektGopher\FFMpegTween\Keyframe;
6155
use ProjektGopher\FFMpegTween\Timeline;
@@ -79,15 +73,16 @@ $x->keyframe((new Keyframe)
7973
->duration(Timing::seconds(1))
8074
);
8175
```
76+
8277
> **Note** `new Timeline()` returns a _fluent_ api, meaning methods can be chained as well.
8378
8479
## Testing
85-
8680
```bash
8781
composer test
8882
```
8983

9084
### Visual Snapshot Testing
85+
#### Easing
9186
To generate plots of all `Ease` methods, from the project root, run
9287
```bash
9388
./scripts/generateEasings
@@ -96,6 +91,7 @@ The 256x256 PNGs will be generated in the `tests/Snapshots/Easings` directory.
9691
These snapshots will be ignored by git, but allow visual inspection of the plots to
9792
compare against known good sources, like [Easings.net](https://easings.net).
9893

94+
#### Timelines
9995
To generate a video using a `Timeline` with `Keyframes`, from the project root, run
10096
```bash
10197
./scripts/generateTimeline
@@ -110,22 +106,17 @@ chmod -R 777 ./scripts
110106
```
111107

112108
## Changelog
113-
114109
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
115110

116111
## Contributing
117-
118112
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
119113

120114
## Security Vulnerabilities
121-
122115
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
123116

124117
## Credits
125-
126118
- [Len Woodward](https://github.com/ProjektGopher)
127119
- [All Contributors](../../contributors)
128120

129121
## License
130-
131122
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 4
5+
level: 9
66
paths:
77
- src
88
tmpDir: build/phpstan

src/Timeline.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProjektGopher\FFMpegTween;
44

5+
use ProjektGopher\FFMpegTween\Enums\Ease;
6+
57
class Timeline
68
{
79
private array $keyframes = [];
@@ -42,8 +44,8 @@ public function buildTweens(): void
4244
->from($this->getKeyframeByIndex($index - 1)->value)
4345
->to($keyframe->value)
4446
->delay(Timing::seconds($current_time))
45-
->duration($keyframe->duration)
46-
->ease($keyframe->ease);
47+
->duration($keyframe->duration ?? Timing::seconds(0))
48+
->ease($keyframe->ease ?? Ease::Linear);
4749
}
4850

4951
$current_time += $keyframe->hold?->seconds;
@@ -56,7 +58,7 @@ public function getKeyframeByIndex(int $index): Keyframe
5658
return $this->keyframes[$index];
5759
}
5860

59-
public function __toString(): string
61+
public function build(): string
6062
{
6163
if (count($this->tweens) === 0) {
6264
$this->buildTweens();
@@ -75,4 +77,9 @@ public function __toString(): string
7577

7678
return $timeline;
7779
}
80+
81+
public function __toString(): string
82+
{
83+
return $this->build();
84+
}
7885
}

src/Tween.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class Tween
1616

1717
protected string $ease;
1818

19-
public function from($from): self
19+
public function from(string $from): self
2020
{
2121
$this->from = "({$from})";
2222

2323
return $this;
2424
}
2525

26-
public function to($to): self
26+
public function to(string $to): self
2727
{
2828
$this->to = "({$to})";
2929

@@ -70,7 +70,7 @@ private function getDelta(): string
7070
* Builds the tween string which can be used in an FFMpeg command.
7171
* This is called automatically at the end of the chain.
7272
*/
73-
public function __toString(): string
73+
public function build(): string
7474
{
7575
if (! $this->ease) {
7676
$this->ease(AvailableEasings::Linear);
@@ -90,7 +90,12 @@ public function __toString(): string
9090
return "if(lt(t\,{$this->delay})\,{$this->from}\,if(gt(t\,{$this->delay}+{$this->duration})\,{$this->to}\,{$this->from}+({$this->getDelta()}*{$this->ease})))";
9191
}
9292

93-
public static function __callStatic($name, $arguments): self
93+
public function __toString(): string
94+
{
95+
return $this->build();
96+
}
97+
98+
public static function __callStatic(string $name, array $arguments): self
9499
{
95100
return (new self)->$name(...$arguments);
96101
}

0 commit comments

Comments
 (0)