-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocationCode.php
More file actions
127 lines (113 loc) · 3.4 KB
/
LocationCode.php
File metadata and controls
127 lines (113 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
namespace Cable8mm\GoodCode;
use Stringable;
/**
* This class represents a warehouse location identified by warehouse, rack, and shelf. The location code is generated from these properties.
*
* @author Samgu Lee <cable8mm@gmail.com>
*
* @since 2025-02-24
*/
class LocationCode implements Stringable
{
/**
* Code representing the product’s storage location
*
* @example A1-B3-S5
*/
private string $locationCode;
private function __construct(
/**
* Warehouse ID (for multi-warehouse management)
*
* @example AUK
* @example SEL
*/
private ?string $warehouse = null,
/**
* Rack number
*
* @example R3
* @example S5
*/
private ?string $rack = null,
/**
* Shelf number
*
* @example S5
* @example F7
*/
private ?string $shelf = null,
) {
if (is_null($warehouse) && is_null($rack) && is_null($shelf)) {
throw new \InvalidArgumentException('At least one parameter must be provided.');
}
$this->warehouse = $warehouse ?? '';
$this->rack = $rack ?? '';
$this->shelf = $shelf ?? '';
$this->locationCode = implode('-', array_filter([
$this->warehouse,
$this->rack,
$this->shelf,
]));
$this->locationCode = preg_replace('/-+/', '-', $this->locationCode);
}
/**
* Get the location code.
*
* @return string The method returns the location code
*
* @example print LocationCode::of(warehouse: 'AUK', rack: 'R3', shelf: 'S32')->locationCode();
*/
public function locationCode(): string
{
return $this->locationCode;
}
/**
* Create a new Location instance.
*
* @param string|array|null $warehouse Warehouse ID or array of arguments
* @param string|null $rack Rack
* @param string|null $shelf Shelf
* @return self Provides fluent interface
*
* @throws \InvalidArgumentException
*/
public static function of(
string|array|null $warehouse = null,
?string $rack = null,
?string $shelf = null
): self {
if (is_array($warehouse)) {
if (empty($warehouse)) {
throw new \InvalidArgumentException('At least one parameter must be provided.');
}
$disallowedKeys = array_diff_key($warehouse, array_flip(['warehouse', 'rack', 'shelf']));
if (! empty($disallowedKeys)) {
throw new \InvalidArgumentException('Invalid key(s): '.implode(', ', array_keys($disallowedKeys)));
}
return new self(
warehouse: $warehouse['warehouse'] ?? null,
rack: $warehouse['rack'] ?? null,
shelf: $warehouse['shelf'] ?? null
);
}
return new self(
warehouse: $warehouse,
rack: $rack,
shelf: $shelf
);
}
/**
* Get the string representation of the location code.
*
* @return string The magic method returns the location code
*
* @example print LocationCode::of(warehouse: 'A1') => 'A1'
* @example print LocationCode::of(warehouse: 'A1', rack: 'B3') => 'A1-B3'
*/
public function __toString(): string
{
return $this->locationCode;
}
}