public static function CreateOrFind($tagName) { $tagService = new TagService(); $tag = $tagService->getTagByName($tagName); if (!$tag) { $tag = new Models\Tag(); $tag->name = $tagName; $tag->save(); } return $tag; }
public function addTags($images, $tags, Models\User $user = null) { if (!is_array($images)) { $images = [$images]; } if (!is_array($tags)) { $tags = [$tags]; } // Text to Objects.. foreach ($tags as &$tag) { if (is_string($tag)) { $tag = TagService::CreateOrFind($tag); } } $imageTagLinks = []; foreach ($images as $image) { /** * @var $image Models\Image */ foreach ($tags as $tag) { /** * @var $tag Models\Tag */ $imageTagLinks[] = $this->addTag($image, $tag, $user); } } return $imageTagLinks; }
public function testTagImages() { $attackRabbitAsset = __DIR__ . "/../Assets/sample-1.jpg"; $attackRabbitMockUpload = $this->makeMockUpload($attackRabbitAsset); $attackRabbitImage = $this->imageService->uploadImage($this->testUser, $attackRabbitMockUpload); $carsAsset = __DIR__ . "/../Assets/sample-1.jpg"; $carsMockUpload = $this->makeMockUpload($carsAsset); $carsImage = $this->imageService->uploadImage($this->testUser, $carsMockUpload); $hompfCatAsset = __DIR__ . "/../Assets/sample-1.jpg"; $hompfCatMockUpload = $this->makeMockUpload($hompfCatAsset); $hompfCatImage = $this->imageService->uploadImage($this->testUser, $hompfCatMockUpload); $this->imageService->addTag($attackRabbitImage, TagService::CreateOrFind("rabbit")); $this->imageService->addTag($carsImage, TagService::CreateOrFind("cars")); $this->imageService->addTag($hompfCatImage, TagService::CreateOrFind("cat"), $this->testUser); $this->imageService->addTags([$attackRabbitImage, $carsImage, $hompfCatImage], "test"); $this->imageService->addTags([$attackRabbitImage, $carsImage, $hompfCatImage], ["test2"]); $this->imageService->addTags($attackRabbitImage, "test3"); }