Common and useful classes, methods, exceptions etc.
Located in Meritoo\Common\ValueObject
namespace and in src/ValueObject/
directory.
Meritoo\Common\ValueObject\Address
Represents address of company, institution, user etc. Contains properties:
$street
- the street$buildingNumber
- the number of building$flatNumber
- the number of flat$zipCode
- the zip code$city
- the city, location
New instance can be created using constructor
new Address('New York', '00123', '4th Avenue', '10', '200');
Has getters for each property, e.g. getFlatNumber()
or getZipCode()
, and 1 extra method:
getFullStreet()
that returns name of street with related numbers (building & flat number).
Example:
$address = new Address('New York', '00123', '4th Avenue', '10', '200');
$fullStreet = $address->getFullStreet(); // "4th Avenue 10/200"
Instance of Address
may be represented as string that contains all non-empty properties separated by ,
.
Example:
$address = new Address('New York', '00123', '4th Avenue', '10', '200');
$asString = (string)$address; // "4th Avenue 10/200, 00123, New York"
Meritoo\Common\ValueObject\BankAccount
Represents bank account. Contains properties:
$bankName
- name of bank$accountNumber
- number of bank's account
New instance can be created using constructor
new BankAccount('Bank of America', '1234567890')
Has getters for each property getBankName()
and getAccountNumber()
.
Instance of BankAccount
may be represented as string that contains all non-empty properties separated by ,
.
Example:
$bank = new BankAccount('Bank of America', '1234567890');
$asString = (string)$bank; // "Bank of America, 1234567890"
Meritoo\Common\ValueObject\Company
Represents a company. Contains properties:
$name
- name of company$address
- address of company$bankAccount
- bank account of company
New instance can be created using constructor:
new Company(
'Test 1',
new Address('New York', '00123', '4th Avenue', '10', '200'),
new BankAccount('Bank 1', '12345')
);
Has getters for each property getName()
, getAddress()
and getBankAccount()
.
Instance of Company
may be represented as string that contains all non-empty properties separated by ,
.
Example:
$company = new Company(
'Test 1',
new Address('New York', '00123', '4th Avenue', '10', '200'),
new BankAccount('Bank 1', '12345')
);
$asString = (string)$company; // "Test 1, 4th Avenue 10/200, 00123, New York, Bank 1, 12345"
Meritoo\Common\ValueObject\Human
Represents human. Based on \Meritoo\Common\Traits\ValueObject\HumanTrait
trait. Contains properties same
as HumanTrait
trait:
$firstName
- first name$lastName
- last name$email
- email address$birthDate
- birth date
New instance can be created using constructor:
new Human('John', 'Scott', '[email protected]', new \DateTime('2001-01-01'));
Has getters for each property, e.g. getFirstName()
, getEmail()
etc.
Instance of Human
may be represented as string that contains first name, last name and email address (if provided).
Example:
$human1 = new Human('John', 'Scott');
$asString1 = (string)$human1; // "John Scott"
$human2 = new Human('John', 'Scott', '[email protected]', new \DateTime('2001-01-01'));
$asString2 = (string)$human2; // "John Scott <[email protected]>"
Meritoo\Common\ValueObject\Size
Size, e.g. of image. Contains properties:
width
- the widthheight
- the heightunit
- unit used when width or height should be returned with unit, default:"px"
separator
- separator used when converting to string, default:" x "
New instance can be created using static methods:
-
fromArray()
- creates new instance from given array// Using default "px" unit Size::fromArray([200, 100]); // With custom "mm" unit Size::fromArray([200, 100], 'mm');
-
fromString()
- creates new instance from given string// Using default "px" unit and default " x " separator Size::fromString('200 x 100'); // With custom "mm" unit and " X " separator Size::fromString('200 X 100', 'mm', ' X ');
Has:
- getters and setters for
width
andheight
properties. - setter for
separator
property toString()
andtoArray()
methods that returns size represented as string and array
Instance of Size
may be represented as string that contains width and height separated by separator (default: " x "
)
.
Example:
$size = Size::fromArray([200, 100]);
// With default separator
$asString1 = (string)$size; // "200 x 100"
// With custom separator
$size->setSeparator('X');
$asString2 = (string)$size; // "200X100"
Meritoo\Common\ValueObject\Template
Template with placeholders that may be filled by real data. Contains properties:
$content
- raw string with placeholders (content of the template)
New instance can be created using constructor:
new Template('First name: %first_name%');
Each placeholder should be wrapped by %
character, e.g. %first_name%
. If content of template is an empty string or
does not contain 1 placeholder at least, an Meritoo\Common\Exception\ValueObject\Template\InvalidContentException
exception will be thrown.
Examples of invalid content of template:
new Template(''); // An empty string
new Template('test'); // Without placeholders
new Template('This is %test'); // With starting tag only (invalid placeholder)
Has 1 public method: fill(array $values)
. Returns content of the template filled with given values (by replacing
placeholders with their proper values).
Example of usage:
$template = new Template('My name is %name% and I am %profession%');
$result = $template->fill([
'name' => 'Jane',
'profession' => 'photographer',
]); // "My name is Jane and I am photographer"
Throws an Meritoo\Common\Exception\ValueObject\Template\NotEnoughValuesException
exception if there is not enough
values (iow. more placeholders than values).
Meritoo\Common\ValueObject\Version
Represents version of software. Contains properties:
$majorPart
- the "major" part of version$minorPart
- the "minor" part of version$patchPart
- the "patch" part of version
New instance can be created using:
-
Constructor:
new Version(1, 0, 2);
-
Static methods:
fromArray(array $version)
- creates new instance using given version as array
Version::fromArray([1, 0, 2]);
fromString(string $version)
- creates new instance using given version as string:
Version::fromString('1.0.2');
Has getters for each property: getMajorPart()
, getMinorPart()
, getPatchPart()
.
Instance of Version
may be represented as string that contains all properties separated by .
($majorPart
.$minorPart
.$patchPart
).
Example:
$version = new Version(1, 0, 2);
$asString = (string)$version; // "1.0.2"