Skip to content

Commit

Permalink
init eloquence-mutable
Browse files Browse the repository at this point in the history
  • Loading branch information
jarektkaczyk committed Oct 13, 2017
0 parents commit 71054d3
Show file tree
Hide file tree
Showing 14 changed files with 807 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/coverage/
/vendor/
/node_modules/
composer.lock
7 changes: 7 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tools:
php_sim: true
php_pdepend: true
php_analyzer: true
filter:
excluded_paths:
- 'tests/*'
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: php

php:
- 7.0
- 7.1

install:
- travis_retry composer require satooshi/php-coveralls:~0.6@stable

before_script:
- mkdir -p build/logs
- travis_retry composer self-update
- travis_retry composer install --prefer-source --no-interaction --dev

script:
- ./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- ./vendor/bin/phpcs src --standard=psr2

after_success:
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then php vendor/bin/coveralls -v; fi;'
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) 2015-2017 Jarek Tkaczyk <jarek@softonsofa.com>

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.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Sofa/Eloquence

[![Build Status](https://travis-ci.org/jarektkaczyk/eloquence-mutable.svg)](https://travis-ci.org/jarektkaczyk/eloquence-mutable) [![Coverage Status](https://coveralls.io/repos/jarektkaczyk/eloquence-mutable/badge.svg)](https://coveralls.io/r/jarektkaczyk/eloquence-mutable) [![Code Quality](https://scrutinizer-ci.com/g/jarektkaczyk/eloquence-mutable/badges/quality-score.png)](https://scrutinizer-ci.com/g/jarektkaczyk/eloquence-mutable) [![Downloads](https://poser.pugx.org/sofa/eloquence-mutable/downloads)](https://packagist.org/packages/sofa/eloquence-mutable) [![stable](https://poser.pugx.org/sofa/eloquence-mutable/v/stable.svg)](https://packagist.org/packages/sofa/eloquence-mutable)

Easy and flexible extensions for the [Eloquent ORM](https://laravel.com/docs/eloquent).

**If I'm saving you some time with my work, you can back me up on [Patreon page](https://patreon.com/jarektkaczyk).**

Currently available extensions:

1. [Searchable](https://github.com/jarektkaczyk/eloquence-base) query - crazy-simple fulltext search through any related model
1. [Validable](https://github.com/jarektkaczyk/eloquence-validable) - self-validating models
2. [Mappable](https://github.com/jarektkaczyk/eloquence-mappable) -map attributes to table fields and/or related models
3. [Metable](https://github.com/jarektkaczyk/eloquence-metable) - meta attributes made easy
4. [Mutable](https://github.com/jarektkaczyk/eloquence-mutable) - flexible attribute get/set mutators with quick setup
5. [Mutator](https://github.com/jarektkaczyk/eloquence-mutable) - pipe-based mutating

## Installation

```bash
composer require sofa/eloquence-mutable
```

**Check the [documentation](https://github.com/jarektkaczyk/eloquence/wiki) for installation and usage info, [website](http://softonsofa.com/tag/eloquence/) for examples and [API reference](http://jarektkaczyk.github.io/eloquence-api)**

## Contribution

Shout out to all the Contributors!

All contributions are welcome, PRs must be **tested** and **PSR-2 compliant**.

To validate your builds before committing use the following composer command:
```bash
composer test
```
49 changes: 49 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "sofa/eloquence-mutable",
"description": "Flexible Searchable, Mappable, Metable, Validation and more extensions for Laravel Eloquent ORM.",
"license": "MIT",
"support": {
"issues": "https://github.com/jarektkaczyk/eloquence-mutable/issues",
"source": "https://github.com/jarektkaczyk/eloquence-mutable"
},
"keywords": [
"laravel",
"eloquent",
"metable",
"searchable",
"mappable",
"mutable"
],
"authors": [
{
"name": "Jarek Tkaczyk",
"email": "jarek@softonsofa.com",
"homepage": "https://softonsofa.com/",
"role": "Developer"
}
],
"require": {
"php": ">=7.0.0",
"sofa/eloquence-base": "5.5.*"
},
"require-dev": {
"phpunit/phpunit": "4.5.0",
"squizlabs/php_codesniffer": "2.3.3",
"mockery/mockery": "0.9.4"
},
"autoload": {
"psr-4": {
"Sofa\\Eloquence\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Sofa\\Eloquence\\Tests\\": "tests"
}
},
"minimum-stability": "stable",
"scripts": {
"test": "phpunit && ./vendor/bin/phpcs src --standard=psr2 --report=diff --colors",
"phpcs": "./vendor/bin/phpcs src --standard=psr2 --report=diff --colors"
}
}
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>
140 changes: 140 additions & 0 deletions src/Mutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

namespace Sofa\Eloquence;

use Sofa\Eloquence\Mutable\Hooks;
use Sofa\Eloquence\Mutator\Mutator;
use Sofa\Eloquence\Contracts\Mutator as MutatorContract;

/**
* @property array $setterMutators
* @property array $getterMutators
*/
trait Mutable
{
/**
* Attribute mutator instance.
*
* @var \Sofa\Eloquence\Contracts\Mutator
*/
protected static $attributeMutator;

/**
* Register hooks for the trait.
*
* @codeCoverageIgnore
*
* @return void
*/
public static function bootMutable()
{
$hooks = new Hooks;

foreach (['setAttribute', 'getAttribute', 'toArray'] as $method) {
static::hook($method, $hooks->{$method}());
}
}

/**
* Mutate mutable attributes for array conversion.
*
* @param array $attributes
* @return array
*/
protected function mutableAttributesToArray(array $attributes)
{
foreach ($attributes as $key => $value) {
if ($this->hasGetterMutator($key)) {
$attributes[$key] = $this->mutableMutate($key, $value, 'getter');
}
}

return $attributes;
}

/**
* Determine whether an attribute has getter mutators defined.
*
* @param string $key
* @return boolean
*/
public function hasGetterMutator($key)
{
return array_key_exists($key, $this->getMutators('getter'));
}

/**
* Determine whether an attribute has setter mutators defined.
*
* @param string $key
* @return boolean
*/
public function hasSetterMutator($key)
{
return array_key_exists($key, $this->getMutators('setter'));
}

/**
* Mutate the attribute.
*
* @param string $key
* @param string $value
* @param string $dir
* @return mixed
*/
protected function mutableMutate($key, $value, $dir)
{
$mutators = $this->getMutatorsForAttribute($key, $dir);

return static::$attributeMutator->mutate($value, $mutators);
}

/**
* Get the mutators for an attribute.
*
* @param string $key
* @return string
*/
protected function getMutatorsForAttribute($key, $dir)
{
return $this->getMutators($dir)[$key];
}

/**
* Get the array of attribute mutators.
*
* @param string $dir
* @return array
*/
public function getMutators($dir)
{
$property = ($dir === 'setter') ? 'setterMutators' : 'getterMutators';

return (property_exists($this, $property)) ? $this->{$property} : [];
}

/**
* Set attribute mutator instance.
*
* @codeCoverageIgnore
*
* @param \Sofa\Eloquence\Contracts\Mutator $mutator
* @return void
*/
public static function setAttributeMutator(MutatorContract $mutator)
{
static::$attributeMutator = $mutator;
}

/**
* Get attribute mutator instance.
*
* @codeCoverageIgnore
*
* @return \Sofa\Eloquence\Contracts\Mutator
*/
public static function getAttributeMutator()
{
return static::$attributeMutator;
}
}
60 changes: 60 additions & 0 deletions src/Mutable/Hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Sofa\Eloquence\Mutable;

/**
* This class provides instance scope for the closures
* so they can be rebound later onto the acutal model.
*/
class Hooks
{
/**
* Register hook on getAttribute method.
*
* @return \Closure
*/
public function getAttribute()
{
return function ($next, $value, $args) {
$key = $args->get('key');

if ($this->hasGetterMutator($key)) {
$value = $this->mutableMutate($key, $value, 'getter');
}

return $next($value, $args);
};
}

/**
* Register hook on setAttribute method.
*
* @return \Closure
*/
public function setAttribute()
{
return function ($next, $value, $args) {
$key = $args->get('key');

if ($this->hasSetterMutator($key)) {
$value = $this->mutableMutate($key, $value, 'setter');
}

return $next($value, $args);
};
}

/**
* Register hook on toArray method.
*
* @return \Closure
*/
public function toArray()
{
return function ($next, $attributes) {
$attributes = $this->mutableAttributesToArray($attributes);

return $next($attributes);
};
}
}
8 changes: 8 additions & 0 deletions src/Mutator/InvalidCallableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Sofa\Eloquence\Mutator;

class InvalidCallableException extends \LogicException
{

}
Loading

0 comments on commit 71054d3

Please sign in to comment.