idx
int64
0
60.3k
question
stringlengths
99
4.85k
target
stringlengths
5
718
100
protected function queryEagerLoadedRelationships ( array $ results , array $ eagerLoads ) : array { $ this -> eagerLoads = $ this -> parseRelations ( $ eagerLoads ) ; return $ this -> eagerLoadRelations ( $ results ) ; }
Launch queries on eager loaded relationships .
101
public function eagerLoadRelations ( array $ results ) : array { foreach ( $ this -> eagerLoads as $ name => $ constraints ) { if ( ! in_array ( $ name , $ this -> entityMap -> getRelationships ( ) ) ) { continue ; } if ( strpos ( $ name , '.' ) === false ) { $ results = $ this -> loadRelation ( $ results , $ name , $ constraints ) ; } } return $ results ; }
Eager load the relationships on a result set .
102
protected function loadRelation ( array $ results , string $ name , Closure $ constraints ) : array { $ relation = $ this -> getRelation ( $ name ) ; $ relation -> addEagerConstraints ( $ results ) ; call_user_func ( $ constraints , $ relation ) ; return $ relation -> match ( $ results , $ name ) ; }
Eagerly load the relationship on a set of entities .
103
protected function buildUnkeyedResultSet ( array $ results ) : array { $ builder = new EntityBuilder ( $ this -> mapper , array_keys ( $ this -> eagerLoads ) , $ this -> useCache ) ; return array_map ( function ( $ item ) use ( $ builder ) { return $ builder -> build ( $ item ) ; } , $ results ) ; }
Build a result set .
104
protected function buildKeyedResultSet ( array $ results , string $ primaryKey ) : array { $ builder = new EntityBuilder ( $ this -> mapper , array_keys ( $ this -> eagerLoads ) , $ this -> useCache ) ; $ keys = array_map ( function ( $ item ) use ( $ primaryKey ) { return $ item [ $ primaryKey ] ; } , $ results ) ; return array_combine ( $ keys , array_map ( function ( $ item ) use ( $ builder ) { return $ builder -> build ( $ item ) ; } , $ results ) ) ; }
Build a result set keyed by PK .
105
public function add ( array $ results ) { $ cachedResults = [ ] ; $ keyColumn = $ this -> entityMap -> getKeyName ( ) ; foreach ( $ results as $ result ) { $ id = $ result [ $ keyColumn ] ; $ cachedResults [ $ id ] = $ this -> rawResult ( $ result ) ; } if ( empty ( $ this -> cache ) ) { $ this -> cache = $ cachedResults ; } else { $ this -> mergeCacheResults ( $ cachedResults ) ; } }
Add an array of key = > attributes representing the initial state of loaded entities .
106
public function get ( string $ id = null ) : array { if ( $ this -> has ( $ id ) ) { return $ this -> cache [ $ id ] ; } return [ ] ; }
Retrieve initial attributes for a single entity .
107
protected function mergeCacheResults ( array $ results ) { foreach ( $ results as $ key => $ entity ) { $ this -> cache [ $ key ] = $ entity ; } }
Combine new result set with existing attributes in cache .
108
public function cacheLoadedRelationResult ( string $ key , string $ relation , $ results , Relationship $ relationship ) { if ( $ results instanceof EntityCollection ) { $ this -> cacheManyRelationResults ( $ key , $ relation , $ results , $ relationship ) ; } if ( $ results instanceof Mappable ) { $ this -> cacheSingleRelationResult ( $ key , $ relation , $ results , $ relationship ) ; } }
Cache Relation s query result for an entity .
109
protected function getCachedRelationship ( string $ relation , $ result , Relationship $ relationship ) { $ pivotColumns = $ relationship -> getPivotAttributes ( ) ; if ( ! array_key_exists ( $ relation , $ this -> pivotAttributes ) ) { $ this -> pivotAttributes [ $ relation ] = $ pivotColumns ; } $ wrapper = $ this -> factory -> make ( $ result ) ; $ hash = $ wrapper -> getEntityHash ( ) ; if ( count ( $ pivotColumns ) > 0 ) { $ pivotAttributes = [ ] ; foreach ( $ pivotColumns as $ column ) { $ pivot = $ wrapper -> getEntityAttribute ( 'pivot' ) ; $ pivotWrapper = $ this -> factory -> make ( $ pivot ) ; $ pivotAttributes [ $ column ] = $ pivotWrapper -> getEntityAttribute ( $ column ) ; } $ cachedRelationship = new CachedRelationship ( $ hash , $ pivotAttributes ) ; } else { $ cachedRelationship = new CachedRelationship ( $ hash ) ; } return $ cachedRelationship ; }
Create a cachedRelationship instance which will hold related entity s hash and pivot attributes if any .
110
protected function cacheManyRelationResults ( string $ parentKey , string $ relation , $ results , Relationship $ relationship ) { $ this -> cache [ $ parentKey ] [ $ relation ] = [ ] ; foreach ( $ results as $ result ) { $ cachedRelationship = $ this -> getCachedRelationship ( $ relation , $ result , $ relationship ) ; $ relatedHash = $ cachedRelationship -> getHash ( ) ; $ this -> cache [ $ parentKey ] [ $ relation ] [ $ relatedHash ] = $ cachedRelationship ; } }
Cache a many relationship .
111
protected function cacheSingleRelationResult ( string $ parentKey , string $ relation , $ result , Relationship $ relationship ) { $ this -> cache [ $ parentKey ] [ $ relation ] = $ this -> getCachedRelationship ( $ relation , $ result , $ relationship ) ; }
Cache a single relationship .
112
protected function getEntityHash ( InternallyMappable $ entity ) : string { $ class = $ entity -> getEntityClass ( ) ; $ mapper = Manager :: getMapper ( $ class ) ; $ keyName = $ mapper -> getEntityMap ( ) -> getKeyName ( ) ; return $ class . '.' . $ entity -> getEntityAttribute ( $ keyName ) ; }
Get Entity s Hash .
113
public function refresh ( Aggregate $ entity ) { $ this -> cache [ $ entity -> getEntityKeyValue ( ) ] = $ this -> transform ( $ entity ) ; }
Refresh the cache record for an aggregated entity after a write operation .
114
protected function transform ( Aggregate $ aggregatedEntity ) : array { $ baseAttributes = $ aggregatedEntity -> getRawAttributes ( ) ; $ relationAttributes = [ ] ; foreach ( $ this -> entityMap -> getSingleRelationships ( ) as $ relation ) { $ aggregates = $ aggregatedEntity -> getRelationship ( $ relation ) ; if ( count ( $ aggregates ) == 1 ) { $ related = $ aggregates [ 0 ] ; $ relationAttributes [ $ relation ] = new CachedRelationship ( $ related -> getEntityHash ( ) ) ; } if ( count ( $ aggregates ) > 1 ) { throw new MappingException ( "Single Relationship '$relation' contains several related entities" ) ; } } foreach ( $ this -> entityMap -> getManyRelationships ( ) as $ relation ) { $ aggregates = $ aggregatedEntity -> getRelationship ( $ relation ) ; $ relationAttributes [ $ relation ] = [ ] ; foreach ( $ aggregates as $ aggregate ) { $ relationAttributes [ $ relation ] [ ] = new CachedRelationship ( $ aggregate -> getEntityHash ( ) , $ aggregate -> getPivotAttributes ( ) ) ; } } return $ baseAttributes + $ relationAttributes ; }
Transform an Aggregated Entity into a cache record .
115
protected function getPivotValues ( string $ relation , InternallyMappable $ entity ) : array { $ values = [ ] ; $ entityAttributes = $ entity -> getEntityAttributes ( ) ; if ( array_key_exists ( $ relation , $ this -> pivotAttributes ) ) { foreach ( $ this -> pivotAttributes [ $ relation ] as $ attribute ) { if ( array_key_exists ( $ attribute , $ entityAttributes ) ) { $ values [ $ attribute ] = $ entity -> getEntityAttribute ( 'pivot' ) -> $ attribute ; } } } return $ values ; }
Get pivot attributes for a relation .
116
public function match ( array $ results , $ relation ) { $ foreign = $ this -> foreignKey ; $ other = $ this -> otherKey ; $ entities = $ this -> getEager ( ) ; $ dictionary = [ ] ; foreach ( $ entities as $ entity ) { $ entity = $ this -> factory -> make ( $ entity ) ; $ dictionary [ $ entity -> getEntityAttribute ( $ other ) ] = $ entity -> getObject ( ) ; } return array_map ( function ( $ result ) use ( $ dictionary , $ foreign , $ relation ) { if ( array_key_exists ( $ foreign , $ result ) && isset ( $ dictionary [ $ result [ $ foreign ] ] ) ) { $ result [ $ relation ] = $ dictionary [ $ result [ $ foreign ] ] ; } else { $ result [ $ relation ] = null ; } return $ result ; } , $ results ) ; }
Match the Results array to an eagerly loaded relation .
117
public function map ( $ results , array $ eagerLoads = [ ] , $ useCache = false ) : Collection { $ builder = $ this -> newResultBuilder ( ! $ useCache ) ; if ( $ results instanceof Collection ) { $ results = $ results -> all ( ) ; } if ( ! is_array ( $ results ) ) { throw new InvalidArgumentException ( "Parameter 'results' should be an array or a collection." ) ; } $ results = array_map ( function ( $ item ) { return ( array ) $ item ; } , $ results ) ; $ results = $ this -> adapter -> fromDatabase ( $ results ) ; $ entities = $ builder -> build ( $ results , $ eagerLoads ) ; return $ this -> entityMap -> newCollection ( $ entities ) ; }
Map results to a Collection .
118
protected function newResultBuilder ( bool $ skipCache = false ) : ResultBuilderInterface { $ factory = new ResultBuilderFactory ( ) ; return $ factory -> make ( $ this , $ skipCache ) ; }
Return result builder used by this mapper .
119
public function store ( $ entity ) { if ( $ this -> manager -> isTraversable ( $ entity ) ) { return $ this -> storeCollection ( $ entity ) ; } else { return $ this -> storeEntity ( $ entity ) ; } }
Persist an entity or an entity collection into the database .
120
protected function storeCollection ( $ entities ) { $ this -> adapter -> beginTransaction ( ) ; foreach ( $ entities as $ entity ) { $ this -> storeEntity ( $ entity ) ; } $ this -> adapter -> commit ( ) ; return $ entities ; }
Store an entity collection inside a single DB Transaction .
121
protected function storeEntity ( $ entity ) { $ this -> checkEntityType ( $ entity ) ; $ store = new Store ( $ this -> aggregate ( $ entity ) , $ this -> newQueryBuilder ( ) ) ; return $ store -> execute ( ) ; }
Store a single entity into the database .
122
protected function checkEntityType ( $ entity ) { if ( get_class ( $ entity ) != $ this -> entityMap -> getClass ( ) && ! is_subclass_of ( $ entity , $ this -> entityMap -> getClass ( ) ) ) { $ expected = $ this -> entityMap -> getClass ( ) ; $ actual = get_class ( $ entity ) ; throw new InvalidArgumentException ( "Expected : $expected, got $actual." ) ; } }
Check that the entity correspond to the current mapper .
123
public function delete ( $ entity ) { if ( $ this -> manager -> isTraversable ( $ entity ) ) { return $ this -> deleteCollection ( $ entity ) ; } else { $ this -> deleteEntity ( $ entity ) ; } }
Delete an entity or an entity collection from the database .
124
protected function deleteCollection ( $ entities ) { $ this -> adapter -> beginTransaction ( ) ; foreach ( $ entities as $ entity ) { $ this -> deleteEntity ( $ entity ) ; } $ this -> adapter -> commit ( ) ; return $ entities ; }
Delete an Entity Collection inside a single db transaction .
125
protected function deleteEntity ( $ entity ) { $ this -> checkEntityType ( $ entity ) ; $ delete = new Delete ( $ this -> aggregate ( $ entity ) , $ this -> newQueryBuilder ( ) ) ; $ delete -> execute ( ) ; }
Delete a single entity from the database .
126
public function fireEvent ( $ event , $ entity , $ halt = true ) { $ eventName = "analogue.{$event}." . $ this -> entityMap -> getClass ( ) ; $ method = $ halt ? 'until' : 'fire' ; if ( ! array_key_exists ( $ event , $ this -> events ) ) { throw new \ LogicException ( "Analogue : Event $event doesn't exist" ) ; } $ eventClass = $ this -> events [ $ event ] ; $ event = new $ eventClass ( $ entity ) ; return $ this -> dispatcher -> $ method ( $ eventName , $ event ) ; }
Fire the given event for the entity .
127
public function registerEvent ( $ event , $ callback ) { $ name = $ this -> entityMap -> getClass ( ) ; $ this -> dispatcher -> listen ( "analogue.{$event}.{$name}" , $ callback ) ; }
Register an entity event with the dispatcher .
128
public function getGlobalScope ( $ scope ) { return array_first ( $ this -> globalScopes , function ( $ key , $ value ) use ( $ scope ) { return $ scope instanceof $ value ; } ) ; }
Get a global scope registered with the modal .
129
public function applyGlobalScopes ( $ query ) { foreach ( $ this -> getGlobalScopes ( ) as $ scope ) { $ scope -> apply ( $ query , $ this ) ; } return $ query ; }
Apply all of the global scopes to an Analogue Query builder .
130
public function newInstance ( ) { $ class = $ this -> entityMap -> getClass ( ) ; if ( $ this -> entityMap -> useDependencyInjection ( ) ) { return $ this -> newInstanceUsingDependencyInjection ( $ class ) ; } return $ this -> newInstanceUsingInstantiator ( $ class ) ; }
Create a new instance of the mapped entity class .
131
public function removeGlobalScopes ( $ query ) { foreach ( $ this -> getGlobalScopes ( ) as $ scope ) { $ scope -> remove ( $ query , $ this ) ; } return $ query ; }
Remove all of the global scopes from an Analogue Query builder .
132
public function executeCustomCommand ( $ command , $ entity ) { $ commandClass = $ this -> customCommands [ $ command ] ; if ( $ this -> manager -> isTraversable ( $ entity ) ) { foreach ( $ entity as $ instance ) { $ this -> executeSingleCustomCommand ( $ commandClass , $ instance ) ; } } else { return $ this -> executeSingleCustomCommand ( $ commandClass , $ entity ) ; } }
Execute a custom command on an Entity .
133
protected function executeSingleCustomCommand ( $ commandClass , $ entity ) { $ this -> checkEntityType ( $ entity ) ; $ instance = new $ commandClass ( $ this -> aggregate ( $ entity ) , $ this -> newQueryBuilder ( ) ) ; return $ instance -> execute ( ) ; }
Execute a single command instance .
134
public function boot ( ) { if ( static :: $ booted ) { return $ this ; } $ dispatcher = new Dispatcher ( ) ; $ connectionProvider = new CapsuleConnectionProvider ( static :: $ capsule ) ; $ illuminate = new IlluminateDriver ( $ connectionProvider ) ; $ driverManager = new DriverManager ( ) ; $ driverManager -> addDriver ( $ illuminate ) ; static :: $ manager = new Manager ( $ driverManager , $ dispatcher ) ; static :: $ instance = $ this ; static :: $ booted = true ; return $ this ; }
Boot Analogue .
135
protected function buildEmbeddedCollection ( array $ rows ) : Collection { return collect ( $ rows ) -> map ( function ( $ attributes ) { return $ this -> buildEmbeddedObject ( $ attributes ) ; } ) ; }
Build an embedded collection and returns it .
136
protected function normalizeAsArray ( $ objects ) : array { $ key = $ this -> relation ; if ( ! is_array ( $ objects ) && ! $ objects instanceof Collection ) { throw new MappingException ( "column '$key' should be of type array or collection" ) ; } if ( $ objects instanceof Collection ) { $ objects = $ objects -> all ( ) ; } $ normalizedObjects = [ ] ; foreach ( $ objects as $ object ) { $ wrapper = $ this -> factory -> make ( $ object ) ; $ normalizedObjects [ ] = $ wrapper -> getEntityAttributes ( ) ; } if ( $ this -> asJson ) { $ normalizedObjects = json_encode ( $ normalizedObjects ) ; } return [ $ key => $ normalizedObjects ] ; }
Normalize object as array containing raw attributes .
137
protected function getMappable ( $ entity ) : InternallyMappable { if ( $ entity instanceof InternallyMappable ) { return $ entity ; } $ factory = new Factory ( ) ; $ wrappedEntity = $ factory -> make ( $ entity ) ; return $ wrappedEntity ; }
Return internally mappable if not mappable .
138
protected function parseRelationships ( ) { foreach ( $ this -> entityMap -> getSingleRelationships ( ) as $ relation ) { $ this -> parseSingleRelationship ( $ relation ) ; } foreach ( $ this -> entityMap -> getManyRelationships ( ) as $ relation ) { $ this -> parseManyRelationship ( $ relation ) ; } }
Parse Every relationships defined on the entity .
139
protected function parseForCommonValues ( $ relation ) { if ( ! $ this -> hasAttribute ( $ relation ) ) { $ this -> relationships [ $ relation ] = [ ] ; return false ; } $ value = $ this -> getRelationshipValue ( $ relation ) ; if ( is_null ( $ value ) ) { $ this -> relationships [ $ relation ] = [ ] ; $ this -> needSync [ ] = $ relation ; return false ; } return $ value ; }
Parse for values common to single & many relations .
140
protected function parseSingleRelationship ( string $ relation ) : bool { if ( ! $ value = $ this -> parseForCommonValues ( $ relation ) ) { return true ; } if ( $ value instanceof Collection || is_array ( $ value ) || $ value instanceof CollectionProxy ) { throw new MappingException ( "Entity's attribute $relation should not be array, or collection" ) ; } if ( $ value instanceof LazyLoadingInterface && ! $ value -> isProxyInitialized ( ) ) { $ this -> relationships [ $ relation ] = [ ] ; return true ; } if ( $ value instanceof LazyLoadingInterface && $ value -> isProxyInitialized ( ) ) { $ value = $ value -> getWrappedValueHolderValue ( ) ; } if ( $ this -> isParentOrRoot ( $ value ) ) { $ this -> relationships [ $ relation ] = [ ] ; return true ; } $ subAggregate = $ this -> createSubAggregate ( $ value , $ relation ) ; $ this -> relationships [ $ relation ] = [ $ subAggregate ] ; $ this -> needSync [ ] = $ relation ; return true ; }
Parse a single relationship .
141
protected function isParentOrRoot ( $ value ) : bool { $ id = spl_object_hash ( $ value ) ; $ root = $ this -> root ? $ this -> root -> getWrappedEntity ( ) -> getObject ( ) : null ; $ parent = $ this -> parent ? $ this -> parent -> getWrappedEntity ( ) -> getObject ( ) : null ; if ( $ parent && ( spl_object_hash ( $ parent ) == $ id ) ) { return true ; } if ( $ root && ( spl_object_hash ( $ root ) == $ id ) ) { return true ; } return false ; }
Check if value isn t parent or root in the aggregate .
142
protected function parseManyRelationship ( string $ relation ) : bool { if ( ! $ value = $ this -> parseForCommonValues ( $ relation ) ) { return true ; } if ( is_array ( $ value ) || ( ! $ value instanceof CollectionProxy && $ value instanceof Collection ) ) { $ this -> needSync [ ] = $ relation ; } if ( $ value instanceof CollectionProxy && $ value -> isProxyInitialized ( ) ) { $ this -> needSync [ ] = $ relation ; } if ( $ value instanceof CollectionProxy && ! $ value -> isProxyInitialized ( ) ) { $ value = $ value -> getAddedItems ( ) ; } if ( ! is_array ( $ value ) && ! $ value instanceof Collection ) { throw new MappingException ( "'$relation' attribute should be array() or Collection" ) ; } $ this -> relationships [ $ relation ] = $ this -> createSubAggregates ( $ value , $ relation ) ; return true ; }
Parse a many relationship .
143
protected function getRelationshipValue ( string $ relation ) { $ value = $ this -> getEntityAttribute ( $ relation ) ; if ( is_scalar ( $ value ) ) { throw new MappingException ( "Entity's attribute $relation should be array, object, collection or null" ) ; } return $ value ; }
Return Entity s relationship attribute .
144
protected function createSubAggregates ( $ entities , string $ relation ) : array { $ aggregates = [ ] ; foreach ( $ entities as $ entity ) { $ aggregates [ ] = $ this -> createSubAggregate ( $ entity , $ relation ) ; } return $ aggregates ; }
Create a child aggregated entity .
145
protected function createSubAggregate ( $ entity , string $ relation ) : self { $ root = is_null ( $ this -> root ) ? $ this : $ this -> root ; return new self ( $ entity , $ this , $ relation , $ root ) ; }
Create a related subAggregate .
146
public function getRelationship ( string $ name ) : array { if ( array_key_exists ( $ name , $ this -> relationships ) ) { return $ this -> relationships [ $ name ] ; } return [ ] ; }
Get a relationship as an aggregated entities array .
147
public function getNonExistingRelated ( array $ relationships ) : array { $ nonExisting = [ ] ; foreach ( $ relationships as $ relation ) { if ( $ this -> hasAttribute ( $ relation ) && array_key_exists ( $ relation , $ this -> relationships ) ) { $ nonExisting = array_merge ( $ nonExisting , $ this -> getNonExistingFromRelation ( $ relation ) ) ; } } return $ nonExisting ; }
Get Non existing related entities from several relationships .
148
protected function getNonExistingFromRelation ( string $ relation ) : array { $ nonExisting = [ ] ; foreach ( $ this -> relationships [ $ relation ] as $ aggregate ) { if ( ! $ aggregate -> exists ( ) ) { $ nonExisting [ ] = $ aggregate ; } } return $ nonExisting ; }
Get non - existing related entities from a single relation .
149
public function syncRelationships ( array $ relationships ) { foreach ( $ relationships as $ relation ) { if ( in_array ( $ relation , $ this -> needSync ) ) { $ this -> synchronize ( $ relation ) ; } } }
Synchronize relationships if needed .
150
protected function synchronize ( string $ relation ) { $ actualContent = $ this -> relationships [ $ relation ] ; $ relationshipObject = $ this -> entityMap -> $ relation ( $ this -> getEntityObject ( ) ) ; $ relationshipObject -> setParent ( $ this -> wrappedEntity ) ; $ relationshipObject -> sync ( $ actualContent ) ; }
Synchronize a relationship attribute .
151
public function getRawAttributes ( ) : array { $ attributes = $ this -> wrappedEntity -> getEntityAttributes ( ) ; foreach ( $ this -> entityMap -> getNonEmbeddedRelationships ( ) as $ relation ) { unset ( $ attributes [ $ relation ] ) ; } if ( $ this -> entityMap -> getInheritanceType ( ) == 'single_table' ) { $ attributes = $ this -> addDiscriminatorColumn ( $ attributes ) ; } $ attributes = $ this -> entityMap -> getColumnNamesFromAttributes ( $ attributes ) ; $ attributes = $ this -> flattenEmbeddables ( $ attributes ) ; $ foreignKeys = $ this -> getForeignKeyAttributes ( ) ; return $ this -> mergeForeignKeysWithAttributes ( $ foreignKeys , $ attributes ) ; }
Get Raw Entity s attributes as they are represented in the database including value objects foreign keys and discriminator column .
152
protected function mergeForeignKeysWithAttributes ( array $ foreignKeys , array $ attributes ) : array { $ cachedAttributes = $ this -> getCachedRawAttributes ( ) ; foreach ( $ foreignKeys as $ fkAttributeKey => $ fkAttributeValue ) { if ( ! array_key_exists ( $ fkAttributeKey , $ attributes ) ) { $ attributes [ $ fkAttributeKey ] = $ fkAttributeValue ; continue ; } if ( $ attributes [ $ fkAttributeKey ] === $ fkAttributeValue ) { $ attributes [ $ fkAttributeKey ] = $ fkAttributeValue ; continue ; } if ( array_key_exists ( $ fkAttributeKey , $ cachedAttributes ) ) { if ( $ attributes [ $ fkAttributeKey ] !== $ cachedAttributes [ $ fkAttributeKey ] ) { continue ; } else { $ attributes [ $ fkAttributeKey ] = $ fkAttributeValue ; } } else { if ( is_null ( $ attributes [ $ fkAttributeKey ] ) ) { $ attributes [ $ fkAttributeKey ] = $ fkAttributeValue ; } } } return $ attributes ; }
Merge foreign keys and attributes by comparing their current value to the cache and guess the user intent .
153
protected function addDiscriminatorColumn ( array $ attributes ) : array { $ discriminatorColumn = $ this -> entityMap -> getDiscriminatorColumn ( ) ; $ entityClass = $ this -> entityMap -> getClass ( ) ; if ( ! array_key_exists ( $ discriminatorColumn , $ attributes ) ) { $ map = $ this -> entityMap -> getDiscriminatorColumnMap ( ) ; $ type = array_search ( $ entityClass , $ map ) ; if ( $ type === false ) { $ attributes [ $ discriminatorColumn ] = $ entityClass ; } else { $ attributes [ $ discriminatorColumn ] = $ type ; } } return $ attributes ; }
Add Discriminator Column if it doesn t exist on the actual entity .
154
protected function flattenEmbeddables ( array $ attributes ) : array { $ embeddables = $ this -> entityMap -> getEmbeddables ( ) ; foreach ( $ embeddables as $ localKey => $ embed ) { $ valueObject = $ attributes [ $ localKey ] ; unset ( $ attributes [ $ localKey ] ) ; $ valueObjectAttributes = $ valueObject -> getEntityAttributes ( ) ; $ voMap = $ this -> getMapper ( ) -> getManager ( ) -> getValueMap ( $ embed ) ; $ valueObjectAttributes = $ voMap -> getColumnNamesFromAttributes ( $ valueObjectAttributes ) ; $ prefix = snake_case ( class_basename ( $ embed ) ) ; foreach ( $ valueObjectAttributes as $ key => $ value ) { $ valueObjectAttributes [ $ prefix . '_' . $ key ] = $ value ; unset ( $ valueObjectAttributes [ $ key ] ) ; } $ attributes = array_merge ( $ attributes , $ valueObjectAttributes ) ; } $ embeddedRelations = $ this -> entityMap -> getEmbeddedRelationships ( ) ; foreach ( $ embeddedRelations as $ relation ) { $ parentInstance = $ this -> getMapper ( ) -> newInstance ( ) ; $ relationInstance = $ this -> entityMap -> $ relation ( $ parentInstance ) ; $ embeddedObject = $ attributes [ $ relation ] ; unset ( $ attributes [ $ relation ] ) ; $ attributes = $ relationInstance -> normalize ( $ embeddedObject ) + $ attributes ; } return $ attributes ; }
Convert Value Objects to raw db attributes .
155
protected function getCachedRawAttributes ( array $ columns = null ) : array { $ cachedAttributes = $ this -> getCache ( ) -> get ( $ this -> getEntityKeyValue ( ) ) ; if ( is_null ( $ columns ) ) { return $ cachedAttributes ; } return array_only ( $ cachedAttributes , $ columns ) ; }
Return s entity raw attributes in the state they were at last query .
156
protected function getCachedAttribute ( $ key ) { $ cachedAttributes = $ this -> getCache ( ) -> get ( $ this -> getEntityKeyValue ( ) ) ; if ( array_key_exists ( $ key , $ cachedAttributes ) ) { return $ cachedAttributes [ $ key ] ; } }
Return a single attribute from the cache .
157
public function getForeignKeyAttributes ( ) : array { $ foreignKeys = [ ] ; foreach ( $ this -> entityMap -> getLocalRelationships ( ) as $ relation ) { if ( $ this -> isNonLoadedProxy ( $ relation ) ) { $ foreignKeys = $ foreignKeys + $ this -> getForeignKeyAttributesFromNonLoadedRelation ( $ relation ) ; continue ; } if ( $ this -> isActualRelationships ( $ relation ) ) { $ foreignKeys = $ foreignKeys + $ this -> getForeignKeyAttributesFromRelation ( $ relation ) ; } else { $ foreignKeys = $ foreignKeys + $ this -> getNullForeignKeyFromRelation ( $ relation ) ; } } if ( ! is_null ( $ this -> parent ) ) { $ foreignKeys = $ this -> getForeignKeyAttributesFromParent ( ) + $ foreignKeys ; } return $ foreignKeys ; }
Convert related Entity s attributes to foreign keys .
158
protected function getNullForeignKeyFromRelation ( string $ relation ) : array { $ key = $ this -> entityMap -> getLocalKeys ( $ relation ) ; if ( is_array ( $ key ) ) { return $ this -> entityMap -> getEmptyValueForLocalKey ( $ relation ) ; } if ( is_null ( $ key ) ) { throw new MappingException ( "Foreign key for relation $relation cannot be null" ) ; } return [ $ key => $ this -> entityMap -> getEmptyValueForLocalKey ( $ relation ) , ] ; }
Get a null foreign key value pair for an empty relationship .
159
public function updatePivotRecords ( ) { $ pivots = $ this -> entityMap -> getPivotRelationships ( ) ; foreach ( $ pivots as $ pivot ) { if ( array_key_exists ( $ pivot , $ this -> relationships ) ) { $ this -> updatePivotRelation ( $ pivot ) ; } } }
Update Pivot records on loaded relationships by comparing the values from the Entity Cache to the actual relationship inside the aggregated entity .
160
protected function updatePivotRelation ( string $ relation ) { $ hashes = $ this -> getEntityHashesFromRelation ( $ relation ) ; $ cachedAttributes = $ this -> getCachedRawAttributes ( ) ; if ( array_key_exists ( $ relation , $ cachedAttributes ) ) { $ new = array_diff ( $ hashes , array_keys ( $ cachedAttributes [ $ relation ] ) ) ; $ existing = array_intersect ( $ hashes , array_keys ( $ cachedAttributes [ $ relation ] ) ) ; } else { $ existing = [ ] ; $ new = $ hashes ; } if ( count ( $ new ) > 0 ) { $ pivots = $ this -> getRelatedAggregatesFromHashes ( $ new , $ relation ) ; $ this -> entityMap -> $ relation ( $ this -> getEntityObject ( ) ) -> createPivots ( $ pivots ) ; } if ( count ( $ existing ) > 0 ) { foreach ( $ existing as $ pivotHash ) { $ this -> updatePivotIfDirty ( $ pivotHash , $ relation ) ; } } }
Update Single pivot relationship .
161
protected function updatePivotIfDirty ( string $ pivotHash , string $ relation ) { $ aggregate = $ this -> getRelatedAggregateFromHash ( $ pivotHash , $ relation ) ; if ( $ aggregate -> hasAttribute ( 'pivot' ) ) { $ pivot = $ aggregate -> getEntityAttribute ( 'pivot' ) -> getEntityAttributes ( ) ; $ cachedPivotAttributes = $ this -> getPivotAttributesFromCache ( $ pivotHash , $ relation ) ; $ actualPivotAttributes = array_only ( $ pivot , array_keys ( $ cachedPivotAttributes ) ) ; $ dirty = $ this -> getDirtyAttributes ( $ actualPivotAttributes , $ cachedPivotAttributes ) ; if ( count ( $ dirty ) > 0 ) { $ id = $ aggregate -> getEntityKeyValue ( ) ; $ this -> entityMap -> $ relation ( $ this -> getEntityObject ( ) ) -> updateExistingPivot ( $ id , $ dirty ) ; } } }
Compare existing pivot record in cache and update it if the pivot attributes are dirty .
162
protected function getDirtyAttributes ( array $ actual , array $ cached ) : array { $ dirty = [ ] ; foreach ( $ actual as $ key => $ value ) { if ( ! $ this -> originalIsNumericallyEquivalent ( $ value , $ cached [ $ key ] ) ) { $ dirty [ $ key ] = $ actual [ $ key ] ; } } return $ dirty ; }
Compare two attributes array and return dirty attributes .
163
protected function getRelatedAggregatesFromHashes ( array $ hashes , string $ relation ) : array { $ related = [ ] ; foreach ( $ hashes as $ hash ) { $ aggregate = $ this -> getRelatedAggregateFromHash ( $ hash , $ relation ) ; if ( $ aggregate ) { $ related [ ] = $ aggregate ; } } return $ related ; }
Returns an array of related Aggregates from its entity hashes .
164
protected function getRelatedAggregateFromHash ( string $ hash , string $ relation ) { foreach ( $ this -> relationships [ $ relation ] as $ aggregate ) { if ( $ aggregate -> getEntityHash ( ) == $ hash ) { return $ aggregate ; } } }
Get related aggregate from its hash .
165
protected function getEntityHashesFromRelation ( string $ relation ) : array { return array_map ( function ( Aggregate $ aggregate ) { return $ aggregate -> getEntityHash ( ) ; } , $ this -> relationships [ $ relation ] ) ; }
Return an array of Entity Hashes from a specific relation .
166
protected function isActualRelationships ( string $ relation ) : bool { return array_key_exists ( $ relation , $ this -> relationships ) && count ( $ this -> relationships [ $ relation ] ) > 0 ; }
Check the existence of an actual relationship .
167
public function getDirtyRawAttributes ( ) : array { $ attributes = $ this -> getRawAttributes ( ) ; $ cachedAttributes = $ this -> getCachedRawAttributes ( array_keys ( $ attributes ) ) ; $ dirty = [ ] ; foreach ( $ attributes as $ key => $ value ) { if ( $ this -> isActualRelation ( $ key ) || $ key == 'pivot' ) { continue ; } if ( ! array_key_exists ( $ key , $ cachedAttributes ) && ! $ value instanceof Pivot ) { $ dirty [ $ key ] = $ value ; } elseif ( $ value !== $ cachedAttributes [ $ key ] && ! $ this -> originalIsNumericallyEquivalent ( $ value , $ cachedAttributes [ $ key ] ) ) { $ dirty [ $ key ] = $ value ; } } return $ dirty ; }
Get Only Raw Entity attributes which have been modified since last query .
168
protected function isNonLoadedProxy ( string $ key ) : bool { $ relation = $ this -> getEntityAttribute ( $ key ) ; return $ relation instanceof ProxyInterface && ! $ relation -> isProxyInitialized ( ) ; }
Return true if attribute is a non - loaded proxy .
169
public function usesAttributesArray ( ) : bool { if ( $ this -> arrayName === null ) { return false ; } if ( $ this -> attributes === null ) { return false ; } return true ; }
Return true if the Entity has an attributes array property .
170
public function getCompiledAttributes ( ) : array { $ key = $ this -> getKeyName ( ) ; $ embeddables = array_keys ( $ this -> getEmbeddables ( ) ) ; $ relationships = $ this -> getRelationships ( ) ; $ attributes = $ this -> getAttributes ( ) ; return array_merge ( [ $ key ] , $ embeddables , $ relationships , $ attributes ) ; }
Get all the attribute names for the class including relationships embeddables and primary key .
171
public function getProperties ( ) : array { return get_parent_class ( __CLASS__ ) !== false ? array_unique ( array_merge ( $ this -> properties , parent :: getProperties ( ) ) ) : $ this -> properties ; }
Return attributes that should be mapped to class properties .
172
public function getEmptyValueForRelationship ( string $ relation ) { if ( $ this -> isSingleRelationship ( $ relation ) ) { return ; } if ( $ this -> isManyRelationship ( $ relation ) ) { return new Collection ( ) ; } throw new MappingException ( "Cannot determine default value of $relation" ) ; }
Return empty value for a given relationship .
173
public function getEmptyValueForLocalKey ( string $ relation ) { if ( $ this -> isPolymorphic ( $ relation ) ) { $ key = $ this -> localForeignKeys [ $ relation ] ; return [ $ key [ 'type' ] => null , $ key [ 'id' ] => null , ] ; } if ( $ this -> isManyRelationship ( $ relation ) ) { return [ ] ; } }
Return empty value for a local foreign key .
174
public function getLocalKeys ( $ relation ) { return isset ( $ this -> localForeignKeys [ $ relation ] ) ? $ this -> localForeignKeys [ $ relation ] : null ; }
Return the local keys associated to the relationship .
175
protected function addSingleRelation ( string $ relation ) { if ( ! in_array ( $ relation , $ this -> singleRelations ) ) { $ this -> singleRelations [ ] = $ relation ; } }
Add a single relation method name once .
176
protected function addForeignRelation ( string $ relation ) { if ( ! in_array ( $ relation , $ this -> foreignRelations ) ) { $ this -> foreignRelations [ ] = $ relation ; } }
Add a foreign relation method name once .
177
protected function addPolymorphicRelation ( string $ relation ) { if ( ! in_array ( $ relation , $ this -> polymorphicRelations ) ) { $ this -> polymorphicRelations [ ] = $ relation ; } }
Add a polymorphic relation method name once .
178
protected function addNonProxyRelation ( string $ relation ) { if ( ! in_array ( $ relation , $ this -> nonProxyRelationships ) ) { $ this -> nonProxyRelationships [ ] = $ relation ; } }
Add a non proxy relation method name once .
179
protected function addLocalRelation ( string $ relation ) { if ( ! in_array ( $ relation , $ this -> localRelations ) ) { $ this -> localRelations [ ] = $ relation ; } }
Add a local relation method name once .
180
protected function addManyRelation ( string $ relation ) { if ( ! in_array ( $ relation , $ this -> manyRelations ) ) { $ this -> manyRelations [ ] = $ relation ; } }
Add a many relation method name once .
181
protected function addPivotRelation ( string $ relation ) { if ( ! in_array ( $ relation , $ this -> pivotRelations ) ) { $ this -> pivotRelations [ ] = $ relation ; } }
Add a pivot relation method name once .
182
protected function addEmbeddedRelation ( string $ relation ) { if ( ! in_array ( $ relation , $ this -> embeddedRelations ) ) { $ this -> embeddedRelations [ ] = $ relation ; } }
Add an embedded relation .
183
public function embedsOne ( $ parent , string $ relatedClass , string $ relation = null ) : EmbedsOne { if ( is_null ( $ relation ) ) { list ( , $ caller ) = debug_backtrace ( false ) ; $ relation = $ caller [ 'function' ] ; } $ this -> addEmbeddedRelation ( $ relation ) ; $ this -> addNonProxyRelation ( $ relation ) ; return new EmbedsOne ( $ parent , $ relatedClass , $ relation ) ; }
Define an Embedded Object .
184
public function embedsMany ( $ parent , string $ relatedClass , string $ relation = null ) : EmbedsMany { if ( is_null ( $ relation ) ) { list ( , $ caller ) = debug_backtrace ( false ) ; $ relation = $ caller [ 'function' ] ; } $ this -> addEmbeddedRelation ( $ relation ) ; $ this -> addNonProxyRelation ( $ relation ) ; return new EmbedsMany ( $ parent , $ relatedClass , $ relation ) ; }
Define an Embedded Collection .
185
public function initialize ( ) { $ userMethods = $ this -> getCustomMethods ( ) ; if ( count ( $ userMethods ) > 0 ) { $ this -> relationships = $ this -> parseMethodsForRelationship ( $ userMethods ) ; } if ( count ( $ this -> dynamicRelationships ) > 0 ) { $ this -> relationships = $ this -> relationships + $ this -> getDynamicRelationships ( ) ; } }
Process EntityMap parsing at initialization time .
186
protected function parseMethodsForRelationship ( array $ customMethods ) : array { $ relationships = [ ] ; $ class = new ReflectionClass ( get_class ( $ this ) ) ; $ entityClass = $ this -> getClass ( ) ; foreach ( $ customMethods as $ methodName ) { $ method = $ class -> getMethod ( $ methodName ) ; if ( $ method -> getNumberOfParameters ( ) > 0 ) { $ params = $ method -> getParameters ( ) ; if ( $ params [ 0 ] -> getClass ( ) && ( $ params [ 0 ] -> getClass ( ) -> name == $ entityClass || is_subclass_of ( $ entityClass , $ params [ 0 ] -> getClass ( ) -> name ) ) ) { $ relationships [ ] = $ methodName ; } } } return $ relationships ; }
Parse user s class methods for relationships .
187
protected function sortRelationshipsByType ( ) { $ entityClass = $ this -> getClass ( ) ; $ entity = unserialize ( sprintf ( 'O:%d:"%s":0:{}' , strlen ( $ entityClass ) , $ entityClass ) ) ; foreach ( $ this -> relationships as $ relation ) { $ this -> $ relation ( $ entity ) ; } }
Sort Relationships methods by type .
188
public function getAdapter ( string $ driver , string $ connection = null ) { if ( array_key_exists ( $ driver , $ this -> drivers ) ) { return $ this -> drivers [ $ driver ] -> getAdapter ( $ connection ) ; } }
Get the DBAdapter .
189
public function make ( $ object ) { $ manager = Manager :: getInstance ( ) ; $ config = new Configuration ( get_class ( $ object ) ) ; $ hydratorClass = $ config -> createFactory ( ) -> getHydratorClass ( ) ; $ hydrator = new $ hydratorClass ( ) ; if ( $ manager -> isValueObject ( $ object ) ) { $ entityMap = $ manager -> getValueMap ( $ object ) ; } else { $ entityMap = $ manager -> mapper ( $ object ) -> getEntityMap ( ) ; } return new ObjectWrapper ( $ object , $ entityMap , $ hydrator ) ; }
Build the wrapper corresponding to the object s type .
190
public function match ( array $ results , $ relation ) { $ entities = $ this -> getEager ( ) ; $ dictionary = $ this -> buildDictionary ( $ entities ) ; $ keyName = $ this -> parentMap -> getKeyName ( ) ; $ cache = $ this -> parentMapper -> getEntityCache ( ) ; $ host = $ this ; return array_map ( function ( $ result ) use ( $ dictionary , $ keyName , $ cache , $ relation , $ host ) { $ key = $ result [ $ keyName ] ; if ( isset ( $ dictionary [ $ key ] ) ) { $ collection = $ host -> relatedMap -> newCollection ( $ dictionary [ $ key ] ) ; $ result [ $ relation ] = $ collection ; $ cache -> cacheLoadedRelationResult ( $ key , $ relation , $ collection , $ this ) ; } else { $ result [ $ relation ] = $ host -> relatedMap -> newCollection ( ) ; } return $ result ; } , $ results ) ; }
Match Eagerly loaded relation to result .
191
public function updatePivot ( $ entity ) { $ keyName = $ this -> relatedMap -> getKeyName ( ) ; $ this -> updateExistingPivot ( $ entity -> getEntityAttribute ( $ keyName ) , $ entity -> getEntityAttribute ( 'pivot' ) -> getEntityAttributes ( ) ) ; }
Update Pivot .
192
public function createPivots ( $ relatedEntities ) { $ keys = [ ] ; $ attributes = [ ] ; $ keyName = $ this -> relatedMap -> getKeyName ( ) ; foreach ( $ relatedEntities as $ entity ) { $ keys [ ] = $ entity -> getEntityAttribute ( $ keyName ) ; } $ records = $ this -> createAttachRecords ( $ keys , $ attributes ) ; $ this -> query -> getQuery ( ) -> from ( $ this -> table ) -> insert ( $ records ) ; }
Create Pivot Records .
193
protected function matchAsAttributes ( array $ attributes ) : array { $ attributesMap = $ this -> getAttributesDictionnary ( ) ; $ originalAttributes = array_only ( $ attributes , $ attributesMap ) ; $ embeddedAttributes = [ ] ; foreach ( $ originalAttributes as $ key => $ value ) { $ embeddedKey = array_search ( $ key , $ attributesMap ) ; $ embeddedAttributes [ $ embeddedKey ] = $ value ; } foreach ( array_keys ( $ originalAttributes ) as $ key ) { unset ( $ attributes [ $ key ] ) ; } $ attributes [ $ this -> relation ] = $ this -> buildEmbeddedObject ( $ embeddedAttributes ) ; return $ attributes ; }
Transform attributes from the parent entity result into an embedded object and return the updated attributes .
194
protected function getAttributesDictionnary ( ) : array { $ embeddedAttributeKeys = $ this -> getEmbeddedObjectAttributes ( ) ; $ attributesMap = [ ] ; foreach ( $ embeddedAttributeKeys as $ key ) { $ attributesMap [ $ key ] = $ this -> getParentAttributeKey ( $ key ) ; } return $ attributesMap ; }
Return a dictionary of attributes key on parent Entity .
195
protected function normalizeAsArray ( $ object ) : array { $ wrapper = $ this -> factory -> make ( $ object ) ; $ value = $ wrapper -> getEntityAttributes ( ) ; if ( $ this -> asJson ) { $ value = json_encode ( $ value ) ; } return [ $ this -> relation => $ value ] ; }
Normalize object an array containing raw attributes .
196
protected function normalizeAsAttributes ( $ object ) : array { if ( is_null ( $ object ) ) { return $ this -> nullObjectAttributes ( ) ; } $ attributesMap = $ this -> getAttributesDictionnary ( ) ; $ wrapper = $ this -> factory -> make ( $ object ) ; $ normalizedAttributes = [ ] ; foreach ( $ attributesMap as $ embedKey => $ parentKey ) { $ normalizedAttributes [ $ parentKey ] = $ wrapper -> getEntityAttribute ( $ embedKey ) ; } return $ normalizedAttributes ; }
Normalize object as parent s attributes .
197
protected function nullObjectAttributes ( ) : array { $ attributesMap = $ this -> getAttributesDictionnary ( ) ; $ normalizedAttributes = [ ] ; foreach ( $ attributesMap as $ embedKey => $ parentKey ) { $ normalizedAttributes [ $ parentKey ] = null ; } return $ normalizedAttributes ; }
Set all object attributes to null .
198
public function execute ( ) { $ entity = $ this -> aggregate -> getEntityObject ( ) ; $ wrappedEntity = $ this -> aggregate -> getWrappedEntity ( ) ; $ mapper = $ this -> aggregate -> getMapper ( ) ; if ( $ mapper -> fireEvent ( 'storing' , $ wrappedEntity ) === false ) { return false ; } $ this -> preStoreProcess ( ) ; if ( ! $ this -> aggregate -> exists ( ) ) { if ( $ mapper -> fireEvent ( 'creating' , $ wrappedEntity ) === false ) { return false ; } $ this -> insert ( ) ; $ mapper -> fireEvent ( 'created' , $ wrappedEntity , false ) ; } elseif ( $ this -> aggregate -> isDirty ( ) ) { if ( $ mapper -> fireEvent ( 'updating' , $ wrappedEntity ) === false ) { return false ; } $ this -> update ( ) ; $ mapper -> fireEvent ( 'updated' , $ wrappedEntity , false ) ; } $ this -> postStoreProcess ( ) ; $ mapper -> fireEvent ( 'stored' , $ wrappedEntity , false ) ; $ key = $ this -> aggregate -> getEntityKeyValue ( ) ; if ( ! $ mapper -> getInstanceCache ( ) -> has ( $ key ) ) { $ mapper -> getInstanceCache ( ) -> add ( $ entity , $ key ) ; } $ this -> syncForeignKeyAttributes ( ) ; $ wrappedEntity -> unwrap ( ) ; return $ entity ; }
Persist the entity in the database .
199
protected function preStoreProcess ( ) { $ localRelationships = $ this -> aggregate -> getEntityMap ( ) -> getLocalRelationships ( ) ; $ this -> createRelatedEntities ( $ localRelationships ) ; $ this -> aggregate -> syncRelationships ( $ localRelationships ) ; }
Run all operations that have to occur before actually storing the entity .