StdLib
.StdLib
underlies all other sub-components of the framework. The library serves as an auxiliary link, the same as dependency injection or symfony / Yaml in any other PHP project.URLObject
that contains several methods for working with redirects, schemes, ports, etc. In addition to helping with OO wrappers, the library also offers basic validation, methods that help in assembling other Webiny components, a simple trait for implementing Singleton, and much more.StdLib
is that it is implemented as a set of traits - a very much undervalued part of modern PHP development. For example, the above URLObject
is created like this: $this->url('http://www.webiny.com/');
, because the StdObject StdObject
can be added to any class that needs this functionality. Most of the other components of Webiny are also implemented on traits. Due to the isolated nature of the framework, the team chose this approach to simplify the hierarchy of classes and some expansion of capabilities.StdLib
can be used autonomously , directly from Composer, or it can be pulled out along dependencies as part of the entire framework . Let's see, what does she offer us?Exception
, it is used only if you intend to create additional Webiny components. The other is StdObject
, which contains the functionality we talked about.ComponentTrait
is only useful if you create additional Webiny components, which we will not do for now. SkipFactoryLoaderTrait
is useful in invoking class instances that have been dynamically defined. For example: $className = 'myNameSpace\myClass'; $argument1 = 'Hello'; $argument2 = 'World'; $mustBe = 'myNameSpace\mySubClass';
myClass
with “Hello” and “World” arguments, making sure that it is an instance of mySubClass
you can use the following approaches: // standard PHP try { $instance = new $className($argument1, $argument2); if (!($instance instanceof $mustBe)) { throw new Exception("Instances don't match!"); } } catch (Exception $e) { // Handle exception }
// FactoryLoaderTrait approach try { $instance = $this->factory($className, $mustBe, [$argument1, $argument2]); } catch (Exception $e) { // Handle exception }
__construct
conflicts. The trait does not implement its implementation of the __construct
method; it can be used both in classes that have their own implementation of the method, and in those used by other Singleton
implementations . But do not forget, if you decide to use this treyt, you must adapt your class constructor according to the pattern.init
and protected _init
methods are implemented, and they are executed in this order after the instance is created.StdObjectTrait
and ValidatorTrait
. StdLibTrait
itself contains some JSON encoding / decoding help functions, but they are rather scarce at the moment, and other PHP analogs know how to serialize into JSON. Perhaps in this place it is better to use Serializer from Symfony, a component that has been well tested by the community and has multiformat support.ValidatorTrait
is a simple collection of native PHP type checking methods wrapped in a treyt. I am not sure about its usefulness or the reasons that guided the authors, since The API remains almost identical ( self::isArray()
vs is_array()
), but suppose that this is somehow related to the ability to extend the component, overload methods, implementing its own functionality.StdObjectWrapper
to provide a smooth interface for checking these types, which in turn is part of the StdObject StdObject
used in Webiny as an object-oriented wrapper for scalars and URL formatting.StdLib
component, its main part . This trait provides the basic features of the standard class such classes as ArrayObject
, UrlObject
, StringObject
and DateTimeObject
. $string = $this->str("My string");
length
, wordCount
and subStringCount
, as well as wrappers for some of the built-in PHP functions. All this is quite useful to have on hand, right inside the class. Through ManipulatorTrait
, common to all StdObjects
, StringObject
also has access to methods that modify it: for example, trim
is the most frequently used and useful feature when working with strings.string
object, but also an automatic substitution in the IDE: $string = $this->str("This is a string"); echo $string->hash()->padLeft(45, "testing");
testing
to the left to a set of 45 characters. And here is the result - testif72017485fbf6423499baf9b240daa14f5f095a1
. Hardly you can make it even easier and clearer than in the example above. $string = new StringObject('Some test string.'); $string->caseUpper()->trimRight('.')->replace(' test'); // SOME STRING
__toString
method to directly prepare it for use as a string. All other StdLib objects also have the implementation of this method for direct printing. At the exit, you can always get a primitive for further work.StringObject
, ArrayObject
offers a convenient interface for working with an array. Naturally, it is suitable for iterations through the foreach and, in general, behaves almost like a native array. $array = new ArrayObject(['one', 'two', 'three']); $array->first(); // StringObject 'one' $array->append('four')->prepend('zero'); // ['zero', 'one', 'two', 'three', 'four']
StdObject
, not actual scalars. The return value will always be of type StdObjectAbstract
. Strings produce StringObject
, arrays produce ArrayObject
. Numbers, which may be somewhat inconsistent, produce StdObjectWrapper
instances with the _value
property, in which our number is given. If the element is a class, then this class will also be wrapped. For example: $array = $this->arr([1, "2", new myClass(1, 2)]); var_dump($array->last());
StdObjectWrapper
in one form or another. But on the other hand, if I deal with an array of classes, I can not just forget about the wrapper and work as before: each time you need to pay some attention to this aspect. Of course, there is a way to get real values ​​from any StdLib
object by calling the val
method, which pulls out the base value contained inside.ArrayObject
, look at the corresponding trait . I find the chaining syntax especially useful, because you can go down to a multidimensional array by specifying key1.key2.key3
as an index, which, in turn, is a rather impressive time saving: $array = [ 'k1' => 'test', 'k2' => [ 'k3' => [ 'k4' => 'deepest' ] ] ]; $a = new ArrayObject($array); $a->key('k2.k3.k4', 'webiny'); $a->key('k2.k3.k5', 'anotherElement'); echo $a->key('k2.k3.k4'); // webiny echo $a->key('k2.k3.k5'); // anotherElement
URLObject
not inherited from StringObject
. For me, the presence of inheritance would make sense, in fact, they both would benefit from each other's manipulative techniques.DateTimeObject
. This is a wrapper over a datetime object that provides a freer and more natural work interface, as well as helping to solve most of the problems encountered when working with time: $dt = new DateTimeObject('3 months ago'); echo $dt; // 2013-02-12 17:00:36 $dt->add('10 days')->sub('5 hours'); // 2013-02-22 12:00:36
DateTimeObject
will be very convenient if you don’t use any library to work with time yet, but for more complex manipulations with datetime I would recommend Carbon , a little seasoned with the Period library.DateTimeObject
completely useless - by default it supports work with time zones, it has a convenient date format, simple and easy diffs as an alternate Period
, readable “x time ago”, time zone dependent and much more.Source: https://habr.com/ru/post/256309/
All Articles