Completed
Push — master ( e0ceab...5b1c33 )
by Joachim
14:58
created

Category::setCustomInfoLayout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Loevgaard\DandomainFoundation\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use Knp\DoctrineBehaviors\Model\SoftDeletable\SoftDeletable;
8
use Knp\DoctrineBehaviors\Model\Timestampable\Timestampable;
9
use Knp\DoctrineBehaviors\Model\Translatable\Translatable;
10
use Loevgaard\DandomainDateTime\DateTimeImmutable;
11
use Loevgaard\DandomainFoundation\Entity\Generated\CategoryInterface;
12
use Loevgaard\DandomainFoundation\Entity\Generated\CategoryTrait;
13
use Loevgaard\DandomainFoundation;
14
15
/**
16
 * @ORM\Entity()
17
 * @ORM\Table(name="loevgaard_dandomain_categories")
18
 */
19
class Category extends AbstractEntity implements CategoryInterface
20
{
21
    use CategoryTrait;
22
    use Timestampable;
23
    use SoftDeletable;
24
    use Translatable;
25
26
    /**
27
     * @var int
28
     *
29
     * @ORM\Id
30
     * @ORM\GeneratedValue
31
     * @ORM\Column(type="integer")
32
     **/
33
    protected $id;
34
35
    /**
36
     * This is the internal id in the API
37
     *
38
     * @var int
39
     *
40
     * @ORM\Column(type="integer", unique=true)
41
     */
42
    protected $externalId;
43
44
    /**
45
     * @var string
46
     *
47
     * @ORM\Column(type="string", unique=true)
48
     */
49
    protected $number;
50
51
    /**
52
     * @var string|null
53
     *
54
     * @ORM\Column(nullable=true, type="string", length=191)
55
     */
56
    protected $b2bGroupId;
57
58
    /**
59
     * @var \DateTimeImmutable|null
60
     *
61
     * @ORM\Column(nullable=true, type="datetime_immutable")
62
     */
63
    protected $createdDate;
64
65
    /**
66
     * @var int|null
67
     *
68
     * @ORM\Column(nullable=true, type="integer")
69
     */
70
    protected $customInfoLayout;
71
72
    /**
73
     * @var int|null
74
     *
75
     * @ORM\Column(nullable=true, type="integer")
76
     */
77
    protected $customListLayout;
78
79
    /**
80
     * @var int|null
81
     *
82
     * @ORM\Column(nullable=true, type="integer")
83
     */
84
    protected $defaultParentId;
85
86
    /**
87
     * @var \DateTimeImmutable|null
88
     *
89
     * @ORM\Column(nullable=true, type="datetime_immutable")
90
     */
91
    protected $editedDate;
92
93
    /**
94
     * @var int|null
95
     *
96
     * @ORM\Column(nullable=true, type="integer")
97
     */
98
    protected $infoLayout;
99
100
    /**
101
     * @var int|null
102
     *
103
     * @ORM\Column(nullable=true, type="integer")
104
     */
105
    protected $internalId;
106
107
    /**
108
     * @var int|null
109
     *
110
     * @ORM\Column(nullable=true, type="integer")
111
     */
112
    protected $listLayout;
113
114
    /**
115
     * @var bool|null
116
     *
117
     * @ORM\Column(nullable=true, type="boolean")
118
     */
119
    protected $modified;
120
121
    /**
122
     * @var array|null
123
     *
124
     * @ORM\Column(nullable=true, type="json")
125
     */
126
    protected $parentIdList;
127
128
    /**
129
     * @var array|null
130
     *
131
     * @ORM\Column(nullable=true, type="json")
132
     */
133
    protected $segmentIdList;
134
135
    /**
136
     * @var Category[]|ArrayCollection
137
     *
138
     * @ORM\ManyToMany(mappedBy="parentCategories", targetEntity="Category")
139
     */
140
    protected $childrenCategories;
141
142
    /**
143
     * @var Category[]|ArrayCollection
144
     *
145
     * @ORM\JoinTable(name="loevgaard_dandomain_category_parents")
146
     * @ORM\ManyToMany(cascade={"persist"}, inversedBy="childrenCategories", targetEntity="Category")
147
     */
148
    protected $parentCategories;
149
150
    /**
151
     * @var Product[]||ArrayCollection
152
     *
153
     * @ORM\ManyToMany(mappedBy="categories", targetEntity="Product")
154
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment Product[]||ArrayCollection at position 4 could not be parsed: Unknown type name '|' at position 4 in Product[]||ArrayCollection.
Loading history...
155
    protected $products;
156
157
    /**
158
     * @var Segment[]|ArrayCollection
159
     *
160
     * @ORM\JoinTable(name="loevgaard_dandomain_category_segments")
161
     * @ORM\ManyToMany(cascade={"persist"}, inversedBy="categories", targetEntity="Segment")
162
     */
163
    protected $segments;
164
165
    public function __construct()
166
    {
167
        $this->childrenCategories = new ArrayCollection();
168
        $this->parentCategories = new ArrayCollection();
169
        $this->products = new ArrayCollection();
170
        $this->segments = new ArrayCollection();
171
    }
172
173
    /**
174
     * Populates a variant based on the response from the Dandomain API
175
     *
176
     * See the properties here:
177
     * http://4221117.shop53.dandomain.dk/admin/webapi/endpoints/v1_0/ProductDataService/help/operations/GetDataProduct
178
     *
179
     * @param \stdClass|array $data
180
     */
181
    public function populateFromApiResponse($data)
182
    {
183
        $data = DandomainFoundation\objectToArray($data);
184
185
        /*
186
        $actualTexts = null;
187
        if (is_array($data['texts'])) {
188
            foreach ($data['texts'] as $text) {
189
                if ($text->siteId != $this->defaultSiteId) {
190
                    continue;
191
                }
192
193
                $actualTexts = $text;
194
            }
195
        }
196
        */
197
198
        if ($data['createdDate']) {
199
            $this->createdDate = DateTimeImmutable::createFromJson($data['createdDate']);
200
        }
201
202
        if ($data['editedDate']) {
203
            $this->createdDate = DateTimeImmutable::createFromJson($data['editedDate']);
204
        }
205
206
        $this
207
            ->setB2bGroupId($data['b2BGroupId'])
208
            ->setCustomInfoLayout($data['customInfoLayout'])
209
            ->setCustomListLayout($data['customListLayout'])
210
            ->setDefaultParentId($data['defaultParentId'])
211
            ->setExternalId($data['number'])
212
            ->setInfoLayout($data['infoLayout'])
213
            ->setInternalId($data['internalId'])
214
            ->setListLayout($data['listLayout'])
215
            ->setModified($data['modified'])
216
            ->setParentIdList($data['parentIdList'])
217
            ->setSegmentIdList($data['segmentIdList'])
218
        ;
219
220
        /*
221
        if (is_array($data['parentIdList'])) {
222
            foreach ($data['parentIdList'] as $parentId) {
223
                $parentEntity = $this->objectManager->getRepository($this->entityClassName)->findOneBy([
224
                    'externalId' => (int) $parentId,
225
                ]);
226
227
                if (null !== $parentEntity) {
228
                    $this->addParentCategory($parentEntity);
229
                }
230
            }
231
        }
232
233
        if (is_array($data['segmentIdList'])) {
234
            foreach ($data['segmentIdList'] as $segmentId) {
235
                $segment = $this->segmentSynchronizer->syncSegment($segmentId, $flush);
236
                $this->addSegment($segment);
237
            }
238
        }
239
240
        if (($entity instanceof TranslatableInterface) && (is_array($data['texts']))) {
241
            foreach ($data['texts'] as $text) {
242
                $entity->translate($text->siteId)->setCategoryNumber($text->categoryNumber);
243
                $entity->translate($text->siteId)->setDescription($text->description);
244
                $entity->translate($text->siteId)->setExternalId($text->id);
245
                $entity->translate($text->siteId)->setHidden($text->hidden);
246
                $entity->translate($text->siteId)->setHiddenMobile($text->hiddenMobile);
247
                $entity->translate($text->siteId)->setIcon($text->icon);
248
                $entity->translate($text->siteId)->setImage($text->image);
249
                $entity->translate($text->siteId)->setKeywords($text->Keywords);
250
                $entity->translate($text->siteId)->setLink($text->link);
251
                $entity->translate($text->siteId)->setMetaDescription($text->metaDescription);
252
                $entity->translate($text->siteId)->setName($text->name);
253
                $entity->translate($text->siteId)->setSiteId($text->siteId);
254
                $entity->translate($text->siteId)->setSortOrder($text->sortOrder);
255
                $entity->translate($text->siteId)->setString($text->string);
256
                $entity->translate($text->siteId)->setTitle($text->title);
257
                $entity->translate($text->siteId)->setUrlname($text->urlname);
258
259
                $entity->mergeNewTranslations();
260
            }
261
        }
262
        */
263
    }
264
265
    /**
266
     * @return int
267
     */
268
    public function getId(): int
269
    {
270
        return (int)$this->id;
271
    }
272
273
    /**
274
     * @param int $id
275
     * @return Category
276
     */
277
    public function setId(int $id)
278
    {
279
        $this->id = $id;
280
        return $this;
281
    }
282
283
    /**
284
     * @return int
285
     */
286
    public function getExternalId(): int
287
    {
288
        return (int)$this->externalId;
289
    }
290
291
    /**
292
     * @param int $externalId
293
     * @return Category
294
     */
295
    public function setExternalId(int $externalId)
296
    {
297
        $this->externalId = $externalId;
298
        return $this;
299
    }
300
301
    /**
302
     * @return string
303
     */
304
    public function getNumber(): string
305
    {
306
        return (string)$this->number;
307
    }
308
309
    /**
310
     * @param string $number
311
     * @return Category
312
     */
313
    public function setNumber(string $number)
314
    {
315
        $this->number = $number;
316
        return $this;
317
    }
318
319
    /**
320
     * @return null|string
321
     */
322
    public function getB2bGroupId()
323
    {
324
        return $this->b2bGroupId;
325
    }
326
327
    /**
328
     * @param null|string $b2bGroupId
329
     * @return Category
330
     */
331
    public function setB2bGroupId($b2bGroupId)
332
    {
333
        $this->b2bGroupId = $b2bGroupId;
334
        return $this;
335
    }
336
337
    /**
338
     * @return \DateTimeImmutable|null
339
     */
340
    public function getCreatedDate()
341
    {
342
        return $this->createdDate;
343
    }
344
345
    /**
346
     * @param \DateTimeImmutable|null $createdDate
347
     * @return Category
348
     */
349
    public function setCreatedDate($createdDate)
350
    {
351
        $this->createdDate = $createdDate;
352
        return $this;
353
    }
354
355
    /**
356
     * @return int|null
357
     */
358
    public function getCustomInfoLayout()
359
    {
360
        return $this->customInfoLayout;
361
    }
362
363
    /**
364
     * @param int|null $customInfoLayout
365
     * @return Category
366
     */
367
    public function setCustomInfoLayout($customInfoLayout)
368
    {
369
        $this->customInfoLayout = $customInfoLayout;
370
        return $this;
371
    }
372
373
    /**
374
     * @return int|null
375
     */
376
    public function getCustomListLayout()
377
    {
378
        return $this->customListLayout;
379
    }
380
381
    /**
382
     * @param int|null $customListLayout
383
     * @return Category
384
     */
385
    public function setCustomListLayout($customListLayout)
386
    {
387
        $this->customListLayout = $customListLayout;
388
        return $this;
389
    }
390
391
    /**
392
     * @return int|null
393
     */
394
    public function getDefaultParentId()
395
    {
396
        return $this->defaultParentId;
397
    }
398
399
    /**
400
     * @param int|null $defaultParentId
401
     * @return Category
402
     */
403
    public function setDefaultParentId($defaultParentId)
404
    {
405
        $this->defaultParentId = $defaultParentId;
406
        return $this;
407
    }
408
409
    /**
410
     * @return \DateTimeImmutable|null
411
     */
412
    public function getEditedDate()
413
    {
414
        return $this->editedDate;
415
    }
416
417
    /**
418
     * @param \DateTimeImmutable|null $editedDate
419
     * @return Category
420
     */
421
    public function setEditedDate($editedDate)
422
    {
423
        $this->editedDate = $editedDate;
424
        return $this;
425
    }
426
427
    /**
428
     * @return int|null
429
     */
430
    public function getInfoLayout()
431
    {
432
        return $this->infoLayout;
433
    }
434
435
    /**
436
     * @param int|null $infoLayout
437
     * @return Category
438
     */
439
    public function setInfoLayout($infoLayout)
440
    {
441
        $this->infoLayout = $infoLayout;
442
        return $this;
443
    }
444
445
    /**
446
     * @return int|null
447
     */
448
    public function getInternalId()
449
    {
450
        return $this->internalId;
451
    }
452
453
    /**
454
     * @param int|null $internalId
455
     * @return Category
456
     */
457
    public function setInternalId($internalId)
458
    {
459
        $this->internalId = $internalId;
460
        return $this;
461
    }
462
463
    /**
464
     * @return int|null
465
     */
466
    public function getListLayout()
467
    {
468
        return $this->listLayout;
469
    }
470
471
    /**
472
     * @param int|null $listLayout
473
     * @return Category
474
     */
475
    public function setListLayout($listLayout)
476
    {
477
        $this->listLayout = $listLayout;
478
        return $this;
479
    }
480
481
    /**
482
     * @return bool|null
483
     */
484
    public function getModified()
485
    {
486
        return $this->modified;
487
    }
488
489
    /**
490
     * @param bool|null $modified
491
     * @return Category
492
     */
493
    public function setModified($modified)
494
    {
495
        $this->modified = $modified;
496
        return $this;
497
    }
498
499
    /**
500
     * @return array|null
501
     */
502
    public function getParentIdList()
503
    {
504
        return $this->parentIdList;
505
    }
506
507
    /**
508
     * @param array|null $parentIdList
509
     * @return Category
510
     */
511
    public function setParentIdList($parentIdList)
512
    {
513
        $this->parentIdList = $parentIdList;
514
        return $this;
515
    }
516
517
    /**
518
     * @return array|null
519
     */
520
    public function getSegmentIdList()
521
    {
522
        return $this->segmentIdList;
523
    }
524
525
    /**
526
     * @param array|null $segmentIdList
527
     * @return Category
528
     */
529
    public function setSegmentIdList($segmentIdList)
530
    {
531
        $this->segmentIdList = $segmentIdList;
532
        return $this;
533
    }
534
535
    /**
536
     * @return ArrayCollection|Category[]
537
     */
538
    public function getChildrenCategories()
539
    {
540
        return $this->childrenCategories;
541
    }
542
543
    /**
544
     * @param ArrayCollection|Category[] $childrenCategories
545
     * @return Category
546
     */
547
    public function setChildrenCategories($childrenCategories)
548
    {
549
        $this->childrenCategories = $childrenCategories;
550
        return $this;
551
    }
552
553
    /**
554
     * @return ArrayCollection|Category[]
555
     */
556
    public function getParentCategories()
557
    {
558
        return $this->parentCategories;
559
    }
560
561
    /**
562
     * @param ArrayCollection|Category[] $parentCategories
563
     * @return Category
564
     */
565
    public function setParentCategories($parentCategories)
566
    {
567
        $this->parentCategories = $parentCategories;
568
        return $this;
569
    }
570
571
    /**
572
     * @return Product[]
573
     */
574
    public function getProducts(): array
575
    {
576
        return $this->products;
577
    }
578
579
    /**
580
     * @param Product[] $products
581
     * @return Category
582
     */
583
    public function setProducts(array $products)
584
    {
585
        $this->products = $products;
586
        return $this;
587
    }
588
589
    /**
590
     * @return ArrayCollection|Segment[]
591
     */
592
    public function getSegments()
593
    {
594
        return $this->segments;
595
    }
596
597
    /**
598
     * @param ArrayCollection|Segment[] $segments
599
     * @return Category
600
     */
601
    public function setSegments($segments)
602
    {
603
        $this->segments = $segments;
604
        return $this;
605
    }
606
}
607