Skip to content

Commit bba3914

Browse files
committed
🎨 Improve code format
1 parent 7c1f247 commit bba3914

File tree

5 files changed

+93
-56
lines changed

5 files changed

+93
-56
lines changed

src/Error.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
<?php namespace Sikessem;
1+
<?php
22

3-
class Error extends \Exception {
3+
namespace Sikessem;
44

5-
public const INVALID_TYPE = 0x00001;
6-
public const INVALID_KEY = 0x00002;
7-
public const INVALID_VALUE = 0x00003;
5+
class Error extends \Exception
6+
{
7+
public const INVALID_TYPE = 0x00001;
8+
9+
public const INVALID_KEY = 0x00002;
10+
11+
public const INVALID_VALUE = 0x00003;
812
}

src/Filter.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
<?php namespace Sikessem;
1+
<?php
22

3-
class Filter {
3+
namespace Sikessem;
44

5-
public static function validate(string $type): bool {
5+
class Filter
6+
{
7+
public static function validate(string $type): bool
8+
{
69

7-
return defined(Type::class . "::$type");
8-
}
10+
return defined(Type::class."::$type");
11+
}
912

10-
public static function sanitize(string $type): string {
13+
public static function sanitize(string $type): string
14+
{
1115

12-
return strtoupper(trim($type));
13-
}
16+
return strtoupper(trim($type));
17+
}
1418
}

src/Map.php

+49-31
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,67 @@
1-
<?php namespace Sikessem;
1+
<?php
22

3-
class Map {
3+
namespace Sikessem;
44

5-
public function __construct(string $keys_type, string $values_type) {
5+
class Map
6+
{
7+
public function __construct(string $keys_type, string $values_type)
8+
{
69

7-
$keys_type = Filter::sanitize($keys_type);
8-
if(!Filter::validate($keys_type)) throw new Error("Unknown type $keys_type", Error::INVALID_TYPE);
9-
$this->keys_type = $keys_type;
10+
$keys_type = Filter::sanitize($keys_type);
11+
if (! Filter::validate($keys_type)) {
12+
throw new Error("Unknown type $keys_type", Error::INVALID_TYPE);
13+
}
14+
$this->keys_type = $keys_type;
1015

11-
$values_type = Filter::sanitize($values_type);
12-
if(!Filter::validate($values_type)) throw new Error("Unknown type $values_type", Error::INVALID_TYPE);
13-
$this->values_type = $values_type;
14-
}
16+
$values_type = Filter::sanitize($values_type);
17+
if (! Filter::validate($values_type)) {
18+
throw new Error("Unknown type $values_type", Error::INVALID_TYPE);
19+
}
20+
$this->values_type = $values_type;
21+
}
1522

16-
protected int $count = 0;
23+
protected int $count = 0;
1724

18-
protected string $keys_type;
19-
protected array $keys_list = [];
25+
protected string $keys_type;
2026

21-
protected string $values_type;
22-
protected array $values_list = [];
27+
protected array $keys_list = [];
2328

24-
public function set(mixed $key, mixed $value): static {
29+
protected string $values_type;
2530

26-
$key_type = Filter::sanitize(gettype($key));
27-
if($key_type !== 'mixed' && $key_type !== $this->keys_type) throw new Error("Invalid key type ($key_type) given", Error::INVALID_KEY);
31+
protected array $values_list = [];
2832

29-
$value_type = Filter::sanitize(gettype($value));
30-
if($value_type !== 'mixed' && $value_type !== $this->values_type) throw new Error("Invalid value type ($value_type) given", Error::INVALID_VALUE);
33+
public function set(mixed $key, mixed $value): static
34+
{
3135

32-
if(false === ($index = array_search($key, $this->keys_list, true))) $index = $this->count++;
36+
$key_type = Filter::sanitize(gettype($key));
37+
if ($key_type !== 'mixed' && $key_type !== $this->keys_type) {
38+
throw new Error("Invalid key type ($key_type) given", Error::INVALID_KEY);
39+
}
3340

34-
$this->keys_list[$index] = $key;
35-
$this->values_list[$index] = $value;
41+
$value_type = Filter::sanitize(gettype($value));
42+
if ($value_type !== 'mixed' && $value_type !== $this->values_type) {
43+
throw new Error("Invalid value type ($value_type) given", Error::INVALID_VALUE);
44+
}
3645

37-
return $this;
38-
}
46+
if (false === ($index = array_search($key, $this->keys_list, true))) {
47+
$index = $this->count++;
48+
}
3949

40-
public function get(mixed $key): mixed {
50+
$this->keys_list[$index] = $key;
51+
$this->values_list[$index] = $value;
4152

42-
return false === ($index = array_search($key, $this->keys_list, true))? null: $this->values_list[$index];
43-
}
53+
return $this;
54+
}
4455

45-
public function has(mixed $key): bool {
56+
public function get(mixed $key): mixed
57+
{
4658

47-
return in_array($key, $this->keys_list, true);
48-
}
59+
return false === ($index = array_search($key, $this->keys_list, true)) ? null : $this->values_list[$index];
60+
}
61+
62+
public function has(mixed $key): bool
63+
{
64+
65+
return in_array($key, $this->keys_list, true);
66+
}
4967
}

src/Type.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
<?php namespace Sikessem;
1+
<?php
22

3-
class Type {
3+
namespace Sikessem;
44

5-
public const BOOLEAN = 'boolean';
6-
public const INTEGER = 'integer';
7-
public const DOUBLE = 'double';
8-
public const STRING = 'string';
9-
public const ARRAY = 'array';
10-
public const OBJECT = 'object';
11-
public const RESOURCE = 'resource';
5+
class Type
6+
{
7+
public const BOOLEAN = 'boolean';
8+
9+
public const INTEGER = 'integer';
10+
11+
public const DOUBLE = 'double';
12+
13+
public const STRING = 'string';
14+
15+
public const ARRAY = 'array';
16+
17+
public const OBJECT = 'object';
18+
19+
public const RESOURCE = 'resource';
1220
}

src/sikessem.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
<?php namespace Sikessem;
1+
<?php
22

3-
function map(string $key_type, string $value_type): Map {
3+
namespace Sikessem;
44

5-
return new Map($key_type, $value_type);
5+
function map(string $key_type, string $value_type): Map
6+
{
7+
8+
return new Map($key_type, $value_type);
69
}

0 commit comments

Comments
 (0)