Slide 1

Slide 1 text

A BEGINNER’S GUIDE TO: COMPLEX DATA TYPES

Slide 2

Slide 2 text

DENIS BRUMANN [email protected] @dbrumann

Slide 3

Slide 3 text

What am I talking about? Latitude and longitude
 form a unit If one is missing or invalid the coordinates are invalid

Slide 4

Slide 4 text

Definition In computer science, a composite data type or compound data type is any data type which can be constructed in a program using the programming language's primitive data types and other composite types. — https://en.wikipedia.org/wiki/Composite_data_type

Slide 5

Slide 5 text

Typical cases Money Coordinates Address Recurring cycles involving e.g. a combined year + month, like 2017/02, form a term (e.g. for reports) An entity’s ID combined with another property
 (e.g. a revision number or date) forming composite ID

Slide 6

Slide 6 text

Money https://github.com/moneyphp/money

Slide 7

Slide 7 text

GeoPHP https://geophp.net/ https://github.com/phayes/geoPHP

Slide 8

Slide 8 text

How to spot complex types? Long list of properties or large number of constructor arguments

Slide 9

Slide 9 text

How to spot complex types? Mismatch between database and code can be an indicator; having to combine primitive types before fetch/insert

Slide 10

Slide 10 text

How to spot complex types? Splitting or combining data in getters/setters

Slide 11

Slide 11 text

How to spot complex types? Special/recurring rules for outputting

Slide 12

Slide 12 text

How to spot complex types? Conditionals involving multiple primitive types Bonus points for when (InvalidArgument)Exceptions are involved

Slide 13

Slide 13 text

What not to do with them? Don’t incorporate them in other entities* Don’t duplicate the logic in your code Don’t spread the logic * it depends
 — Don’t rip apart your entities just for the sake of having smaller, coherent types

Slide 14

Slide 14 text

If you have to keep mutability Sanity checks avoid inconsistent state External validation is an option

Slide 15

Slide 15 text

PHP 7 is your friend Return types will cause a (catchable) TypeError to be thrown when value was not set before

Slide 16

Slide 16 text

If you have to keep mutability Should a model decide
 it’s own validity?

Slide 17

Slide 17 text

Immutability Immutability helps with consistency Keep logic inside the object Check constraints during construction

Slide 18

Slide 18 text

Complex made from complex Keeps things simple Avoid chained calls

Slide 19

Slide 19 text

Factory methods Factory methods provide convenience and help expressing edge case

Slide 20

Slide 20 text

Keep alternations together alternate formats in one place makes changes easier

Slide 21

Slide 21 text

Database mapping Advanced field value conversion using custom mapping types http://docs.doctrine-project.org/ projects/doctrine-orm/en/latest/ cookbook/advanced-field-value- Separating concerns using Embeddables http://docs.doctrine-project.org/ projects/doctrine-orm/en/latest/ tutorials/embeddables.html

Slide 22

Slide 22 text

Persisting complex types Custom Doctrine Mapping type

Slide 23

Slide 23 text

Mapping Type converts back and forth between database field and property

Slide 24

Slide 24 text

Embeddables Declare complex type as Embeddable and map properties to database fields Use @Embedded in your Entity

Slide 25

Slide 25 text

Thanks