Completed
Pull Request — master (#314)
by greg
03:20
created

TradingCardModel::createChrono()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace PlaygroundGame\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Doctrine\ORM\Mapping\HasLifecycleCallbacks;
8
use Doctrine\ORM\Mapping\PrePersist;
9
use Doctrine\ORM\Mapping\PreUpdate;
10
use Zend\InputFilter\InputFilter;
11
use Zend\InputFilter\Factory as InputFactory;
12
use Zend\InputFilter\InputFilterAwareInterface;
13
use Zend\InputFilter\InputFilterInterface;
14
use Gedmo\Mapping\Annotation as Gedmo;
15
use Gedmo\Translatable\Translatable;
16
17
/**
18
 * @ORM\Entity @HasLifecycleCallbacks
19
 * @ORM\Table(name="game_tradingcard_model")
20
 * @Gedmo\TranslationEntity(class="PlaygroundGame\Entity\GameTranslation")
21
 */
22
class TradingCardModel implements InputFilterAwareInterface, \JsonSerializable
23
{
24
    protected $inputFilter;
25
26
    /**
27
     * @ORM\Id
28
     * @ORM\Column(type="integer");
29
     * @ORM\GeneratedValue(strategy="AUTO")
30
     */
31
    protected $id;
32
33
    /**
34
     * @ORM\ManyToOne(targetEntity="TradingCard", inversedBy="models")
35
     * @ORM\JoinColumn(name="game_id", referencedColumnName="id", onDelete="CASCADE")
36
     */
37
    protected $game;
38
39
    /**
40
     * @ORM\OneToMany(targetEntity="TradingCardCard", mappedBy="model", cascade={"persist","remove"})
41
     */
42
    private $cards;
43
44
    /**
45
     * @Gedmo\Translatable
46
     * @ORM\Column(type="string", length=255, nullable=false)
47
     */
48
    protected $title;
49
50
    /**
51
     * @Gedmo\Translatable
52
     * @ORM\Column(type="text", nullable=true)
53
     */
54
    protected $description;
55
56
    /**
57
     * @Gedmo\Translatable
58
     * @ORM\Column(name="image", type="string", length=255, nullable=true)
59
     */
60
    protected $image;
61
62
    /**
63
     * The type of the card : collector, standard, ...
64
     * @Gedmo\Translatable
65
     * @ORM\Column(type="string", length=255, nullable=true)
66
     */
67
    protected $type;
68
69
    /**
70
     * a card can be part of a set of cards
71
     * @Gedmo\Translatable
72
     * @ORM\Column(type="string", length=255, nullable=true)
73
     */
74
    protected $family;
75
76
    /**
77
     * @ORM\Column(type="integer", nullable=true)
78
     */
79
    protected $points = 0;
80
81
    /**
82
     * @ORM\Column(type="float", nullable=false)
83
     */
84
    protected $distribution = 1;
85
86
    /**
87
     * datetime when this model will be available
88
     * @ORM\Column(type="datetime", nullable=true)
89
     */
90
    protected $availability;
91
92
    /**
93
     * @ORM\Column(name="json_data", type="text", nullable=true)
94
     */
95
    protected $jsonData;
96
97
    /**
98
     * @ORM\Column(name="created_at", type="datetime")
99
     */
100
    protected $createdAt;
101
102
    /**
103
     * @ORM\Column(name="updated_at",type="datetime")
104
     */
105
    protected $updatedAt;
106
107
    public function __construct()
108
    {
109
        $this->cards = new ArrayCollection();
110
    }
111
112
    /**
113
     * @PrePersist
114
     */
115
    public function createChrono()
116
    {
117
        $this->createdAt = new \DateTime("now");
118
        $this->updatedAt = new \DateTime("now");
119
    }
120
121
    /**
122
     * @PreUpdate
123
     */
124
    public function updateChrono()
125
    {
126
        $this->updatedAt = new \DateTime("now");
127
    }
128
129
    /**
130
     * Gets the value of id.
131
     *
132
     * @return mixed
133
     */
134
    public function getId()
135
    {
136
        return $this->id;
137
    }
138
139
    /**
140
     * Sets the value of id.
141
     *
142
     * @param mixed $id the id
143
     *
144
     * @return self
145
     */
146
    public function setId($id)
147
    {
148
        $this->id = $id;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Gets the value of title.
155
     *
156
     * @return mixed
157
     */
158
    public function getTitle()
159
    {
160
        return $this->title;
161
    }
162
163
    /**
164
     * Sets the value of title.
165
     *
166
     * @param mixed $title the title
167
     *
168
     * @return self
169
     */
170
    public function setTitle($title)
171
    {
172
        $this->title = $title;
173
174
        return $this;
175
    }
176
177
    /**
178
     * Gets the value of description.
179
     *
180
     * @return mixed
181
     */
182
    public function getDescription()
183
    {
184
        return $this->description;
185
    }
186
187
    /**
188
     * Sets the value of description.
189
     *
190
     * @param mixed $description the description
191
     *
192
     * @return self
193
     */
194
    public function setDescription($description)
195
    {
196
        $this->description = $description;
197
198
        return $this;
199
    }
200
201
    /**
202
     * Gets the value of image.
203
     *
204
     * @return mixed
205
     */
206
    public function getImage()
207
    {
208
        return $this->image;
209
    }
210
211
    /**
212
     * Sets the value of image.
213
     *
214
     * @param mixed $image the image
215
     *
216
     * @return self
217
     */
218
    public function setImage($image)
219
    {
220
        $this->image = $image;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Gets the value of type.
227
     *
228
     * @return mixed
229
     */
230
    public function getType()
231
    {
232
        return $this->type;
233
    }
234
235
    /**
236
     * Sets the value of type.
237
     *
238
     * @param mixed $type the type
239
     *
240
     * @return self
241
     */
242
    public function setType($type)
243
    {
244
        $this->type = $type;
245
246
        return $this;
247
    }
248
249
    /**
250
     * Gets the value of family.
251
     *
252
     * @return mixed
253
     */
254
    public function getFamily()
255
    {
256
        return $this->family;
257
    }
258
259
    /**
260
     * Sets the value of family.
261
     *
262
     * @param mixed $family the family
263
     *
264
     * @return self
265
     */
266
    public function setFamily($family)
267
    {
268
        $this->family = $family;
269
270
        return $this;
271
    }
272
273
    /**
274
     * Gets the value of points.
275
     *
276
     * @return mixed
277
     */
278
    public function getPoints()
279
    {
280
        return $this->points;
281
    }
282
283
    /**
284
     * Sets the value of points.
285
     *
286
     * @param mixed $points the points
287
     *
288
     * @return self
289
     */
290
    public function setPoints($points)
291
    {
292
        $this->points = $points;
293
294
        return $this;
295
    }
296
297
    /**
298
     * Gets the value of distribution.
299
     *
300
     * @return mixed
301
     */
302
    public function getDistribution()
303
    {
304
        return $this->distribution;
305
    }
306
307
    /**
308
     * Sets the value of distribution.
309
     *
310
     * @param mixed $distribution the distribution
311
     *
312
     * @return self
313
     */
314
    public function setDistribution($distribution)
315
    {
316
        $this->distribution = $distribution;
317
318
        return $this;
319
    }
320
321
    /**
322
     * Gets the value of availability.
323
     *
324
     * @return mixed
325
     */
326
    public function getAvailability()
327
    {
328
        return $this->availability;
329
    }
330
331
    /**
332
     * Sets the value of availability.
333
     *
334
     * @param mixed $availability the availability
335
     *
336
     * @return self
337
     */
338
    public function setAvailability($availability)
339
    {
340
        $this->availability = $availability;
341
342
        return $this;
343
    }
344
345
    /**
346
     * Gets the value of game.
347
     *
348
     * @return mixed
349
     */
350
    public function getGame()
351
    {
352
        return $this->game;
353
    }
354
355
    /**
356
     * Sets the value of game.
357
     *
358
     * @param mixed $game the game
359
     *
360
     * @return self
361
     */
362
    public function setGame($game)
363
    {
364
        $this->game = $game;
365
366
        return $this;
367
    }
368
369
        /**
370
     * Gets the value of jsonData.
371
     *
372
     * @return mixed
373
     */
374
    public function getJsonData()
375
    {
376
        return $this->jsonData;
377
    }
378
379
    /**
380
     * Sets the value of jsonData.
381
     *
382
     * @param mixed $jsonData the json data
383
     *
384
     * @return self
385
     */
386
    public function setJsonData($jsonData)
387
    {
388
        $this->jsonData = $jsonData;
389
390
        return $this;
391
    }
392
393
    /**
394
     * Gets the value of cards.
395
     *
396
     * @return mixed
397
     */
398
    public function getCards()
399
    {
400
        return $this->cards;
401
    }
402
403
    /**
404
     * Sets the value of cards.
405
     *
406
     * @param mixed $cards the cards
407
     *
408
     * @return self
409
     */
410
    private function setCards($cards)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
411
    {
412
        $this->cards = $cards;
413
414
        return $this;
415
    }
416
417
    public function addCards(ArrayCollection $cards)
418
    {
419
        foreach ($cards as $card) {
420
            $card->setModel($this);
421
            $this->cards->add($card);
422
        }
423
    }
424
425
    public function removeCards(ArrayCollection $cards)
426
    {
427
        foreach ($cards as $card) {
428
            $card->setModel(null);
429
            $this->cards->removeElement($card);
430
        }
431
    }
432
433
    /**
434
     * Add an card to the Model.
435
     *
436
     * @param TradingCardModel $card
437
     *
438
     * @return void
439
     */
440
    public function addCard($card)
441
    {
442
        $this->cards[] = $card;
443
    }
444
445
    /**
446
     * Gets the value of createdAt.
447
     *
448
     * @return mixed
449
     */
450
    public function getCreatedAt()
451
    {
452
        return $this->createdAt;
453
    }
454
455
    /**
456
     * Sets the value of createdAt.
457
     *
458
     * @param mixed $createdAt the created at
459
     *
460
     * @return self
461
     */
462
    public function setCreatedAt($createdAt)
463
    {
464
        $this->createdAt = $createdAt;
465
466
        return $this;
467
    }
468
469
    /**
470
     * Gets the value of updatedAt.
471
     *
472
     * @return mixed
473
     */
474
    public function getUpdatedAt()
475
    {
476
        return $this->updatedAt;
477
    }
478
479
    /**
480
     * Sets the value of updatedAt.
481
     *
482
     * @param mixed $updatedAt the updated at
483
     *
484
     * @return self
485
     */
486
    public function setUpdatedAt($updatedAt)
487
    {
488
        $this->updatedAt = $updatedAt;
489
490
        return $this;
491
    }
492
493
    /**
494
     * Convert the object to an array.
495
     *
496
     * @return array
497
     */
498
    public function getArrayCopy()
499
    {
500
        $obj_vars = get_object_vars($this);
501
502
        return $obj_vars;
503
    }
504
505
    /**
506
     * Convert the object to json.
507
     *
508
     * @return array
509
     */
510
    public function jsonSerialize()
511
    {
512
        $jsonArray = $this->getArrayCopy();
513
        unset($jsonArray['inputFilter']);
514
        unset($jsonArray['__initializer__']);
515
        unset($jsonArray['__cloner__']);
516
        unset($jsonArray['__isInitialized__']);
517
        unset($jsonArray['game']);
518
        unset($jsonArray['cards']);
519
        unset($jsonArray['distribution']);
520
        unset($jsonArray['createdAt']);
521
        unset($jsonArray['updatedAt']);
522
        
523
        return $jsonArray;
524
    }
525
526
        /**
527
     * Populate from an array.
528
     *
529
     * @param array $data
530
     */
531
    public function populate($data = array())
532
    {
533 View Code Duplication
        if (isset($data['title']) && $data['title'] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
534
            $this->title = $data['title'];
535
        }
536
537 View Code Duplication
        if (isset($data['description']) && $data['description'] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
538
            $this->description = $data['description'];
539
        }
540
541 View Code Duplication
        if (isset($data['jsonData']) && $data['jsonData'] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
542
            $this->jsonData = $data['jsonData'];
543
        }
544
545
        if (isset($data['distribution']) && $data['distribution'] !== null) {
546
            $this->distribution = $data['distribution'];
547
        }
548
549 View Code Duplication
        if (isset($data['type']) && $data['type'] !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
550
            $this->type = $data['type'];
551
        }
552
553
        if (isset($data['family']) && $data['family'] !== null) {
554
            $this->family = $data['family'];
555
        }
556
557
        $this->points = (!empty($data['points'])) ?
558
            $this->points = $data['points']:
559
            0;
560
561
        if (!empty($data['image'])) {
562
            $this->image = $data['image'];
563
        }
564
565
        $this->availability  = (!empty($data['availability'])) ?
566
            \DateTime::createFromFormat('d/m/Y H:i:s', $data['availability']) :
567
            null;
568
    }
569
570
    public function setInputFilter(InputFilterInterface $inputFilter)
571
    {
572
        throw new \Exception("Not used");
573
    }
574
575
    public function getInputFilter()
576
    {
577
        if (!$this->inputFilter) {
578
            $inputFilter = new InputFilter();
579
            $factory = new InputFactory();
580
581
            $inputFilter->add($factory->createInput(array(
582
                'name' => 'availability',
583
                'required' => false,
584
            )));
585
586
            $this->inputFilter = $inputFilter;
587
        }
588
589
        return $this->inputFilter;
590
    }
591
}
592