Skip to content

Commit 02dfff8

Browse files
committed
wip
refactor tree init starting point in classes no additional functionalities added for now
1 parent a99aff0 commit 02dfff8

39 files changed

+579
-220
lines changed

README.md

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@ This approach has a few key benefits:
5151
### Laravel example
5252

5353
here's how it can be used in practice across different types, focusing on strict handling for both integers and floats:
54+
5455
```php
5556
namespace App\Http\Controllers;
5657

57-
use Illuminate\Http\Request;
58-
use Nejcc\PhpDatatypes\Integers\Unsigned\UInt8;
59-
use Nejcc\PhpDatatypes\Floats\Float32;
58+
use Illuminate\Http\Request;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;
6059

6160
class TestController
6261
{
@@ -86,9 +85,9 @@ Here, we're not only safeguarding user IDs but also handling potentially complex
8685
PHP examples
8786

8887
### Integers
88+
8989
```php
90-
use Nejcc\PhpDatatypes\Integers\Signed\Int8;
91-
use Nejcc\PhpDatatypes\Integers\Unsigned\UInt8;
90+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt8;
9291

9392
$int8 = new Int8(-128); // Minimum value for Int8
9493
echo $int8->getValue(); // -128
@@ -98,9 +97,9 @@ echo $uint8->getValue(); // 255
9897
```
9998

10099
### Floats
100+
101101
```php
102-
use Nejcc\PhpDatatypes\Floats\Float32;
103-
use Nejcc\PhpDatatypes\Floats\Float64;
102+
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;
104103

105104
$float32 = new Float32(3.14);
106105
echo $float32->getValue(); // 3.14
@@ -110,8 +109,9 @@ echo $float64->getValue(); // 1.7976931348623157e308
110109
```
111110

112111
### Arithmetic Operations
112+
113113
```php
114-
use Nejcc\PhpDatatypes\Integers\Signed\Int8;
114+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;
115115

116116
$int1 = new Int8(50);
117117
$int2 = new Int8(30);
@@ -121,6 +121,66 @@ echo $result->getValue(); // 80
121121

122122
```
123123

124+
# ROAD MAP
125+
126+
```md
127+
Data Types
128+
129+
├── Scalar Types
130+
│ ├── Integer Types
131+
│ │ ├── Signed Integers
132+
│ │ │ ├── ✓ Int8
133+
│ │ │ ├── ✓ Int16
134+
│ │ │ ├── ✓ Int32
135+
│ │ │ ├── Int64
136+
│ │ │ └── Int128
137+
│ │ └── Unsigned Integers
138+
│ │ ├── ✓ UInt8
139+
│ │ ├── ✓ UInt16
140+
│ │ ├── ✓ UInt32
141+
│ │ ├── UInt64
142+
│ │ └── UInt128
143+
│ ├── Floating Point Types
144+
│ │ ├── ✓ Float32
145+
│ │ ├── ✓ Float64
146+
│ │ ├── Double
147+
│ │ └── Double Floating Point
148+
│ ├── Boolean
149+
│ │ └── Boolean (true/false)
150+
│ ├── Char
151+
│ └── Byte
152+
153+
├── Composite Types
154+
│ ├── Arrays
155+
│ │ ├── StringArray
156+
│ │ ├── IntArray
157+
│ │ ├── FloatArray
158+
│ │ └── Byte Slice
159+
│ ├── Struct
160+
│ │ └── struct { fields of different types }
161+
│ ├── Union
162+
│ │ └── union { shared memory for different types }
163+
│ ├── List
164+
│ └── Dictionary
165+
166+
├── Reference Types
167+
│ ├── Reference Pointer
168+
│ ├── Void (Nullable)
169+
│ └── Channel (Concurrency)
170+
171+
├── Map Types
172+
│ ├── Hashmap
173+
│ └── Map
174+
175+
└── Specialized Types
176+
├── String
177+
├── Double
178+
├── Slice
179+
├── Map
180+
└── Channel
181+
```
182+
183+
124184
### Testing
125185

126186
```bash

index.php

Lines changed: 103 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,117 @@
11
<?php
22

3-
use Nejcc\PhpDatatypes\Integers\Signed\Int16;
4-
use Nejcc\PhpDatatypes\Integers\Signed\Int32;
5-
use Nejcc\PhpDatatypes\Integers\Signed\Int8;
6-
use Nejcc\PhpDatatypes\Integers\Unsigned\UInt8;
3+
use Nejcc\PhpDatatypes\Composite\Arrays\ByteSlice;
4+
use Nejcc\PhpDatatypes\Composite\Arrays\FloatArray;
5+
use Nejcc\PhpDatatypes\Composite\Arrays\IntArray;
6+
use Nejcc\PhpDatatypes\Composite\Arrays\StringArray;
7+
use Nejcc\PhpDatatypes\Scalar\Byte;
8+
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32;
9+
use Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64;
10+
use Nejcc\PhpDatatypes\Scalar\Char;
11+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int16;
12+
use Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8;
13+
use Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt32;
714

815
require_once __DIR__ . '/vendor/autoload.php';
916

10-
class test
17+
class TestExamples
1118
{
12-
public \Nejcc\PhpDatatypes\Floats\Float8 $min;
19+
private Float32 $account_balance;
20+
private Float64 $investment_amount;
21+
private Char $grade;
22+
private Byte $age;
23+
private StringArray $names;
24+
private IntArray $scores;
25+
private FloatArray $weights;
26+
private ByteSlice $data;
27+
private Int8 $years;
28+
private UInt32 $account_number;
1329

14-
public function __construct(int $int)
30+
public function __construct()
1531
{
16-
$this->min = float8($int);
32+
$this->years = int8(33);
33+
$this->account_number = uint32(343233);
34+
// Scalar Types
35+
$this->account_balance = float32(1234.56);
36+
$this->investment_amount = float64(78910.12345);
37+
$this->grade = new Char('A');
38+
$this->age = new Byte(25);
39+
40+
// Composite Types (Arrays)
41+
$this->names = new StringArray(['John', 'Jane', 'Doe']);
42+
$this->scores = new IntArray([100, 95, 87]);
43+
$this->weights = new FloatArray([60.5, 72.3, 88.9]);
44+
$this->data = new ByteSlice([255, 128, 0]);
45+
1746
}
1847

19-
public function __invoke(): \Nejcc\PhpDatatypes\Floats\Float8
48+
public function getExamples(): array
2049
{
21-
return $this->min;
50+
/*
51+
* Test only
52+
array (size=10)
53+
'years' =>
54+
object(Nejcc\PhpDatatypes\Scalar\Integers\Signed\Int8)[4]
55+
protected readonly int 'value' => int 33
56+
'account_number' =>
57+
object(Nejcc\PhpDatatypes\Scalar\Integers\Unsigned\UInt32)[5]
58+
protected readonly int 'value' => int 343233
59+
'account_balance' =>
60+
object(Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float32)[6]
61+
protected readonly float 'value' => float 1234.56
62+
'investment_amount' =>
63+
object(Nejcc\PhpDatatypes\Scalar\FloatingPoints\Float64)[7]
64+
protected readonly float 'value' => float 78910.12345
65+
'grade' =>
66+
object(Nejcc\PhpDatatypes\Scalar\Char)[8]
67+
private string 'value' => string 'A' (length=1)
68+
'age' =>
69+
object(Nejcc\PhpDatatypes\Scalar\Byte)[9]
70+
private int 'value' => int 25
71+
'names' =>
72+
object(Nejcc\PhpDatatypes\Composite\Arrays\StringArray)[10]
73+
private array 'value' =>
74+
array (size=3)
75+
0 => string 'John' (length=4)
76+
1 => string 'Jane' (length=4)
77+
2 => string 'Doe' (length=3)
78+
'scores' =>
79+
object(Nejcc\PhpDatatypes\Composite\Arrays\IntArray)[11]
80+
private array 'value' =>
81+
array (size=3)
82+
0 => int 100
83+
1 => int 95
84+
2 => int 87
85+
'weights' =>
86+
object(Nejcc\PhpDatatypes\Composite\Arrays\FloatArray)[12]
87+
private array 'value' =>
88+
array (size=3)
89+
0 => float 60.5
90+
1 => float 72.3
91+
2 => float 88.9
92+
'data' =>
93+
object(Nejcc\PhpDatatypes\Composite\Arrays\ByteSlice)[13]
94+
private array 'value' =>
95+
array (size=3)
96+
0 => int 255
97+
1 => int 128
98+
2 => int 0
99+
*/
100+
return [
101+
'years' => $this->years,
102+
'account_number' => $this->account_number,
103+
'account_balance' => $this->account_balance,
104+
'investment_amount' => $this->investment_amount,
105+
'grade' => $this->grade,
106+
'age' => $this->age,
107+
'names' => $this->names,
108+
'scores' => $this->scores,
109+
'weights' => $this->weights,
110+
'data' => $this->data,
111+
];
22112
}
23113
}
24114

25-
$c = new test(8);
26-
var_dump($c);
27-
28-
115+
// Instantiate the class and invoke the examples
116+
$example = new TestExamples();
117+
var_dump($example->getExamples());

src/Abstract/AbstractString.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Nejcc\PhpDatatypes\Abstract;
6+
7+
use Nejcc\PhpDatatypes\Interfaces\StringInterface;
8+
9+
/**
10+
* Abstract class for string types.
11+
*
12+
* @package Nejcc\PhpDatatypes\Abstract
13+
*/
14+
abstract class AbstractString implements StringInterface
15+
{
16+
protected readonly string $value;
17+
18+
/**
19+
* AbstractString constructor.
20+
*
21+
* @param string $value
22+
*/
23+
public function __construct(string $value)
24+
{
25+
$this->setValue($value);
26+
}
27+
28+
/**
29+
* Set the string value.
30+
*
31+
* @param string $value
32+
* @return void
33+
*/
34+
protected function setValue(string $value): void
35+
{
36+
// Perform validations if necessary (e.g., length checks)
37+
$this->value = $value;
38+
}
39+
40+
/**
41+
* Get the string value.
42+
*
43+
* @return string
44+
*/
45+
public function getValue(): string
46+
{
47+
return $this->value;
48+
}
49+
50+
/**
51+
* Compare two strings.
52+
*
53+
* @param StringInterface $other
54+
* @return int
55+
*/
56+
public function compare(StringInterface $other): int
57+
{
58+
return strcmp($this->value, $other->getValue());
59+
}
60+
61+
/**
62+
* Append another string to this one.
63+
*
64+
* @param StringInterface $other
65+
* @return static
66+
*/
67+
public function append(StringInterface $other): static
68+
{
69+
return new static($this->value . $other->getValue());
70+
}
71+
72+
/**
73+
* Get a substring of the current string.
74+
*
75+
* @param int $start
76+
* @param int|null $length
77+
* @return static
78+
*/
79+
public function substring(int $start, ?int $length = null): static
80+
{
81+
return new static(substr($this->value, $start, $length));
82+
}
83+
84+
/**
85+
* Check if this string contains another string.
86+
*
87+
* @param StringInterface $needle
88+
* @return bool
89+
*/
90+
public function contains(StringInterface $needle): bool
91+
{
92+
return str_contains($this->value, $needle->getValue());
93+
}
94+
95+
/**
96+
* Get the length of the string.
97+
*
98+
* @return int
99+
*/
100+
public function length(): int
101+
{
102+
return strlen($this->value);
103+
}
104+
}

src/Composite/Arrays/ByteSlice.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Nejcc\PhpDatatypes\Composite\Arrays;
4+
5+
class ByteSlice {
6+
private array $value;
7+
8+
public function __construct(array $value) {
9+
foreach ($value as $item) {
10+
if (!is_int($item) || $item < 0 || $item > 255) {
11+
throw new \InvalidArgumentException("All elements must be valid bytes (0-255).");
12+
}
13+
}
14+
$this->value = $value;
15+
}
16+
17+
public function getValue(): array {
18+
return $this->value;
19+
}
20+
}

src/Composite/Arrays/FloatArray.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Nejcc\PhpDatatypes\Composite\Arrays;
4+
5+
class FloatArray {
6+
private array $value;
7+
8+
public function __construct(array $value) {
9+
foreach ($value as $item) {
10+
if (!is_float($item)) {
11+
throw new \InvalidArgumentException("All elements must be floats.");
12+
}
13+
}
14+
$this->value = $value;
15+
}
16+
17+
public function getValue(): array {
18+
return $this->value;
19+
}
20+
}

0 commit comments

Comments
 (0)