Quellcode durchsuchen

Evolution #738: Playlist

Sevajol Bastien vor 11 Jahren
Ursprung
Commit
21a91cc56c

+ 238 - 0
src/Muzich/CoreBundle/Entity/Playlist.php Datei anzeigen

@@ -0,0 +1,238 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Entity;
4
+
5
+use Doctrine\ORM\Mapping as ORM;
6
+use Gedmo\Mapping\Annotation as Gedmo;
7
+use Symfony\Component\Validator\Constraints as Assert;
8
+use \Doctrine\Common\Collections\ArrayCollection;
9
+use \Doctrine\Common\Collections\Collection;
10
+
11
+use Muzich\CoreBundle\lib\Collection\ElementCollectionManager;
12
+use Muzich\CoreBundle\lib\Collection\TagCollectionManager;
13
+
14
+/**
15
+ * @ORM\Entity
16
+ */
17
+class Playlist
18
+{
19
+  
20
+  /**
21
+   * @ORM\Id
22
+   * @ORM\Column(type="integer")
23
+   * @ORM\GeneratedValue(strategy="AUTO")
24
+   * @var type int
25
+   */
26
+  protected $id;
27
+  
28
+  /**
29
+   * @ORM\Column(type="string", length=128, unique=false)
30
+   * @Assert\NotBlank()
31
+   * @Assert\Length(min = 3, max = 64)
32
+   * @var type string
33
+   */
34
+  protected $name;
35
+  
36
+  /**
37
+   * @ORM\ManyToOne(targetEntity="User", inversedBy="playlists_owneds")
38
+   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
39
+   */
40
+  protected $owner;
41
+  
42
+  /**
43
+   * @ORM\ManyToOne(targetEntity="Playlist", inversedBy="copys")
44
+   * @ORM\JoinColumn(name="copied_id", referencedColumnName="id")
45
+   */
46
+  protected $copied;
47
+  
48
+  /**
49
+   * @ORM\OneToMany(targetEntity="Playlist", mappedBy="copied")
50
+   */
51
+  protected $copys;
52
+  
53
+  /**
54
+   * @ORM\Column(type="boolean")
55
+   */
56
+  protected $public = true;
57
+  
58
+  /**
59
+   * @ORM\OneToMany(targetEntity="UserPlaylistPicked", mappedBy="user")
60
+   */
61
+  protected $user_playlists_pickeds;
62
+  
63
+  /**
64
+   * @ORM\Column(type="text", unique=false, nullable=true)
65
+   */
66
+  private $tags;
67
+  
68
+  /**
69
+   * @ORM\Column(type="text", unique=false, nullable=true)
70
+   */
71
+  private $elements;
72
+  
73
+  public function __construct()
74
+  {
75
+    $this->user_playlists_pickeds = new ArrayCollection();
76
+    $this->copys = new ArrayCollection();
77
+    $this->tags = json_encode(array());
78
+    $this->elements = json_encode(array());
79
+  }
80
+  
81
+  public function getId()
82
+  {
83
+    return $this->id;
84
+  }
85
+  
86
+  public function getName()
87
+  {
88
+    return $this->name;
89
+  }
90
+  
91
+  public function setName($name)
92
+  {
93
+    $this->name = $name;
94
+  }
95
+  
96
+  /** @return User */
97
+  public function getOwner()
98
+  {
99
+    return $this->owner;
100
+  }
101
+  
102
+  public function setOwner(User $owner)
103
+  {
104
+    $this->owner = $owner;
105
+  }
106
+  
107
+  public function isPublic()
108
+  {
109
+    return ($this->public)?true:false;
110
+  }
111
+  
112
+  public function setPublic($public)
113
+  {
114
+    ($public) ? $this->public = true : $this->public = false;
115
+  }
116
+  
117
+  /** @return Collection */
118
+  public function getUserPlaylistsPickeds()
119
+  {
120
+    return $this->user_playlists_pickeds;
121
+  }
122
+  
123
+  public function setUserPlaylistsPickeds(Collection $user_playlists_pickeds = null)
124
+  {
125
+    $this->user_playlists_pickeds = $user_playlists_pickeds;
126
+  }
127
+  
128
+  public function addPickedUser(User $user)
129
+  {
130
+    $this->addUserPlaylistPicked($user);
131
+  }
132
+  
133
+  public function addUserPlaylistPicked(User $user)
134
+  {
135
+    // TODO
136
+    $user_playlist_picked = new UserPlaylistPicked();
137
+    $user_playlist_picked->setUser($user);
138
+    $user_playlist_picked->setPlaylist($this);
139
+    
140
+    $this->getUserPlaylistsPickeds()->add($user_playlist_picked);
141
+  }
142
+  
143
+  public function removePickedUser(User $user)
144
+  {
145
+    // TODO: à coder
146
+  }
147
+  
148
+  /** @return array */
149
+  public function getTags()
150
+  {
151
+    return json_decode($this->tags, true);
152
+  }
153
+  
154
+  /** @param tags array */
155
+  public function setTags($tags)
156
+  {
157
+    $this->tags = json_encode($tags);
158
+  }
159
+  
160
+  public function addTag(Tag $tag)
161
+  {
162
+    $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
163
+    $tags_manager->add($tag);
164
+    $this->tags = json_encode($tags_manager->getContent(), true);
165
+  }
166
+  
167
+  public function removeTag(Tag $tag)
168
+  {
169
+    $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
170
+    $tags_manager->remove($tag);
171
+    $this->tags = json_encode($tags_manager->getContent(), true);
172
+  }
173
+  
174
+  public function getTagsIds()
175
+  {
176
+    $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
177
+    return $tags_manager->getAttributes(TagCollectionManager::ATTRIBUTE_ID);
178
+  }
179
+  
180
+  /** @return array */
181
+  public function getElements()
182
+  {
183
+    return json_decode($this->elements, true);
184
+  }
185
+  
186
+  /** @param tags array */
187
+  public function setElements($elements)
188
+  {
189
+    $this->elements = json_encode($elements);
190
+  }
191
+  
192
+  public function addElement(Element $element)
193
+  {
194
+    $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
195
+    $elements_manager->add($element);
196
+    $this->elements = json_encode($elements_manager->getContent(), true);
197
+  }
198
+  
199
+  public function removeElement(Element $element)
200
+  {
201
+    $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
202
+    $elements_manager->remove($element);
203
+    $this->elements = json_encode($elements_manager->getContent(), true);
204
+  }
205
+  
206
+  public function getElementsIds()
207
+  {
208
+    $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
209
+    return $elements_manager->getAttributes(ElementCollectionManager::ATTRIBUTE_ID);
210
+  }
211
+  
212
+  public function haveElement(Element $element)
213
+  {
214
+    $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
215
+    return $elements_manager->have($element);
216
+  }
217
+  
218
+  public function setCopied(Playlist $copied)
219
+  {
220
+    $this->copied = $copied;
221
+  }
222
+  
223
+  public function getCopied()
224
+  {
225
+    return $this->copied;
226
+  }
227
+  
228
+  public function addCopy(Playlist $playlist)
229
+  {
230
+    $this->copys->add($playlist);
231
+  }
232
+  
233
+  public function getCopys()
234
+  {
235
+    return $this->copys;
236
+  }
237
+  
238
+}

+ 57 - 0
src/Muzich/CoreBundle/Entity/User.php Datei anzeigen

@@ -5,6 +5,7 @@ namespace Muzich\CoreBundle\Entity;
5 5
 use FOS\UserBundle\Entity\User as BaseUser;
6 6
 use Doctrine\ORM\Mapping as ORM;
7 7
 use \Doctrine\Common\Collections\ArrayCollection;
8
+use \Doctrine\Common\Collections\Collection;
8 9
 use Gedmo\Mapping\Annotation as Gedmo;
9 10
 use Doctrine\ORM\EntityManager;
10 11
 use Muzich\CoreBundle\Entity\UsersTagsFavorites;
@@ -83,6 +84,11 @@ class User extends BaseUser
83 84
   protected $tags_favorites;
84 85
   
85 86
   /**
87
+   * @ORM\OneToMany(targetEntity="UserPlaylistPicked", mappedBy="user")
88
+   */
89
+  protected $user_playlists_pickeds;
90
+  
91
+  /**
86 92
    * Cet attribut contient les enregistrements UsersElementsFavorites lié 
87 93
    * a cet utilisateur dans le cadre des éléments Favoris.
88 94
    * 
@@ -134,6 +140,11 @@ class User extends BaseUser
134 140
   protected $groups_owned;
135 141
   
136 142
   /**
143
+   * @ORM\OneToMany(targetEntity="Playlist", mappedBy="owner")
144
+   */
145
+  protected $playlists_owneds;
146
+  
147
+  /**
137 148
    * @ORM\Column(type="integer", nullable=true)
138 149
    * @var int 
139 150
    */
@@ -297,6 +308,8 @@ class User extends BaseUser
297 308
     $this->followed_groups = new ArrayCollection();
298 309
     $this->groups = new ArrayCollection();
299 310
     $this->groups_owned = new ArrayCollection();
311
+    $this->user_playlists_pickeds = new ArrayCollection();
312
+    $this->playlists_owneds = new ArrayCollection();
300 313
     $this->help_tour = json_encode(array(
301 314
       self::HELP_TOUR_HOME => true
302 315
     ));
@@ -1201,4 +1214,48 @@ class User extends BaseUser
1201 1214
     }
1202 1215
   }
1203 1216
   
1217
+  public function getUserPlaylistsPickeds()
1218
+  {
1219
+    return $this->user_playlists_pickeds;
1220
+  }
1221
+  
1222
+  public function setUserPlaylistsPickeds(Collection $user_playlists_pickeds)
1223
+  {
1224
+    $this->user_playlists_pickeds = $user_playlists_pickeds;
1225
+  }
1226
+  
1227
+  public function havePlaylistPicked(Playlist $playlist)
1228
+  {
1229
+    foreach ($this->getPickedsPlaylists() as $playlist_picked)
1230
+    {
1231
+      if ($playlist_picked->getId() == $playlist->getId())
1232
+      {
1233
+        return true;
1234
+      }
1235
+    }
1236
+    
1237
+    return false;
1238
+  }
1239
+  
1240
+  public function getPickedsPlaylists()
1241
+  {
1242
+    $playlists = new ArrayCollection();
1243
+    foreach ($this->user_playlists_pickeds as $user_playlist_picked)
1244
+    {
1245
+      $playlists->add($user_playlist_picked->getPlaylist());
1246
+    }
1247
+    
1248
+    return $playlists;
1249
+  }
1250
+  
1251
+  public function getPlaylistsOwneds()
1252
+  {
1253
+    return $this->playlists_owneds;
1254
+  }
1255
+  
1256
+  public function setPlaylistsOwneds(Collection $playlists_owneds)
1257
+  {
1258
+    $this->playlists_owneds = $playlists_owneds;
1259
+  }
1260
+  
1204 1261
 }

+ 68 - 0
src/Muzich/CoreBundle/Entity/UserPlaylistPicked.php Datei anzeigen

@@ -0,0 +1,68 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Entity;
4
+
5
+use Doctrine\ORM\Mapping as ORM;
6
+
7
+/**
8
+ * @ORM\Entity
9
+ */
10
+class UserPlaylistPicked
11
+{
12
+  
13
+  /**
14
+   * @ORM\Id
15
+   * @ORM\Column(type="integer")
16
+   * @ORM\GeneratedValue(strategy="AUTO")
17
+   * @var type int
18
+   */
19
+  protected $id;
20
+  
21
+  /**
22
+   * @ORM\ManyToOne(targetEntity="User", inversedBy="user_playlists_pickeds")
23
+   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
24
+   */
25
+  protected $user;
26
+  
27
+  /**
28
+   * @ORM\ManyToOne(targetEntity="Tag", inversedBy="users_favorites")
29
+   * @ORM\JoinColumn(name="tag_id", referencedColumnName="id", onDelete="CASCADE")
30
+   */
31
+  protected $playlist;
32
+  
33
+  
34
+  /**  @return integer */
35
+  public function getId()
36
+  {
37
+      return $this->id;
38
+  }
39
+
40
+  public function setUser(User $user)
41
+  {
42
+    $this->user = $user;
43
+  }
44
+
45
+  /** @return User */
46
+  public function getUser()
47
+  {
48
+    return $this->user;
49
+  }
50
+  
51
+  public function setPlaylist(Playlist $playlist)
52
+  {
53
+    $this->playlist = $playlist;
54
+  }
55
+
56
+  /** @return Playlist */
57
+  public function getPlaylist()
58
+  {
59
+    return $this->playlist;
60
+  }
61
+  
62
+  public function init(User $user, Playlist $playlist)
63
+  {
64
+    $this->setUser($user);
65
+    $this->setPlaylist($playlist);
66
+  }
67
+  
68
+}

+ 95 - 0
src/Muzich/CoreBundle/Managers/PlaylistManager.php Datei anzeigen

@@ -0,0 +1,95 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Managers;
4
+
5
+use Doctrine\ORM\EntityManager;
6
+use Muzich\CoreBundle\Entity\Playlist;
7
+use Muzich\CoreBundle\Entity\User;
8
+use Muzich\CoreBundle\Entity\UserPlaylistPicked;
9
+use \Doctrine\Common\Collections\ArrayCollection;
10
+
11
+class PlaylistManager
12
+{
13
+  
14
+  protected $entity_manager;
15
+  protected $user;
16
+  
17
+  public function __construct(EntityManager $entity_manager)
18
+  {
19
+    $this->entity_manager = $entity_manager;
20
+  }
21
+  
22
+  public function findOnePlaylistWithId($playlist_id, User $user = null)
23
+  {
24
+    $query_builder = $this->entity_manager->createQueryBuilder()
25
+      ->select('p')
26
+      ->from('MuzichCoreBundle:Playlist', 'p')
27
+      ->where('p.id = :playlist_id')
28
+    ;
29
+    
30
+    if ($user)
31
+    {
32
+      $query_builder->andWhere('p.public = 1 OR p.owner = :user_id');
33
+    }
34
+    else
35
+    {
36
+      $query_builder->andWhere('p.public = 1');
37
+    }
38
+    
39
+    return $query_builder->getWuery()->getResult();
40
+  }
41
+  
42
+  public function getNewPlaylist(User $owner)
43
+  {
44
+    $playlist = new Playlist();
45
+    $playlist->setOwner($owner);
46
+    return $playlist;
47
+  }
48
+  
49
+  public function addPickedPlaylistToUser(User $user, Playlist $playlist)
50
+  {
51
+    if (!$user->havePlaylistPicked($playlist))
52
+    {
53
+      $user_playlist_picked = new UserPlaylistPicked();
54
+      $user_playlist_picked->init($user, $playlist);
55
+      $user->getUserPlaylistsPickeds()->add($user_playlist_picked);
56
+      $this->entity_manager->persist($user);
57
+      $this->entity_manager->persist($user_playlist_picked);
58
+    }
59
+  }
60
+  
61
+  public function removePickedPlaylistToUser(User $user, Playlist $playlist)
62
+  {
63
+    if ($user->havePlaylistPicked($playlist))
64
+    {
65
+      $user_playlists_pickeds = new ArrayCollection();
66
+      foreach ($user->getUserPlaylistsPickeds() as $user_playlist_picked)
67
+      {
68
+        if ($user_playlist_picked->getPlaylist()->getId() == $playlist->getId())
69
+        {
70
+          $this->entity_manager->remove($user_playlist_picked);
71
+        }
72
+        else
73
+        {
74
+          $user_playlists_pickeds->add($user_playlist_picked);
75
+        }
76
+      }
77
+      $user->setUserPlaylistsPickeds($user_playlists_pickeds);
78
+      $this->entity_manager->persist($user);
79
+    }
80
+  }
81
+  
82
+  public function copyPlaylist(User $user, Playlist $playlist)
83
+  {
84
+    $playlist_copied = new Playlist();
85
+    $playlist_copied->setOwner($user);
86
+    $playlist_copied->setTags($playlist->getTags());
87
+    $playlist_copied->setElements($playlist->getElements());
88
+    $playlist_copied->setCopied($playlist);
89
+    $playlist->addCopy($playlist_copied);
90
+    $user->getPlaylistsOwneds()->add($playlist_copied);
91
+    $this->entity_manager->persist($playlist_copied);
92
+    $this->entity_manager->persist($user);
93
+  }
94
+  
95
+}

+ 145 - 0
src/Muzich/CoreBundle/Tests/Controller/PlaylistTest.php Datei anzeigen

@@ -0,0 +1,145 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Controller;
4
+
5
+use Muzich\CoreBundle\lib\FunctionalTest;
6
+use Muzich\CoreBundle\Managers\PlaylistManager;
7
+use Muzich\CoreBundle\Entity\Element;
8
+use Muzich\CoreBundle\Entity\Playlist;
9
+
10
+class ElementTest extends Element
11
+{
12
+  public function __construct($id, $name)
13
+  {
14
+    $this->id = $id;
15
+    $this->name = $name;
16
+  }
17
+}
18
+
19
+class PlaylistTest extends Playlist
20
+{
21
+  public function setId($id)
22
+  {
23
+    $this->id = $id;
24
+  }
25
+}
26
+
27
+class NoPassTest extends FunctionalTest
28
+{
29
+  
30
+  protected $users = array();
31
+  protected $elements = array();
32
+  protected $playlist_manager = null;
33
+  protected $playlists = array();
34
+  
35
+  protected function init()
36
+  {
37
+    $this->client = self::createClient();
38
+    $this->users['bux'] = $this->getUser('bux');
39
+    $this->users['bob'] = $this->getUser('bob');
40
+    
41
+    $this->elements[1] = new ElementTest(1, 'Element 1');
42
+    $this->elements[2] = new ElementTest(2, 'Element 2');
43
+    $this->elements[3] = new ElementTest(3, 'Element 3');
44
+    $this->elements[4] = new ElementTest(4, 'Element 4');
45
+    
46
+    $this->playlist_manager = new PlaylistManager($this->getEntityManager());
47
+    
48
+    $this->playlists['bux'] = new PlaylistTest();
49
+    $this->playlists['bux']->setOwner($this->users['bux']);
50
+    $this->playlists['bux']->setId(1);
51
+  }
52
+  
53
+  public function testPlaylistCreationAndPick()
54
+  {
55
+    $this->init();
56
+    
57
+    $this->addElementsToBuxPlaylist(array($this->elements[1], $this->elements[2], $this->elements[3], $this->elements[4]));
58
+    $this->checkElementsInBuxPlaylist(array($this->elements[1], $this->elements[2], $this->elements[3], $this->elements[4]));
59
+    $this->removeAnElementFormBuxPlaylist($this->elements[3]);
60
+    $this->checkElementsInBuxPlaylist(array($this->elements[1], $this->elements[2], $this->elements[4]));
61
+    
62
+    $this->bobPickBuxPlaylist();
63
+    $this->checkBobPickedPlaylists(array($this->playlists['bux']));
64
+    $this->bobRemoveBuxPickedPlaylist();
65
+    $this->checkBobPickedPlaylists(array());
66
+    $this->bobRemoveBuxPickedPlaylist();
67
+    $this->bobCopyBuxPlaylist();
68
+    $this->checkCopyedPlaylist();
69
+  }
70
+  
71
+  protected function addElementsToBuxPlaylist($elements)
72
+  {
73
+    foreach ($elements as $element)
74
+    {
75
+      $this->playlists['bux']->addElement($element);
76
+    }
77
+  }
78
+  
79
+  protected function checkElementsInBuxPlaylist($elements)
80
+  {
81
+    $this->assertEquals(count($elements), count($this->playlists['bux']->getElements()));
82
+    foreach ($elements as $element)
83
+    {
84
+      $this->playlists['bux']->haveElement($element);
85
+    }
86
+  }
87
+  
88
+  protected function removeAnElementFormBuxPlaylist($element)
89
+  {
90
+    $this->playlists['bux']->removeElement($element);
91
+  }
92
+  
93
+  protected function bobPickBuxPlaylist()
94
+  {
95
+    $this->playlist_manager->addPickedPlaylistToUser($this->users['bob'], $this->playlists['bux']);
96
+  }
97
+  
98
+  protected function checkBobPickedPlaylists($playlists)
99
+  {
100
+    $this->assertEquals(count($playlists), count($this->users['bob']->getPickedsPlaylists()));
101
+    foreach ($playlists as $playlist)
102
+    {
103
+      $this->assertTrue($this->playlistIsInPickedPlaylists($playlist, $this->users['bob']->getPickedsPlaylists()));
104
+    }
105
+  }
106
+  
107
+  protected function playlistIsInPickedPlaylists($playlist_searched, $playlists)
108
+  {
109
+    foreach ($playlists as $playlist)
110
+    {
111
+      if ($playlist->getId() == $playlist_searched->getId())
112
+      {
113
+        return true;
114
+      }
115
+    }
116
+    
117
+    return false;
118
+  }
119
+  
120
+  protected function bobRemoveBuxPickedPlaylist()
121
+  {
122
+    $this->playlist_manager->removePickedPlaylistToUser($this->users['bob'], $this->playlists['bux']);
123
+  }
124
+  
125
+  protected function bobCopyBuxPlaylist()
126
+  {
127
+    $this->playlist_manager->copyPlaylist($this->users['bob'], $this->playlists['bux']);
128
+  }
129
+  
130
+  protected function checkCopyedPlaylist()
131
+  {
132
+    $this->assertEquals(1, count($this->users['bob']->getPlaylistsOwneds()));
133
+    $bob_playlists = $this->users['bob']->getPlaylistsOwneds();
134
+    
135
+    $bux_playlist_copys = $this->playlists['bux']->getCopys();
136
+    $this->assertCount(1, $bux_playlist_copys);
137
+    $this->assertEquals($bux_playlist_copys[0], $bob_playlists[0]);
138
+    
139
+    foreach (array($this->elements[1], $this->elements[2], $this->elements[4]) as $element)
140
+    {
141
+      $this->assertTrue($bob_playlists[0]->haveElement($element));
142
+    }
143
+  }
144
+  
145
+}

+ 49 - 0
src/Muzich/CoreBundle/Tests/Utils/CollectionManagerTest.php Datei anzeigen

@@ -0,0 +1,49 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Tests\Utils;
4
+
5
+use Muzich\CoreBundle\lib\Collection\TagCollectionManager;
6
+use Muzich\CoreBundle\Entity\Tag;
7
+
8
+class TagTest extends Tag
9
+{
10
+  public function __construct($id, $name)
11
+  {
12
+    $this->id = $id;
13
+    $this->name = $name;
14
+  }
15
+}
16
+
17
+class CollectionManagerTest extends \PHPUnit_Framework_TestCase
18
+{
19
+  
20
+  public function testTagCollectionManager()
21
+  {
22
+    $tags_collection_manager = new TagCollectionManager(array());
23
+    $this->assertEquals('[]', json_encode($tags_collection_manager->getContent(), true));
24
+    
25
+    $tag_pop = new TagTest(1, 'Pop');
26
+    $tags_collection_manager->add($tag_pop);
27
+    $this->assertEquals('[{"Id":"1","Name":"Pop"}]', json_encode($tags_collection_manager->getContent(), true));
28
+    
29
+    $tag_rock = new TagTest(2, 'Rock');
30
+    $tags_collection_manager->add($tag_rock);
31
+    $this->assertEquals('[{"Id":"1","Name":"Pop"},{"Id":"2","Name":"Rock"}]', json_encode($tags_collection_manager->getContent(), true));
32
+    
33
+    $this->assertTrue($tags_collection_manager->have($tag_rock));
34
+    
35
+    $tags_collection_manager->add($tag_rock);
36
+    $this->assertEquals('[{"Id":"1","Name":"Pop"},{"Id":"2","Name":"Rock"}]', json_encode($tags_collection_manager->getContent(), true));
37
+    
38
+    $tag_metal = new TagTest(3, 'Metal');
39
+    $tags_collection_manager->add($tag_metal);
40
+    $this->assertEquals('[{"Id":"1","Name":"Pop"},{"Id":"2","Name":"Rock"},{"Id":"3","Name":"Metal"}]', json_encode($tags_collection_manager->getContent(), true));
41
+    
42
+    $this->assertEquals(array('1', '2', '3'), $tags_collection_manager->getAttributes(TagCollectionManager::ATTRIBUTE_ID));
43
+    $this->assertEquals(array('Pop', 'Rock', 'Metal'), $tags_collection_manager->getAttributes(TagCollectionManager::ATTRIBUTE_NAME));
44
+    
45
+    $tags_collection_manager->remove($tag_pop);
46
+    $this->assertEquals('[{"Id":"2","Name":"Rock"},{"Id":"3","Name":"Metal"}]', json_encode($tags_collection_manager->getContent(), true));
47
+  }
48
+  
49
+}

+ 91 - 0
src/Muzich/CoreBundle/lib/Collection/CollectionManager.php Datei anzeigen

@@ -0,0 +1,91 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\lib\Collection;
4
+
5
+use \Doctrine\Common\Collections\ArrayCollection;
6
+
7
+abstract class CollectionManager
8
+{
9
+  
10
+  protected $content;
11
+  protected $schema = array();
12
+  protected $object_reference_attribute;
13
+  
14
+  public function __construct($content)
15
+  {
16
+    if (!is_array($content))
17
+      throw new \Exception('Content must be array type !');
18
+    
19
+    if (!count($this->schema))
20
+      throw new \Exception('Schema must be defined in child class !');
21
+    
22
+    if (!count($this->object_reference_attribute))
23
+      throw new \Exception('Object reference attribute must be defined in child class !');
24
+    
25
+    $this->content = $content;
26
+  }
27
+  
28
+  public function add($object)
29
+  {
30
+    if (!$this->have($object))
31
+    {
32
+      $content_line = array();
33
+      foreach ($this->schema as $attribute)
34
+      {
35
+        $method_name = 'get' . $attribute;
36
+        $content_line[$attribute] = (string)$object->$method_name();
37
+      }
38
+      
39
+      $this->content[] = $content_line;
40
+    }
41
+  }
42
+  
43
+  public function have($object)
44
+  {
45
+    $method_name = 'get' . $this->object_reference_attribute;
46
+    foreach ($this->content as $content_line)
47
+    {
48
+      if ($object->$method_name() == $content_line[$this->object_reference_attribute])
49
+      {
50
+        return true;
51
+      }
52
+    }
53
+    
54
+    return false;
55
+  }
56
+  
57
+  public function remove($object)
58
+  {
59
+    $new_content = array();
60
+    $method_name = 'get' . $this->object_reference_attribute;
61
+    foreach ($this->content as $content_line)
62
+    {
63
+      if ($object->$method_name() != $content_line[$this->object_reference_attribute])
64
+      {
65
+        $new_content[] = $content_line;
66
+      }
67
+    }
68
+    
69
+    $this->content = $new_content;
70
+  }
71
+  
72
+  public function getAttributes($attribute)
73
+  {
74
+    if (!in_array($attribute, $this->schema))
75
+      throw new \Exception('This attribute is unknow !');
76
+    
77
+    $attributes = array();
78
+    foreach ($this->content as $content_line)
79
+    {
80
+      $attributes[] = $content_line[$attribute];
81
+    }
82
+    
83
+    return $attributes;
84
+  }
85
+  
86
+  public function getContent()
87
+  {
88
+    return $this->content;
89
+  }
90
+  
91
+}

+ 20 - 0
src/Muzich/CoreBundle/lib/Collection/ElementCollectionManager.php Datei anzeigen

@@ -0,0 +1,20 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\lib\Collection;
4
+
5
+use Muzich\CoreBundle\Entity\Element;
6
+
7
+class ElementCollectionManager extends CollectionManager
8
+{
9
+  
10
+  const ATTRIBUTE_ID   = 'Id';
11
+  const ATTRIBUTE_NAME = 'Name';
12
+  
13
+  protected $schema = array(
14
+    self::ATTRIBUTE_ID,
15
+    self::ATTRIBUTE_NAME
16
+  );
17
+  
18
+  protected $object_reference_attribute = self::ATTRIBUTE_ID;
19
+  
20
+}

+ 20 - 0
src/Muzich/CoreBundle/lib/Collection/TagCollectionManager.php Datei anzeigen

@@ -0,0 +1,20 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\lib\Collection;
4
+
5
+use Muzich\CoreBundle\Entity\Tag;
6
+
7
+class TagCollectionManager extends CollectionManager
8
+{
9
+  
10
+  const ATTRIBUTE_ID   = 'Id';
11
+  const ATTRIBUTE_NAME = 'Name';
12
+  
13
+  protected $schema = array(
14
+    self::ATTRIBUTE_ID,
15
+    self::ATTRIBUTE_NAME
16
+  );
17
+  
18
+  protected $object_reference_attribute = self::ATTRIBUTE_ID;
19
+  
20
+}