-
Notifications
You must be signed in to change notification settings - Fork 0
/
Product.php
328 lines (273 loc) · 8.47 KB
/
Product.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<!--
Project: Short PLCS PHP Implementation
Started : 2019, oct 13
From : http://www.plcs.org/plcslib/plcslib/data/PLCS/psm_model/model_base.html
PURPOSE:
Learn bases of object oriented programming how to :
- call classes, create objects,
- use inherance, ploymorphism, encapsulation,
- implement Interface and abstract class
=== THIS IS THE PRODUCT CLASS
SysLM block diagram http://www.plcs.org/plcslib/plcslib/data/PLCS/psm_model/images/SysML_Block_Definition_Diagram__Diagrams__Product.png
-->
<?php
include 'ReferenceData.php';
require 'Identifier.php';
/*
Definition:
A Product is the identification of a product or of a type of product. It is a collector of data common to all revisions of the Product.
*/
class Product implements JsonSerializable
{
/******************************************************************************
/*-------------------------------------Properties----------------------------------
/************************************************/
/* * * * * * * Parts properties * * * * * * */
//id [1..*] : Identifier ; a set of Identifiers for the Product
private $id;
// name [0..*] : Name
private $name;
// description [0..*] : Descriptor ; a set of text based descriptions of the Product.
private $description ;
// classifiedAs [0..*] : Classification ; a reference to a class held externally to the exchange file that classifies this Product This Product is a member of the referenced class.
private $classifiedAs;
// state [1] : Classification
private $state;
// handling [1] : Classification
private $handling;
// weight [0..1] : NumericalValue ;
private $weight;
/* * * * * * * Reference properties * * * * * * */
// goingToRoom [0..1] :
private $goingToRoom;
// document [0..*] : Document
private $document;
/******************************************************************************
/*-------------------------------------Functions----------------------------------
/************************************************/
/* Build a product with at least those 3 properties :
id, name and state */
public function __construct($myName=null)
{
// 3 properties : id, name and state
global $STATE_NEW, $HANDLING_NORMAL;
$id01 = new Identifier();
$this->id = array($id01);
$this->state = $STATE_NEW;
$this->handling = $HANDLING_NORMAL;
if($myName != null){
$this->name[]= $myName;
}
}
public function jsonSerialize() {
return [
'id' => $this->id,
'name' => $this->name,
'descrition' => $this->description,
'classifiedAs' => $this->classifiedAs,
'state' => $this->state,
'handling' => $this->handling,
'weight' => $this->weight,
'goingToRoom' => $this->goingToRoom,
'document' => $this->document
];
}
// Check user input and return the validated string
public static function test_format($input){
// check if cardboard name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$input)) {
$Err = "<br> Only letters and white space allowed <br>";
echo $Err;
}
else return $input;
}
// If the user add a space or tab, newline, backslashes(\), they should be removed. Trim() and stripslashes() remove them. Hackers might exploit $_SERVER["PHP_SELF"] by adding scripts in the user fields (Cross-site Scriting attack). htmlspecialchars() means to avoid this.
public static function clean_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
/* Obsolete
public function get_id()
{
return $this->id->get_idString();
}
*/
// modify existing id of the current product
public function set_id($newId, $index)
{
$this->id[$index] = $newId;
}
// Add a new id
// at the end of the array
public function new_id($newId)
{
$this->id[]= $newId;
}
// return the last defined current id product
// might use current — Return the current element in an array
// get_ids (if $full = TRUE): Return the whole property all the id values in an array
// get_ids (if $full = FALSE):Return only the id values in an array
// might need 'array_values' — Return all the values of an array
public function get_ids($full)
{
if($full == TRUE){
return $this->id;
}
else{
$pdt_ids = array();// initiate an empty array
// For each id[] index, the Identifier's idString is put in an array
foreach ($this->id as $id_index)
{
$pdt_ids[] = $id_index->get_idString();
// $pdt_ids[] = $id_index ;
}
// Return the array
return $pdt_ids;
}
}
/* might use :
in_array — Checks if a value exists in an array
array_search — Searches the array for a given value and returns the first corresponding key if successful
*/
/**/
public function in_ids($qid)
{
if (in_array($qid, $this->id, TRUE))
{
echo "this id was found with strict check\n";
}
}
// remove the current product's specified id
public function get_name()
{
return $this->name;
}
// modify one of the names
// by finding the name the index of the name be to modified
public function set_name($newName, $index)
{
if(is_int($index) && $index >= 0 && $index < sizeof($this->name)){
$this->name[$index] = $newName;
}
else{
echo "<div> parameter index value shall be an integer [0, ". sizeof($this->name) . "[</div>" ;
}
}
// Add a new name
// at the end of the array
public function new_name($newName)
{
$this->name[]= $newName;
}
public function new_description($newDescription)
{
$this->description[]= $newDescription;
}
public function get_description()
{
return $this->description;
}
public function set_description($descr, $index)
{
$this->description[$index] = $descr;
}
public function get_classifiedAs()
{
return $this->classifiedAs;
}
public function set_classification($classification, $index)
{
$this->classifiedAs[$index] = $classification;
}
public function new_classification($newClassification)
{
$this->classifiedAs[]= $newClassification;
}
public function get_handling()
{
return $this->handling;
}
public function set_handling($handlingWay)
{
global $HANDLING_NORMAL, $HANDLING_FRAGILE ;
if($handlingWay == 0){
$this->handling = $HANDLING_NORMAL;
}
elseif($handlingWay == 1){
$this->handling = $HANDLING_FRAGILE;
}
else{
echo "Error value range. Please enter value 0 for normal handling or 1 for fragile handling.";
}
}
public function get_state()
{
return $this->state;
}
public function set_state($nextState)
{
GLOBAL $STATE_NEW, $STATE_GOOD, $STATE_USED, $STATE_DAMAGED ;
if($nextState == 1){ $this->state = $STATE_NEW; }
elseif($nextState == 2){ $this->state = $STATE_GOOD; }
elseif($nextState == 3){ $this->state = $STATE_USED; }
elseif($nextState == 4){ $this->state = $STATE_DAMAGED; }
else{ echo "<div> Argument range error. Value should be :<br>1 for new state, <br>2 for good state, <br>3 for used state, <br>4 for damaged state </div>";
}
}
public function get_weight()
{
return $this->weight;
}
public function set_weight($newWeight)
{
if(is_numeric($newWeight) && $newWeight > 0){
$this->weight = $newWeight;
}
else{
return "Weight value shall be a positive number.";
}
}
public function add_weight($additionalWeight){
if(is_numeric($additionalWeight)){
$this->weight = $this->weight + $additionalWeight;
}
else{
echo "\n Additional weight value shall be a number. \n";
}
}
public function remove_weight($removedWeight){
if(is_numeric($remove_weight)){
$this->weight = $this->weight - $remove_weight;
}
else{
echo "\n Removed Weight value shall be a number. \n";
}
}
public function reset_weight(){
$this->weight = NULL;
}
/* * * * * * *
Missing
setters & getters for :
Reference properties
* * * * * * */
public function get_goingToRoom(){
return $this->goingToRoom;
}
public function get_document(){
return $this->document;
}
}
// Set_weight() set_Name() unit tests
/* $c1 = new Product('Jouets');
//var_dump($c1);
// $c1->set_weight(0);
$json1 = json_encode($c1);
echo "<div> c1 :<br>". $json1 . "</div>";
$c1->set_name('jouet ame',0);
$json1 = json_encode($c1, JSON_PRETTY_PRINT);
echo "<div> c1 :<br>". $json1 . "</div>";
*/
?>