Dummy Data
Once you have set up the Route -> Controller -> Model -> View chain, you can start adding dummy data to models, for use in views.
Data propetries
After discussions with Jake, we're coming to the conclusion that while a little more faffy, making getter functions for all data is probably better. This lends well to dummy data. On the model, create a new getter function for any piece of data that you want to use. The only thing to bear in mind is to ensure that it returns the same type of data that you expect this dummy data to eventually be replaced with. So if you have a name, it should return a string. If you intend to have a true / false evaluation, it should return an appropriate boolean value.
/**
* Gets the name of this Dummy
*
* @return string
*/
public function getName()
{
return 'Dummy Name';
}
This will enable a Backend developer to (most likely) come alone and change return 'Dummy Name'; to return $this->name; and for the whole thing to 'just work'.
It's worth thinking about this as thoroughly as you can before making it, as changing data types part way through can still be a headache. For example, you may with to save a price against a model, but will that be saved as an integer or as a string? If it's an integer, will you be wanting it in pounds, or pence? When you don't know the answer or aren't totally sure, make a function that gets what you want, in the format that you want it. For example, if the intention is to store a money value AND do maths on it to derive other values, it should definitely be an integer of pence. However, if you don't care about the maths, or if you know you only want a string representation of it, add additional comments to your getter function, and maybe name it accordingly.
/**
* Gets the price of this Dummy.
*
* @return string
*/
public function getPrice()
{
return 9999;
}
/**
* Gets the price of this Dummy, returned as a human readable string.
*
* @return string
*/
public function getPriceString()
{
return '£99.99';
}
If you feel confident, add the logic yourself from converting one piece of dummy data to another.
/**
* Gets the price of this Dummy, returned as a human readable string.
*
* @return string
*/
public function getPriceString()
{
$pence = $this->getPrice();
$pounds = round(($pence / 100), 2);
return '£' . $pounds;
}
Dummy Relationships
You could also potentially return dummy relationships. The following code would simulate a relationship to another Model
/**
* Gets the placeholder associated with this Dummy
*
* @return Placeholder
*/
public function getPlaceholder()
{
return new Placeholder;
}
Or, you could simulated a hasMany or belongsToMany type relationship with a collection
/**
* Gets the dummy's related placeholders
*
* @return Collection
*/
public function getPlaceholders()
{
$placeholders = (new Placeholder)->newCollection();
$placeholders->push(new Placeholder);
$placeholders->push(new Placeholder);
$placeholders->push(new Placeholder);
return $placeholders;
}
Dummy Functions
You could (and probably should) also make dummy functions for other things. For example, you should call a hasPlaceholders() function in your views to test whether to include a block of html, instead of making that check with the if statement itself:
/**
* Test whether this Dummy has any associated placholders
*
* @return boolean
*/
public function hasPlaceholders()
{
return true;
}
If you can, you could even add the correct logic based on other getters that you've made.
/**
* Test whether this Dummy has any associated placholders
*
* @return boolean
*/
public function hasPlaceholders()
{
return !! $this->getPlaceholders()->count();
}