primo commit
This commit is contained in:
78
libraries/fof30/Model/Mixin/Assertions.php
Normal file
78
libraries/fof30/Model/Mixin/Assertions.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FOF
|
||||
* @copyright Copyright (c)2010-2021 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 2, or later
|
||||
*/
|
||||
|
||||
namespace FOF30\Model\Mixin;
|
||||
|
||||
defined('_JEXEC') || die;
|
||||
|
||||
use Joomla\CMS\Language\Text;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Trait for check() method assertions
|
||||
*/
|
||||
trait Assertions
|
||||
{
|
||||
/**
|
||||
* Make sure $condition is true or throw a RuntimeException with the $message language string
|
||||
*
|
||||
* @param bool $condition The condition which must be true
|
||||
* @param string $message The language key for the message to throw
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function assert($condition, $message)
|
||||
{
|
||||
if (!$condition)
|
||||
{
|
||||
throw new RuntimeException(Text::_($message));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that $value is not empty or throw a RuntimeException with the $message language string
|
||||
*
|
||||
* @param mixed $value The value to check
|
||||
* @param string $message The language key for the message to throw
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function assertNotEmpty($value, $message)
|
||||
{
|
||||
$this->assert(!empty($value), $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that $value is set to one of $validValues or throw a RuntimeException with the $message language string
|
||||
*
|
||||
* @param mixed $value The value to check
|
||||
* @param array $validValues An array of valid values for $value
|
||||
* @param string $message The language key for the message to throw
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function assertInArray($value, array $validValues, $message)
|
||||
{
|
||||
$this->assert(in_array($value, $validValues), $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that $value is set to none of $validValues. Otherwise throw a RuntimeException with the $message language
|
||||
* string.
|
||||
*
|
||||
* @param mixed $value The value to check
|
||||
* @param array $validValues An array of invalid values for $value
|
||||
* @param string $message The language key for the message to throw
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function assertNotInArray($value, array $validValues, $message)
|
||||
{
|
||||
$this->assert(!in_array($value, $validValues, true), $message);
|
||||
}
|
||||
|
||||
}
|
||||
145
libraries/fof30/Model/Mixin/DateManipulation.php
Normal file
145
libraries/fof30/Model/Mixin/DateManipulation.php
Normal file
@ -0,0 +1,145 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FOF
|
||||
* @copyright Copyright (c)2010-2021 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 2, or later
|
||||
*/
|
||||
|
||||
namespace FOF30\Model\Mixin;
|
||||
|
||||
defined('_JEXEC') || die;
|
||||
|
||||
use FOF30\Date\Date;
|
||||
use FOF30\Model\DataModel;
|
||||
use JDatabaseDriver;
|
||||
use JLoader;
|
||||
|
||||
/**
|
||||
* Trait for date manipulations commonly used in models
|
||||
*/
|
||||
trait DateManipulation
|
||||
{
|
||||
/**
|
||||
* Normalise a date into SQL format
|
||||
*
|
||||
* @param string $value The date to normalise
|
||||
* @param string $default The default date to use if the normalised date is invalid or empty (use 'now' for
|
||||
* current date/time)
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function normaliseDate($value, $default = '2001-01-01')
|
||||
{
|
||||
/** @var DataModel $this */
|
||||
|
||||
$db = $this->container->platform->getDbo();
|
||||
|
||||
if (empty($value) || ($value == $db->getNullDate()))
|
||||
{
|
||||
$value = $default;
|
||||
}
|
||||
|
||||
if (empty($value) || ($value == $db->getNullDate()))
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
$regex = '/^\d{1,4}(\/|-)\d{1,2}(\/|-)\d{2,4}[[:space:]]{0,}(\d{1,2}:\d{1,2}(:\d{1,2}){0,1}){0,1}$/';
|
||||
|
||||
if (!preg_match($regex, $value))
|
||||
{
|
||||
$value = $default;
|
||||
}
|
||||
|
||||
if (empty($value) || ($value == $db->getNullDate()))
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
$date = new Date($value);
|
||||
|
||||
$value = $date->toSql();
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the published up/down times in case they are give out of order. If publish_up equals publish_down the
|
||||
* foreverDate will be used for publish_down.
|
||||
*
|
||||
* @param string $publish_up Publish Up date
|
||||
* @param string $publish_down Publish Down date
|
||||
* @param string $foreverDate See above
|
||||
*
|
||||
* @return array (publish_up, publish_down)
|
||||
*/
|
||||
protected function sortPublishDates($publish_up, $publish_down, $foreverDate = '2038-01-18 00:00:00')
|
||||
{
|
||||
$jUp = new Date($publish_up);
|
||||
$jDown = new Date($publish_down);
|
||||
|
||||
if ($jDown->toUnix() < $jUp->toUnix())
|
||||
{
|
||||
$temp = $publish_up;
|
||||
$publish_up = $publish_down;
|
||||
$publish_down = $temp;
|
||||
}
|
||||
elseif ($jDown->toUnix() == $jUp->toUnix())
|
||||
{
|
||||
$jDown = new Date($foreverDate);
|
||||
$publish_down = $jDown->toSql();
|
||||
}
|
||||
|
||||
return [$publish_up, $publish_down];
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish or unpublish a DataModel item based on its publish_up / publish_down fields
|
||||
*
|
||||
* @param DataModel $row The DataModel to publish/unpublish
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function publishByDate(DataModel $row)
|
||||
{
|
||||
static $uNow = null;
|
||||
|
||||
if (is_null($uNow))
|
||||
{
|
||||
$jNow = new Date();
|
||||
$uNow = $jNow->toUnix();
|
||||
}
|
||||
|
||||
/** @var JDatabaseDriver $db */
|
||||
$db = $this->container->platform->getDbo();
|
||||
|
||||
$triggered = false;
|
||||
|
||||
$publishDown = $row->getFieldValue('publish_down');
|
||||
|
||||
if (!empty($publishDown) && ($publishDown != $db->getNullDate()))
|
||||
{
|
||||
$publish_down = $this->normaliseDate($publishDown, '2038-01-18 00:00:00');
|
||||
$publish_up = $this->normaliseDate($row->publish_up, '2001-01-01 00:00:00');
|
||||
|
||||
$jDown = new Date($publish_down);
|
||||
$jUp = new Date($publish_up);
|
||||
|
||||
if (($uNow >= $jDown->toUnix()) && $row->enabled)
|
||||
{
|
||||
$row->enabled = 0;
|
||||
$triggered = true;
|
||||
}
|
||||
elseif (($uNow >= $jUp->toUnix()) && !$row->enabled && ($uNow < $jDown->toUnix()))
|
||||
{
|
||||
$row->enabled = 1;
|
||||
$triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($triggered)
|
||||
{
|
||||
$row->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
65
libraries/fof30/Model/Mixin/Generators.php
Normal file
65
libraries/fof30/Model/Mixin/Generators.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FOF
|
||||
* @copyright Copyright (c)2010-2021 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 2, or later
|
||||
*/
|
||||
|
||||
namespace FOF30\Model\Mixin;
|
||||
|
||||
defined('_JEXEC') || die;
|
||||
|
||||
use Exception;
|
||||
use Generator;
|
||||
use ReflectionObject;
|
||||
|
||||
/**
|
||||
* Trait for PHP 5.5 Generators
|
||||
*/
|
||||
trait Generators
|
||||
{
|
||||
/**
|
||||
* Returns a PHP Generator of DataModel instances based on your currently set Model state. You can foreach() the
|
||||
* returned generator to walk through each item of the data set.
|
||||
*
|
||||
* WARNING! This only works on PHP 5.5 and later.
|
||||
*
|
||||
* When the generator is done you might get a PHP warning. This is normal. Joomla! doesn't support multiple db
|
||||
* cursors being open at once. What we do instead is clone the database object. Of course it cannot close the db
|
||||
* connection when we dispose of it (since it's already in use by Joomla), hence the warning. Pay no attention.
|
||||
*
|
||||
* @param integer $limitstart How many items from the start to skip (0 = do not skip)
|
||||
* @param integer $limit How many items to return (0 = all)
|
||||
* @param bool $overrideLimits Set to true to override limitstart, limit and ordering
|
||||
*
|
||||
* @return Generator A PHP generator of DataModel objects
|
||||
* @throws Exception
|
||||
* @since 3.3.2
|
||||
*/
|
||||
public function &getGenerator($limitstart = 0, $limit = 0, $overrideLimits = false)
|
||||
{
|
||||
$limitstart = max($limitstart, 0);
|
||||
$limit = max($limit, 0);
|
||||
|
||||
$query = $this->buildQuery($overrideLimits);
|
||||
|
||||
$db = clone $this->getDbo();
|
||||
$db->setQuery($query, $limitstart, $limit);
|
||||
$cursor = $db->execute();
|
||||
|
||||
$reflectDB = new ReflectionObject($db);
|
||||
$refFetchAssoc = $reflectDB->getMethod('fetchAssoc');
|
||||
$refFetchAssoc->setAccessible(true);
|
||||
|
||||
while ($data = $refFetchAssoc->invoke($db, $cursor))
|
||||
{
|
||||
$item = clone $this;
|
||||
$item->clearState()->reset(true);
|
||||
$item->bind($data);
|
||||
$item->relationManager = clone $this->relationManager;
|
||||
$item->relationManager->rebase($item);
|
||||
|
||||
yield $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
61
libraries/fof30/Model/Mixin/ImplodedArrays.php
Normal file
61
libraries/fof30/Model/Mixin/ImplodedArrays.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FOF
|
||||
* @copyright Copyright (c)2010-2021 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 2, or later
|
||||
*/
|
||||
|
||||
namespace FOF30\Model\Mixin;
|
||||
|
||||
defined('_JEXEC') || die;
|
||||
|
||||
/**
|
||||
* Trait for dealing with imploded arrays, stored as comma-separated values
|
||||
*/
|
||||
trait ImplodedArrays
|
||||
{
|
||||
/**
|
||||
* Converts the loaded comma-separated list into an array
|
||||
*
|
||||
* @param string $value The comma-separated list
|
||||
*
|
||||
* @return array The exploded array
|
||||
*/
|
||||
protected function getAttributeForImplodedArray($value)
|
||||
{
|
||||
if (is_array($value))
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (empty($value))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
$value = explode(',', $value);
|
||||
$value = array_map('trim', $value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts an array of values into a comma separated list
|
||||
*
|
||||
* @param array $value The array of values
|
||||
*
|
||||
* @return string The imploded comma-separated list
|
||||
*/
|
||||
protected function setAttributeForImplodedArray($value)
|
||||
{
|
||||
if (!is_array($value))
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
$value = array_map('trim', $value);
|
||||
$value = implode(',', $value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
64
libraries/fof30/Model/Mixin/JsonData.php
Normal file
64
libraries/fof30/Model/Mixin/JsonData.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* @package FOF
|
||||
* @copyright Copyright (c)2010-2021 Nicholas K. Dionysopoulos / Akeeba Ltd
|
||||
* @license GNU General Public License version 2, or later
|
||||
*/
|
||||
|
||||
namespace FOF30\Model\Mixin;
|
||||
|
||||
defined('_JEXEC') || die;
|
||||
|
||||
/**
|
||||
* Trait for dealing with data stored as JSON-encoded strings
|
||||
*/
|
||||
trait JsonData
|
||||
{
|
||||
/**
|
||||
* Converts the loaded JSON string into an array
|
||||
*
|
||||
* @param string $value The JSON string
|
||||
*
|
||||
* @return array The data
|
||||
*/
|
||||
protected function getAttributeForJson($value)
|
||||
{
|
||||
if (is_array($value))
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
if (empty($value))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
$value = json_decode($value, true);
|
||||
|
||||
if (empty($value))
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts and array into a JSON string
|
||||
*
|
||||
* @param array $value The data
|
||||
*
|
||||
* @return string The JSON string
|
||||
*/
|
||||
protected function setAttributeForJson($value)
|
||||
{
|
||||
if (!is_array($value))
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
$value = json_encode($value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user