Skip to content

Commit

Permalink
initial commit - Mappable
Browse files Browse the repository at this point in the history
  • Loading branch information
jarektkaczyk committed Mar 19, 2015
0 parents commit b204368
Show file tree
Hide file tree
Showing 8 changed files with 565 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- hhvm

before_script:
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script:
- ./vendor/bin/phpunit
- ./vendor/bin/phpcs src --standard=psr2
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2014 :author_name <:author_email>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# Sofa/Eloquence

(WIP currently only `Mappable` - inspired by https://github.com/RomainLanz/laravel-attributes-mapper, much more to come soon!)

Useful extensions for the Eloquent ORM.

## Requirements

* This package requires PHP 5.4+

## Usage

### 1. Require the package in your `composer.json`:

```
"require": {
...
"sofa/eloquence": "~0.1@dev",
...
},
```

### 2. Add trait to the model and define mappings:

```
<?php namespace App;
use Sofa\Eloquence\Mappable; // trait
class User extends \Eloquent {
use Mappable;
protected $maps = [
// implicit mapping:
'profile' => ['first_name', 'last_name'],
// explicit mapping:
'picture' => 'profile.piture_path'
];
public function profile()
{
return $this->belongsTo(Profile::class);
}
```

### You can also add mapped attributes to the array representation of your model, just like any accessor:

```
<?php namespace App;
use Sofa\Eloquence\Mappable; // trait
class User extends \Eloquent {
use Mappable;
protected $maps = [
'picture' => 'profile.piture_path'
];
protected $appends = ['picture'];
}
```

### You can get as well as set mapped attributes:

```
$user->profile->first_name; // 'Jarek Tkaczyk'
$user->first_name = 'John Doe';
$user->profile->first_name; // 'John Doe'
// remember to save related model in order to save the changes:
$user->profile->save();
// or simply use push:
$user->push();
```


## Explicit vs. Implicit mappings

`Mappable` offers 2 ways of defining mappings for your convenience.

Let's compare equivalent mappings:
```
// Assuming User belongsTo Profile
// and Profile hasOne Picture
// profiles table: id, first_name, last_name
// pictures table: id, profile_id, path
// User model
// explicit
protected $maps = [
'first_name' => 'profile.first_name', // $user->first_name
'last_name' => 'profile.last_name', // $user->last_name
'picture_path' => 'profile.picture.path', // $user->picture_path
];
// implicit
protected $maps = [
'profile' => ['first_name', 'last_name'], // $user->first_name / ->last_name
'profile.picture' => ['path'], // $user->path
];
```

As you notice behaviour is just the same. However there is slight difference - explicit mapping offers more flexibility, in that you can define custom key for mapped value (`picture_path`), while with implicit mapping you have to use real attribute name defined in the related model (`path`).
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "sofa/eloquence",
"description": "Extensions for the Eloquent ORM.",
"license": "MIT",
"authors": [
{
"name": "Jarek Tkaczyk",
"email": "[email protected]",
"homepage": "http://softonsofa.com/",
"role": "Developer"
}
],
"require": {
"php": ">=5.4"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
"squizlabs/php_codesniffer": "~2.0",
"illuminate/database": "~5.0"
},
"autoload": {
"psr-4": {
"Sofa\\Eloquence\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Sofa\\Eloquence\\Tests\\": "tests"
}
}
}
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Eloquence">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit b204368

Please sign in to comment.