Skip to content

Commit

Permalink
model locale support
Browse files Browse the repository at this point in the history
model validation helpers
  • Loading branch information
jturbide committed Jul 27, 2023
1 parent 7665f33 commit 4425a68
Show file tree
Hide file tree
Showing 3 changed files with 624 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Mvc/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class Model extends \Phalcon\Mvc\Model
use \Zemit\Mvc\Model\Options;
use \Zemit\Mvc\Model\Uuid;
use \Zemit\Mvc\Model\Slug;
use \Zemit\Mvc\Model\Validate;
use \Zemit\Mvc\Model\Locale;

public function initialize(): void
{
Expand Down
110 changes: 110 additions & 0 deletions src/Mvc/Model/Locale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* This file is part of the Zemit Framework.
*
* (c) Zemit Team <contact@zemit.com>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

namespace Zemit\Mvc\Model;

trait Locale
{
/**
* Returns the translation string of the given key
*
* @param array $placeholders
* @param string $translateKey
* @return string
*/
public function _(string $translateKey, array $placeholders = []): string
{
$translate = $this->getDI()->get('translate');
assert($translate instanceof \Phalcon\Translate\Adapter\AbstractAdapter);

return $translate->_($translateKey, $placeholders);
}

/**
* Magic caller to set or get localed named field automagically using the current locale
* - Allow to call $this->getName{Fr|En|Sp|...}
* - Allow to call $this->setName{Fr|En|Sp|...}
*
* @param string $method method name
* @param array $arguments method arguments
* @return mixed
* @throws \Phalcon\Mvc\Model\Exception
*/
public function __call(string $method, array $arguments)
{
$locale = $this->getDI()->get('locale');
assert($locale instanceof \Zemit\Locale);

$lang = $locale->getLocale() ?: 'en';

if (mb_strrpos($method, ucfirst($lang)) !== mb_strlen($method) - mb_strlen($lang)) {
$call = $method . ucfirst($lang);
if (method_exists($this, $call)) {
return $this->$call(...$arguments);
}
}

return parent::__call($method, $arguments);
}

/**
* Magic setter to set localed named field automatically using the current locale
* - Allow to set $this->name{Fr|En|Sp|...} from missing name property
*
* @param string $property property name
* @param mixed $value value to set
* @return void
*/
public function __set(string $property, $value)
{
$locale = $this->getDI()->get('locale');
assert($locale instanceof \Zemit\Locale);

$lang = $locale->getLocale();

if (mb_strrpos($property, ucfirst($lang)) !== mb_strlen($property) - 2) {
$set = $property . ucfirst($lang);

if (property_exists($this, $set)) {
$this->writeAttribute($set, $value);

return;
}
}

parent::__set($property, $value);
}

/**
* Magic getter to get localed named field automatically using the current locale
* - Allow to get $this->name{Fr|En|Sp|...} from missing name property
*
* @param string $property property name
* @return mixed|null
*/
public function __get(string $property)
{
$locale = $this->getDI()->get('locale');
assert($locale instanceof \Zemit\Locale);

$lang = $locale->getLocale();

if (mb_strrpos($property, ucfirst($lang)) !== mb_strlen($property) - 2) {
$set = $property . ucfirst($lang);

if (property_exists($this, $set)) {
return $this->readAttribute($set);
}
}

return parent::__get($property);
}
}
Loading

0 comments on commit 4425a68

Please sign in to comment.