aboutsummaryrefslogtreecommitdiffstats
path: root/core/namespace/ItemFactory.php
blob: ba32cd5f0182fbdb419c0e7e29b759e43cb3f0ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
abstract class ItemFactory extends Factory {

	#===========================================================================
	# Build instance by ID
	#===========================================================================
	public static function build($itemID): Item {
		if(!$Instance = parent::fetchInstance($itemID)) {
			$Item = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Item';
			$Instance = parent::storeInstance($itemID, new $Item($itemID, \Application::getDatabase()));
		}

		return $Instance;
	}

	#===========================================================================
	# Build instance by slug
	#===========================================================================
	public static function buildBySlug($slug): Item {
		$Item = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Item';
		return self::buildByAttribute($Item::getByField('slug', $slug, \Application::getDatabase()));
	}

	#===========================================================================
	# Build instance by Attribute
	#===========================================================================
	public static function buildByAttribute(Attribute $Attribute) {
		if(!$Instance = parent::fetchInstance($Attribute->get('id'))) {
			$Item = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Item';
			$Instance = parent::storeInstance($Attribute->get('id'), new $Item($Attribute, \Application::getDatabase()));
		}

		return $Instance;
	}
}