1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\MediaBundle\PHPCR; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
17
|
|
|
use Sonata\MediaBundle\Model\Gallery; |
18
|
|
|
use Sonata\MediaBundle\Model\GalleryItemInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Bundle\MediaBundle\Document\BaseGallery. |
22
|
|
|
*/ |
23
|
|
|
abstract class BaseGallery extends Gallery |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $uuid; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->galleryItems = new ArrayCollection(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get universal unique id. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getUuid() |
44
|
|
|
{ |
45
|
|
|
return $this->uuid; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
public function addGalleryItem(GalleryItemInterface $galleryItem): void |
52
|
|
|
{ |
53
|
|
|
$galleryItem->setGallery($this); |
54
|
|
|
|
55
|
|
|
// set nodename of GalleryItem |
56
|
|
|
if (!$galleryItem->getNodename()) { |
|
|
|
|
57
|
|
|
$galleryItem->setNodename( |
|
|
|
|
58
|
|
|
'media'.($this->galleryItems->count() + 1) |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$this->galleryItems->set($galleryItem->getNodename(), $galleryItem); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Pre persist method. |
67
|
|
|
*/ |
68
|
|
|
public function prePersist(): void |
69
|
|
|
{ |
70
|
|
|
$this->createdAt = new \DateTime(); |
71
|
|
|
$this->updatedAt = new \DateTime(); |
72
|
|
|
|
73
|
|
|
$this->reorderGalleryItems(); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Pre Update method. |
78
|
|
|
*/ |
79
|
|
|
public function preUpdate(): void |
80
|
|
|
{ |
81
|
|
|
$this->updatedAt = new \DateTime(); |
82
|
|
|
|
83
|
|
|
$this->reorderGalleryItems(); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: