Object To String
, (*1)
Just like the php __toString() method, but with the power of the symfony expression language., (*2)
Installation
Download the Library
Open a command console, enter your project directory and execute the
following command to download the latest stable version of this bundle:, (*3)
$ composer require sk/object-to-string "dev-master"
This command requires you to have Composer installed globally, as explained
in the installation chapter
of the Composer documentation., (*4)
Usage
Before you can use the power of this library you have to initialize it. Depending on your needs, activate the driver you want.
Here is a full example with all available driver:, (*5)
realpath('/path/to/config')));
$driverChain = new DriverChain([
new AnnotationDriver(new AnnotationReader()),
new XmlDriver($fileLocator),
new YamlDriver($fileLocator),
new PhpDriver($fileLocator),
]);
$metadataFactory =new \Metadata\MetadataFactory($driverChain);
$expressionLanguage = new ExpressionLanguage();
$objectToString = new ObjectToString($metadataFactory, $expressionLanguage);
```
But also you can just activate one driver. Just the annotation driver for example:
```php
'", params={"email": "email", "name": "name"})
* @ObjectToString(name="full_email_alternative", format="name ~ ' '", params={"email": "emailAlternative", "name": "name"})
* @ObjectToString(name="full_email_validated", format="_this.getName() ~ (_this.isValidated() ? ' (validated)') ~' '")
*/
class Email
{
/**
* @var string
*/
private $email;
/**
* @var string
*/
private $emailAlternative;
/**
* @var string
*/
private $name;
/**
* @var bool
*/
private $validated = false;
/**
* Email constructor.
*
* @param string $email
* @param string $emailAlternative
* @param string $name
*/
public function __construct($name = null, $email = null, $emailAlternative = null)
{
$this->email = $email;
$this->emailAlternative = $emailAlternative;
$this->name = $name;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
*
* @return Email
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* @return string
*/
public function getEmailAlternative()
{
return $this->emailAlternative;
}
/**
* @param string $emailAlternative
*
* @return Email
*/
public function setEmailAlternative($emailAlternative)
{
$this->emailAlternative = $emailAlternative;
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $name
*
* @return Email
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @param bool $validated
*
* @return Email
*/
public function setValidated($validated)
{
$this->validated = $validated;
return $this;
}
}
```
#### Php
```php
addToString('name', 'name', array('name' => 'name'));
$metadata->addToString('email', 'email', array('email' => 'email'));
$metadata->addToString('email_alternative', 'email', array('email' => 'emailAlternative'));
$metadata->addToString('full_email', 'name ~ \' \'', array('email' => 'email', 'name' => 'name'));
$metadata->addToString(
'full_email_alternative',
'name ~ \' \'',
array('email' => 'emailAlternative', 'name' => 'name')
);
return $metadata;
```
#### YAML
```yaml
# file: /path/to/config/Acme.Model.Email.yml
\Acme\Model\Email:
name:
format: "name"
params:
name: "name"
email:
format: "email"
params:
email: "email"
email_alternative:
format: "email"
params:
email: "emailAlternative"
full_email:
format: "name ~ ' '"
params:
name: "name"
email: "email"
full_email_alternative:
format: "name ~ ' '"
params:
name: "name"
email: "emailAlternative"
```
#### XML
```xml
<object-to-string>
<class name="\Acme\Model\Email">
<name name="name" format="name">
<param name="name">name</param>
</name>
<name name="email" format="email">
<param name="email">email</param>
</name>
<name name="email_alternative" format="email">
<param name="email">emailAlternative</param>
</name>
<name name="full_email" format="name ~ ' <' ~ email ~ '>'">
<param name="email">email</param>
<param name="name">name</param>
</name>
<name name="full_email_alternative" format="name ~ ' <' ~ email ~ '>'">
<param name="email">emailAlternative</param>
<param name="name">name</param>
</name>
</class>
</object-to-string>
```
A note to xml:
As you can see in this example, you have to escape some characters. Here the characters '<' and '>' are replaced with '<' and 'gt;'.
So if you get a xml error like 'Extra content at the end of the document', make sure your xml is valid.
For more information take a look at:
- XML escape characters - on stackoverflow.com
- Character Data and Markup - on www.w3.org, (*6)
### Example Usage
After initializing and defining the names you are now able to use it:
```php
<?php
$email = new \Acme\Model\Email('John Doe', 'john.doe@example.com', 'jd@example.com');
echo $objectToString->generate('name', $email) . "\n";
echo $objectToString->generate('email', $email) . "\n";
echo $objectToString->generate('email_alternative', $email) . "\n";
echo $objectToString->generate('full_email', $email) . "\n";
echo $objectToString->generate('full_email_alternative', $email) . "\n";
echo $objectToString->generate('full_email_validated', $email) . "\n";
$email->setValidated(true);
echo $objectToString->generate('full_email_validated', $email) . "\n";
Output
John Doe
john.doe@example.com
jd@example.com
John Doe <john.doe@example.com>
John Doe <jd@example.com>
John Doe <john.doe@example.com>
John Doe (validated) <john.doe@example.com>
Issues and feature requests
Issues and feature requests are handled on github. If you found a bug, you are always welcome to open an issue. And also feel
free to create a pull request with a fix. Same for feature requests., (*7)
License
This library is under the MIT license. See the complete license in the library LICENSE file., (*8)