Skip to content
This repository was archived by the owner on Jul 28, 2024. It is now read-only.

Commit f1633f9

Browse files
committed
Add ContentTypes utils
1 parent c8a8e59 commit f1633f9

8 files changed

+341
-181
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ composer require wavevision/utils
2020
The package contains useful classes for:
2121

2222
- [Arrays](./src/Utils/Arrays.php) – array helpers (manipulate, sort, extract etc.)
23+
- [ContentTypes](./src/Utils/ContentTypes.php) – format extensions and filenames for content types
2324
- [DOM](./src/Utils/DOM) – create and format data attributes for HTML elements
2425
- [ExternalProgram](./src/Utils/ExternalProgram/Executor.php) – simple external command runner
2526
- [Finder](./src/Utils/Finder.php) – adds sorting to [nette/finder](https://github.com/nette/finder)

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"require-dev": {
4444
"jakub-onderka/php-parallel-lint": "^1.0",
4545
"mikey179/vfsstream": "^1.6",
46-
"phing/phing": "^2.16",
46+
"phing/phing": "^3.0-alpha3",
4747
"php-coveralls/php-coveralls": "^2.1",
4848
"php-mock/php-mock-phpunit": "^2.4",
4949
"phpstan/phpstan": "^0.12.3",

composer.lock

+233-175
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Utils/Arrays.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ function (array $item) use ($key) {
144144
}
145145

146146
/**
147-
* @param iterable<mixed> $collection
147+
* @param mixed $collection
148148
* @return mixed
149149
*/
150-
public static function firstItem(iterable $collection)
150+
public static function firstItem($collection)
151151
{
152152
return $collection[self::firstKey($collection)] ?? null;
153153
}
@@ -356,17 +356,17 @@ public static function jsonPath(array $data, string $expression)
356356
}
357357

358358
/**
359-
* @param iterable<mixed> $collection
359+
* @param mixed $collection
360360
* @return mixed
361361
*/
362-
public static function lastItem(iterable $collection)
362+
public static function lastItem($collection)
363363
{
364364
return $collection[self::lastKey($collection)] ?? null;
365365
}
366366

367367
/**
368368
* @param iterable<mixed> $collection
369-
* @return mixed
369+
* @return int|string|null
370370
*/
371371
public static function lastKey(iterable $collection)
372372
{

src/Utils/ContentTypes.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare (strict_types = 1);
2+
3+
namespace Wavevision\Utils;
4+
5+
use Nette\StaticClass;
6+
7+
class ContentTypes
8+
{
9+
10+
use StaticClass;
11+
12+
public const CSS = 'text/css';
13+
14+
public const CSS_EXTENSION = 'css';
15+
16+
public const JPEG = 'image/jpeg';
17+
18+
public const JPEG_EXTENSION = 'jpg';
19+
20+
public const JS = 'application/javascript';
21+
22+
public const JS_EXTENSION = 'js';
23+
24+
public const PDF = 'application/pdf';
25+
26+
public const PDF_EXTENSION = 'pdf';
27+
28+
public const PNG = 'image/png';
29+
30+
public const PNG_EXTENSION = 'png';
31+
32+
public const SVG = 'image/svg+xml';
33+
34+
public const SVG_EXTENSION = 'svg';
35+
36+
public const ZIP = 'application/zip';
37+
38+
public const ZIP_EXTENSION = 'zip';
39+
40+
public const CONTENT_TYPE_FILE_EXTENSIONS = [
41+
self::CSS => self::CSS_EXTENSION,
42+
self::JPEG => self::JPEG_EXTENSION,
43+
self::JS => self::JS_EXTENSION,
44+
self::PDF => self::PDF_EXTENSION,
45+
self::PNG => self::PNG_EXTENSION,
46+
self::SVG => self::SVG_EXTENSION,
47+
self::ZIP => self::ZIP_EXTENSION,
48+
];
49+
50+
public static function getExtension(string $contentType, bool $withDelimiter = false): string
51+
{
52+
$ext = self::CONTENT_TYPE_FILE_EXTENSIONS[$contentType];
53+
return $withDelimiter ? ".$ext" : $ext;
54+
}
55+
56+
public static function getFilename(string $file, string $contentType): string
57+
{
58+
return $file . self::getExtension($contentType, true);
59+
}
60+
61+
}

src/Utils/DOM/DataAttribute.php

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ public function name(): string
5858
return $this->currentName;
5959
}
6060

61+
/**
62+
* @param Html<mixed> $element
63+
* @return Html<mixed>
64+
*/
65+
public function remove(Html $element): Html
66+
{
67+
$element->removeAttribute($this->currentName);
68+
return $element;
69+
}
70+
6171
/**
6272
* @param mixed $value
6373
*/

tests/UtilsTests/ContentTypesTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare (strict_types = 1);
2+
3+
namespace Wavevision\UtilsTests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Wavevision\Utils\ContentTypes;
7+
8+
class ContentTypesTest extends TestCase
9+
{
10+
11+
public function testGetExtension(): void
12+
{
13+
$this->assertEquals('js', ContentTypes::getExtension(ContentTypes::JS));
14+
$this->assertEquals('.js', ContentTypes::getExtension(ContentTypes::JS, true));
15+
}
16+
17+
public function testGetFilename(): void
18+
{
19+
$this->assertEquals('style.css', ContentTypes::getFilename('style', ContentTypes::CSS));
20+
}
21+
22+
}

tests/UtilsTests/DOM/DataAttributeTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public function testName(): void
3232
$this->assertEquals('data-prefix-test', $this->createDataAttribute('prefix')->name());
3333
}
3434

35+
public function testRemove(): void
36+
{
37+
$attribute = $this->createDataAttribute();
38+
$element = Html::el();
39+
$this->assertTrue($attribute->assign($element)->getAttribute($attribute->name()) === '');
40+
$this->assertNull($attribute->remove($element)->getAttribute($attribute->name()));
41+
}
42+
3543
public function testValue(): void
3644
{
3745
$this->assertEquals('something', $this->createDataAttribute()->value('something'));

0 commit comments

Comments
 (0)